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