Optimize GC for value updating in text component.
This commit is contained in:
@@ -42,7 +42,6 @@ namespace Cryville.Crtr.Components {
|
||||
foreach (var m in meshes) m.Value.Destroy();
|
||||
}
|
||||
|
||||
readonly Dictionary<Texture2D, MeshWrapper> meshes = new Dictionary<Texture2D, MeshWrapper>();
|
||||
Dictionary<char, SpriteInfo> m_frames;
|
||||
public Dictionary<char, SpriteInfo> Frames {
|
||||
get { return m_frames; }
|
||||
@@ -75,31 +74,38 @@ namespace Cryville.Crtr.Components {
|
||||
float frameHeight = 0;
|
||||
foreach (var m in meshes) m.Value.Destroy();
|
||||
meshes.Clear();
|
||||
verts.Clear();
|
||||
uvs.Clear();
|
||||
foreach (var f in m_frames) {
|
||||
f.Value.Load();
|
||||
if (frameHeight == 0) frameHeight = f.Value.Rect.height;
|
||||
else if (frameHeight != f.Value.Rect.height) throw new Exception("Inconsistent frame height");
|
||||
if (!meshes.ContainsKey(f.Value.Frame.Texture)) {
|
||||
var tex = f.Value.Frame.Texture;
|
||||
if (!meshes.ContainsKey(tex)) {
|
||||
var m = new MeshWrapper();
|
||||
m.Init(mesh.MeshTransform);
|
||||
m.Mesh = new Mesh();
|
||||
m.Renderer.material.mainTexture = f.Value.Frame.Texture;
|
||||
meshes.Add(f.Value.Frame.Texture, m);
|
||||
m.Renderer.material.mainTexture = tex;
|
||||
meshes.Add(tex, m);
|
||||
verts.Add(tex, new List<Vector3>());
|
||||
uvs.Add(tex, new List<Vector2>());
|
||||
tris.Add(tex, new List<int>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float sum_x;
|
||||
readonly Dictionary<Texture2D, MeshWrapper> meshes = new Dictionary<Texture2D, MeshWrapper>();
|
||||
readonly Dictionary<Texture2D, List<Vector3>> verts = new Dictionary<Texture2D, List<Vector3>>();
|
||||
readonly Dictionary<Texture2D, List<Vector2>> uvs = new Dictionary<Texture2D, List<Vector2>>();
|
||||
readonly Dictionary<Texture2D, List<int>> tris = new Dictionary<Texture2D, List<int>>();
|
||||
void UpdateMeshes() {
|
||||
// TODO optimize GC
|
||||
if (meshes.Count == 0) return;
|
||||
sum_x = 0;
|
||||
int vc = m_value.Length * 4;
|
||||
Dictionary<Texture2D, List<Vector3>> verts = new Dictionary<Texture2D, List<Vector3>>();
|
||||
Dictionary<Texture2D, List<Vector2>> uvs = new Dictionary<Texture2D, List<Vector2>>();
|
||||
foreach (var t in meshes.Keys) {
|
||||
verts.Add(t, new List<Vector3>(vc));
|
||||
uvs.Add(t, new List<Vector2>(vc));
|
||||
verts[t].Clear();
|
||||
uvs[t].Clear();
|
||||
tris[t].Clear();
|
||||
}
|
||||
foreach (var c in m_value) {
|
||||
var f = m_frames[c];
|
||||
@@ -119,18 +125,18 @@ namespace Cryville.Crtr.Components {
|
||||
var m = meshes[t].Mesh;
|
||||
m.Clear();
|
||||
int cc = verts[t].Count / 4;
|
||||
int[] tris = new int[cc * 6];
|
||||
var _tris = tris[t];
|
||||
for (int i = 0; i < cc; i++) {
|
||||
tris[i * 6 ] = i * 4 ;
|
||||
tris[i * 6 + 1] = i * 4 + 3;
|
||||
tris[i * 6 + 2] = i * 4 + 1;
|
||||
tris[i * 6 + 3] = i * 4 + 1;
|
||||
tris[i * 6 + 4] = i * 4 + 3;
|
||||
tris[i * 6 + 5] = i * 4 + 2;
|
||||
_tris.Add(i * 4);
|
||||
_tris.Add(i * 4 + 3);
|
||||
_tris.Add(i * 4 + 1);
|
||||
_tris.Add(i * 4 + 1);
|
||||
_tris.Add(i * 4 + 3);
|
||||
_tris.Add(i * 4 + 2);
|
||||
}
|
||||
m.vertices = verts[t].ToArray();
|
||||
m.uv = uvs[t].ToArray();
|
||||
m.triangles = tris;
|
||||
m.SetVertices(verts[t]);
|
||||
m.SetUVs(0, uvs[t]);
|
||||
m.SetTriangles(tris[t], 0);
|
||||
m.RecalculateNormals();
|
||||
}
|
||||
sum_x -= m_spacing;
|
||||
|
Reference in New Issue
Block a user