実現したいこと
OutOfMemoryExceptionのエラーを解消したい
エラーメッセージ
OutOfMemoryException: Out of memory ExampleClass.GrassGenerate () (at Assets/Script/ExampleClass.cs:133) ExampleClass.Start () (at Assets/Script/ExampleClass.cs:36)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using System.Linq; 4using UnityEngine; 5 6public class ExampleClass : MonoBehaviour 7{ 8 public int rows; 9 public int columns; 10 11 Mesh mesh; 12 Matrix4x4[][] matrices; 13 int count; 14 public Material material; 15 16 Mesh grassMesh; 17 Matrix4x4[][] grassMatrices; 18 int grassCount; 19 public Material grassMaterial; 20 21 Mesh grassMesh2; 22 Matrix4x4[][] grassMatrices2; 23 int grassCount2; 24 public Material grassMaterial2; 25 26 Mesh lotusMesh; 27 Matrix4x4[][] lotusMatrices; 28 int lotusCount; 29 public Material lotusMaterial; 30 31 private void Start() 32 { 33 rows = World.instance.width; 34 columns = World.instance.height; 35 Instance(); 36 GrassGenerate(); 37 GrassGenerate2(); 38 LotusGenerate(); 39 } 40 41 void Instance() 42 { 43 mesh = new Mesh(); 44 mesh.vertices = new Vector3[] { 45 new Vector3 (0, 0, 0), 46 new Vector3 (0, 1, 0), 47 new Vector3 (1 ,0, 0), 48 new Vector3 (1, 1, 0), 49 }; 50 51 mesh.uv = new Vector2[] { 52 new Vector2 (0, 0), 53 new Vector2 (0, 1), 54 new Vector2 (1, 0), 55 new Vector2 (1, 1), 56 }; 57 58 mesh.triangles = new int[] { 59 0, 1, 2, 60 1, 3, 2, 61 }; 62 63 mesh.RecalculateNormals(); 64 mesh.RecalculateBounds(); 65 66 count = rows * columns; 67 var flatMatrices = new Matrix4x4[count]; 68 69 for(var i = 0;i < World.instance.NotWaterCoordinates.Count; i++) 70 { 71 var index = i; 72 73 var position = World.instance.NotWaterCoordinates[i]; 74 75 var rotation = Quaternion.identity; 76 77 var scale = Vector3.one; 78 79 flatMatrices[index] = Matrix4x4.TRS(position, rotation, scale); 80 } 81 82 matrices = flatMatrices.Select((m, i) => (m, i / 1023)) 83 .GroupBy(t => t.Item2) 84 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 85 } 86 87 void GrassGenerate() 88 { 89 grassMesh = new Mesh(); 90 grassMesh.vertices = new Vector3[] { 91 new Vector3 (0, 0, 0), 92 new Vector3 (0, 1, 0), 93 new Vector3 (1 ,0, 0), 94 new Vector3 (1, 1, 0), 95 }; 96 97 grassMesh.uv = new Vector2[] { 98 new Vector2 (0, 0), 99 new Vector2 (0, 1), 100 new Vector2 (1, 0), 101 new Vector2 (1, 1), 102 }; 103 104 grassMesh.triangles = new int[] { 105 0, 1, 2, 106 1, 3, 2, 107 }; 108 109 grassMesh.RecalculateNormals(); 110 grassMesh.RecalculateBounds(); 111 112 //grassCount = rows * columns; 113 grassCount = World.instance.GrassCoordinates.Count; 114 var flatMatrices = new Matrix4x4[grassCount]; 115 116 for(var i = 0;i < World.instance.GrassCoordinates.Count; i++) 117 { 118 var index = i; 119 120 var position = World.instance.GrassCoordinates[i]; 121 122 var rotation = Quaternion.identity; 123 124 var scale = Vector3.one; 125 126 flatMatrices[index] = Matrix4x4.TRS(position, rotation, scale); 127 } 128 129 grassMatrices = flatMatrices.Select((m, i) => (m, i / 1023)) 130 .GroupBy(t => t.Item2) 131 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 132 } 133 134 void GrassGenerate2() 135 { 136 grassMesh2 = new Mesh(); 137 grassMesh2.vertices = new Vector3[] { 138 new Vector3 (0, 0, 0), 139 new Vector3 (0, 1, 0), 140 new Vector3 (1 ,0, 0), 141 new Vector3 (1, 1, 0), 142 }; 143 144 grassMesh2.uv = new Vector2[] { 145 new Vector2 (0, 0), 146 new Vector2 (0, 1), 147 new Vector2 (1, 0), 148 new Vector2 (1, 1), 149 }; 150 151 grassMesh2.triangles = new int[] { 152 0, 1, 2, 153 1, 3, 2, 154 }; 155 156 grassMesh2.RecalculateNormals(); 157 grassMesh2.RecalculateBounds(); 158 159 grassCount2 = rows * columns; 160 var flatMatrices = new Matrix4x4[grassCount2]; 161 162 for (var i = 0; i < World.instance.GrassCoordinates2.Count; i++) 163 { 164 var index = i; 165 166 var position = World.instance.GrassCoordinates2[i]; 167 168 var rotation = Quaternion.identity; 169 170 var scale = Vector3.one; 171 172 flatMatrices[index] = Matrix4x4.TRS(position, rotation, scale); 173 } 174 175 grassMatrices2 = flatMatrices.Select((m, i) => (m, i / 1023)) 176 .GroupBy(t => t.Item2) 177 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 178 } 179 180 void LotusGenerate() 181 { 182 lotusMesh = new Mesh(); 183 lotusMesh.vertices = new Vector3[] { 184 new Vector3 (0, 0, 0), 185 new Vector3 (0, 1, 0), 186 new Vector3 (1 ,0, 0), 187 new Vector3 (1, 1, 0), 188 }; 189 190 lotusMesh.uv = new Vector2[] { 191 new Vector2 (0, 0), 192 new Vector2 (0, 1), 193 new Vector2 (1, 0), 194 new Vector2 (1, 1), 195 }; 196 197 lotusMesh.triangles = new int[] { 198 0, 1, 2, 199 1, 3, 2, 200 }; 201 202 lotusMesh.RecalculateNormals(); 203 lotusMesh.RecalculateBounds(); 204 205 lotusCount = rows * columns; 206 var flatMatrices = new Matrix4x4[lotusCount]; 207 208 for (var i = 0; i < World.instance.LotusCoordinates.Count; i++) 209 { 210 var index = i; 211 212 var position = World.instance.LotusCoordinates[i]; 213 214 var rotation = Quaternion.identity; 215 216 var scale = Vector3.one; 217 218 flatMatrices[index] = Matrix4x4.TRS(position, rotation, scale); 219 } 220 221 lotusMatrices = flatMatrices.Select((m, i) => (m, i / 1023)) 222 .GroupBy(t => t.Item2) 223 .Select(g => g.Select(t => t.Item1).ToArray()).ToArray(); 224 } 225 226 void Update() 227 { 228 foreach (var m in matrices) 229 { 230 Graphics.DrawMeshInstanced(mesh, 0, material, m); 231 } 232 233 foreach(var g in grassMatrices) 234 { 235 Graphics.DrawMeshInstanced(grassMesh, 0, grassMaterial, g); 236 } 237 238 foreach(var g2 in grassMatrices2) 239 { 240 Graphics.DrawMeshInstanced(grassMesh2, 0, grassMaterial2, g2); 241 } 242 243 foreach(var l in lotusMatrices) 244 { 245 Graphics.DrawMeshInstanced(lotusMesh, 0, lotusMaterial, l); 246 } 247 } 248}
試したこと
World.csにGrassAddというメソッドがあるのですが、この関数を使用するとエラーになります。
ExampleClass.csのGrassGenerateメソッドで、
C#
1grassCount = World.instance.GrassCoordinates.Count; 2
としていますが、エラーの原因となっているようです。恐らく、grassCountが大きすぎるのかもしれません。
自分がやりたいことは、草のテクスチャが描かれたメッシュを大量に生成することなのですが、OutOfMemoryException、メモリ不足となってしまいます。
どのように書き直せばいいでしょうか? 回答お願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。