12 using System.Collections;
21 public class SpriteMesh : ISpriteMesh
25 protected MeshFilter meshFilter;
26 protected MeshRenderer meshRenderer;
27 protected Mesh m_mesh;
28 protected Texture m_texture;
29 protected Vector3[] m_vertices =
new Vector3[4];
30 protected Color[] m_colors =
new Color[4];
31 protected Vector2[] m_uvs =
new Vector2[4];
32 protected Vector2[] m_uvs2 =
new Vector2[4];
33 protected int[] m_faces =
new int[6];
34 protected bool m_useUV2 =
false;
39 get {
return m_sprite; }
46 if (m_sprite.spriteMesh !=
this)
47 m_sprite.spriteMesh =
this;
52 meshFilter = (MeshFilter)m_sprite.gameObject.GetComponent(typeof(MeshFilter));
53 if (meshFilter == null)
54 meshFilter = (MeshFilter) m_sprite.gameObject.AddComponent(typeof(MeshFilter));
56 meshRenderer = (MeshRenderer)m_sprite.gameObject.GetComponent(typeof(MeshRenderer));
57 if (meshRenderer == null)
58 meshRenderer = (MeshRenderer)m_sprite.gameObject.AddComponent(typeof(MeshRenderer));
60 m_mesh = meshFilter.sharedMesh;
62 if (meshRenderer.sharedMaterial != null)
63 m_texture = meshRenderer.sharedMaterial.GetTexture(
"_MainTex");
64 #if WARN_ON_NO_MATERIAL
66 Debug.LogWarning(
"Sprite on GameObject \"" + m_sprite.name +
"\" has not been assigned a material.");
71 public virtual Texture texture
73 get {
return m_texture; }
76 public virtual Material material
78 get {
return meshRenderer.sharedMaterial; }
81 meshRenderer.sharedMaterial = value;
82 m_texture = meshRenderer.sharedMaterial.mainTexture;
83 if (m_sprite != null && m_texture != null)
84 m_sprite.SetPixelToUV(m_texture);
88 public virtual Vector3[] vertices
90 get {
return m_vertices; }
93 public virtual Vector2[] uvs
98 public virtual Vector2[] uvs2
100 get {
return m_uvs2; }
103 public virtual bool UseUV2
105 get {
return m_useUV2; }
106 set { m_useUV2 = value; }
109 public virtual Mesh mesh
120 set { m_mesh = value; }
123 public virtual void Init()
132 m_mesh.vertices = m_vertices;
134 m_mesh.colors = m_colors;
135 m_mesh.triangles = m_faces;
137 SetWindingOrder(m_sprite.winding);
140 if (!m_sprite.uvsInitialized)
143 m_sprite.uvsInitialized =
true;
146 m_sprite.SetBleedCompensation(m_sprite.bleedCompensation);
149 if (m_sprite.pixelPerfect)
151 if (m_texture == null)
153 if (meshRenderer.sharedMaterial != null)
154 m_texture = meshRenderer.sharedMaterial.GetTexture(
"_MainTex");
157 if (m_texture != null)
159 m_sprite.SetPixelToUV(m_texture);
162 if (m_sprite.renderCamera == null)
163 m_sprite.SetCamera(Camera.main);
165 m_sprite.SetCamera(m_sprite.renderCamera);
167 else if (!m_sprite.hideAtStart)
168 m_sprite.SetSize(m_sprite.width, m_sprite.height);
170 m_mesh.RecalculateNormals();
173 m_sprite.SetColor(m_sprite.color);
176 protected void CreateMesh()
178 meshFilter.sharedMesh =
new Mesh();
179 m_mesh = meshFilter.sharedMesh;
180 #if UNITY_4 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9
181 m_mesh.MarkDynamic();
183 if (m_sprite.persistent)
184 GameObject.DontDestroyOnLoad(m_mesh);
187 public virtual void UpdateVerts()
192 m_mesh.vertices = m_vertices;
193 m_mesh.RecalculateBounds();
194 #if SPRITE_WANT_NORMALS
195 m_mesh.RecalculateNormals();
199 public virtual void UpdateUVs()
209 public virtual void UpdateColors(Color color)
218 m_mesh.colors = m_colors;
222 public virtual void Hide(
bool tf)
224 if (meshRenderer == null)
227 meshRenderer.enabled = !tf;
230 public virtual bool IsHidden()
232 if (meshRenderer == null)
235 return !meshRenderer.enabled;
238 public void SetPersistent()
241 GameObject.DontDestroyOnLoad(m_mesh);
274 m_mesh.triangles = m_faces;
The root class of all sprites. Does not assume any animation capabilities or atlas packing...