C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click1{ 6 7 public enum Type { Water } 8 public Type type; 9 10 public Click1(Type type) 11 { 12 this.type = Type.Water; 13 } 14} 15
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System; 5 6public class Click2 : MonoBehaviour { 7 8 public static Click2 instance; 9 10 public Material material; 11 12 public Click1[,] tiles; 13 14 private int i; 15 private int j; 16 17 // Use this for initialization 18 void Awake () { 19 instance = this; 20 } 21 22 private void Start() 23 { 24 tiles = new Click1[i, j]; 25 } 26 27 // Update is called once per frame 28 void Update () { 29 if (Input.GetMouseButtonDown(0)) 30 { 31 Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); 32 i = (int)pos.x; 33 j = (int)pos.y; 34 tiles[i, j] = new Click1(Click1.Type.Water); 35 GenerateMesh(i, j); 36 } 37 } 38 39 /*void CreateTiles() 40 { 41 tiles[i, j] = new Click1(Click1.Type.Water); 42 GenerateMesh(i, j); 43 }*/ 44 45 void GenerateMesh(int x, int y) 46 { 47 48 Click3 data = new Click3(i, j); 49 50 GameObject meshGO = new GameObject("CHUNK_" + x + "_" + y); 51 meshGO.transform.SetParent(this.transform); 52 53 MeshFilter filter = meshGO.AddComponent<MeshFilter>(); 54 MeshRenderer render = meshGO.AddComponent<MeshRenderer>(); 55 render.material = material; 56 57 Mesh mesh = filter.mesh; 58 59 mesh.vertices = data.vertices.ToArray(); 60 mesh.triangles = data.triangles.ToArray(); 61 mesh.uv = data.UVs.ToArray(); 62 } 63 64 public Click1 GetTileAt(int x, int y) 65 { 66 return tiles[i, j]; 67 } 68} 69
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click3{ 6 7 public List<Vector3> vertices; 8 public List<Vector2> UVs; 9 public List<int> triangles; 10 11 public Click3(int x, int y) 12 { 13 vertices = new List<Vector3>(); 14 UVs = new List<Vector2>(); 15 triangles = new List<int>(); 16 17 CreateSquare(x, y); 18 } 19 20 void CreateSquare(int x, int y) 21 { 22 23 24 25 Click1 tile = Click2.instance.GetTileAt(x, y); 26 27 vertices.Add(new Vector3(x + 0, y + 0)); 28 vertices.Add(new Vector3(x + 1, y + 0)); 29 vertices.Add(new Vector3(x + 0, y + 1)); 30 vertices.Add(new Vector3(x + 1, y + 1)); 31 32 triangles.Add(vertices.Count - 1); 33 triangles.Add(vertices.Count - 3); 34 triangles.Add(vertices.Count - 4); 35 36 triangles.Add(vertices.Count - 2); 37 triangles.Add(vertices.Count - 1); 38 triangles.Add(vertices.Count - 4); 39 40 UVs.AddRange(Click4.instance.GetTileUVs(tile)); 41 } 42} 43
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click4 : MonoBehaviour { 6 7 public static Click4 instance; 8 9 Dictionary<string, Vector2[]> tileUVMap; 10 11 public Sprite[] a; 12 13 // Use this for initialization 14 void Awake() 15 { 16 17 instance = this; 18 19 tileUVMap = new Dictionary<string, Vector2[]>(); 20 21 //Sprite[] sprites = Resources.LoadAll<Sprite> (""); 22 23 float imageWidth = 0f; 24 float imageHeight = 0f; 25 26 foreach (Sprite s in a) 27 { 28 29 if (s.rect.x + s.rect.width > imageWidth) 30 imageWidth = s.rect.x + s.rect.width; 31 32 if (s.rect.y + s.rect.height > imageHeight) 33 imageHeight = s.rect.y + s.rect.height; 34 } 35 36 foreach (Sprite s in a) 37 { 38 39 Vector2[] uvs = new Vector2[4]; 40 41 uvs[0] = new Vector2(s.rect.x / imageWidth, s.rect.y / imageHeight); 42 uvs[1] = new Vector2((s.rect.x + s.rect.width) / imageWidth, s.rect.y / imageHeight); 43 uvs[2] = new Vector2(s.rect.x / imageWidth, (s.rect.y + s.rect.height) / imageHeight); 44 uvs[3] = new Vector2((s.rect.x + s.rect.width) / imageWidth, (s.rect.y + s.rect.height) / imageHeight); 45 46 tileUVMap.Add(s.name, uvs); 47 } 48 49 } 50 51 // Update is called once per frame 52 void Update() 53 { 54 55 } 56 57 public Vector2[] GetTileUVs(Click1 tile) 58 { 59 60 string key = tile.type.ToString(); 61 62 if (tileUVMap.ContainsKey(key) == true) 63 { 64 65 return tileUVMap[key]; 66 } 67 else 68 { 69 70 Debug.LogError("There is no UV map for tile type: " + key); 71 return tileUVMap["Void"]; 72 } 73 } 74 75} 76
クリックした場所にメッシュを一つ生成するスクリプトを書いているのですが、二つ目のスクリプトの
tiles[i, j] = new Click1(Click1.Type.Water);
という箇所でエラーになります。
IndexOutOfRangeException: Array index is out of range.
Click2.Update () (at Assets/Scripts/Click2.cs:34)
どうしてエラーになるのか分からないので、教えてもらえないでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/07/26 23:39
2018/07/27 23:48
退会済みユーザー
2018/07/28 02:32