SpriteManager 2
 All Classes Functions Variables Enumerations Enumerator Properties
PackedSprite.cs
1 //-----------------------------------------------------------------
2 // Copyright 2010 Brady Wright and Above and Beyond Software
3 // All rights reserved
4 //-----------------------------------------------------------------
5 
6 
7 using UnityEngine;
8 using System.Collections;
9 using System.Collections.Generic;
10 
11 
12 
19 [AddComponentMenu("SpriteManager 2/PackedSprite")]
21 {
25  [HideInInspector]
26  public string staticTexPath="";
27  [HideInInspector]
28  public string staticTexGUID="";
29 
30  // This will hold the UV coordinates calculated
31  // by the atlas builder for our static texture:
32  [HideInInspector]
33  public CSpriteFrame _ser_stat_frame_info = new CSpriteFrame();
34  [HideInInspector]
35  public SPRITE_FRAME staticFrameInfo;
36 
37 
43 
44  public override TextureAnim[] States
45  {
46  get
47  {
48  if (textureAnimations == null)
50  return textureAnimations;
51  }
52  set { textureAnimations = value; }
53  }
54 
55  public override CSpriteFrame DefaultFrame
56  {
57  get { return _ser_stat_frame_info; }
58  }
59 
60  public override TextureAnim DefaultState
61  {
62  get
63  {
64  if (textureAnimations != null)
65  if (textureAnimations.Length != 0)
66  if(defaultAnim < textureAnimations.Length)
68 
69  return null;
70  }
71  }
72 
73  public override Vector2 GetDefaultPixelSize(PathFromGUIDDelegate guid2Path, AssetLoaderDelegate loader)
74  {
75  if (staticTexGUID == "")
76  return Vector2.zero;
77 
78  Texture2D tex = (Texture2D)loader(guid2Path(staticTexGUID), typeof(Texture2D));
79  Vector2 size = new Vector2(tex.width * (1f / (_ser_stat_frame_info.scaleFactor.x * 2f)), tex.height * (1f / (_ser_stat_frame_info.scaleFactor.y * 2f)));
80 
81  return size;
82  }
83 
84  public override bool SupportsArbitraryAnimations
85  {
86  get { return true; }
87  }
88 
89 
90  protected override void Awake()
91  {
92  if (textureAnimations == null)
94 
95  staticFrameInfo = _ser_stat_frame_info.ToStruct();
96 
97  base.Awake();
98 
99  Init();
100  }
101 
102 
103  public override void Start()
104  {
105  if (m_started)
106  return;
107  base.Start();
108 
109  // See if we should play a default animation:
110  if (playAnimOnStart && defaultAnim < animations.Length)
111  if (Application.isPlaying)
113  }
114 
115 
116  protected override void Init()
117  {
118  base.Init();
119  }
120 
121 
122 
127  public override void Copy(SpriteRoot s)
128  {
129  base.Copy(s);
130 
131  PackedSprite sp;
132 
133  // Check the type:
134  if (!(s is PackedSprite))
135  return;
136 
137  sp = (PackedSprite)s;
138 
139  if (!sp.m_started)
140  staticFrameInfo = sp._ser_stat_frame_info.ToStruct();
141  else
142  staticFrameInfo = sp.staticFrameInfo;
143 
144  if (curAnim != null)
145  {
146  if (curAnim.index == -1)
147  PlayAnim(curAnim);
148  else
149  SetState(curAnim.index);
150  }
151  else
152  {
153  frameInfo = staticFrameInfo;
154  uvRect = frameInfo.uvs;
155 
156  if (autoResize || pixelPerfect)
157  CalcSize();
158  else
159  SetSize(s.width, s.height);
160  }
161 
162  SetBleedCompensation();
163  }
164 
165 
166  // Implements the functionality of acquiring our "static" UV coordinates:
167  public override void InitUVs()
168  {
169  frameInfo = staticFrameInfo;
170  uvRect = staticFrameInfo.uvs;
171  }
172 
173  //-----------------------------------------------------------------
174  // Animation-related routines:
175  //-----------------------------------------------------------------
176 
177 
182  public void AddAnimation(UVAnimation anim)
183  {
184  UVAnimation[] temp;
185  temp = animations;
186 
187  animations = new UVAnimation[temp.Length + 1];
188  temp.CopyTo(animations, 0);
189 
190  animations[animations.Length - 1] = anim;
191  }
192 
193 
199  public override void Aggregate(PathFromGUIDDelegate guid2Path, LoadAssetDelegate load, GUIDFromPathDelegate path2Guid)
200  {
201  List<Texture2D> texList = new List<Texture2D>();
202  List<CSpriteFrame> frameList = new List<CSpriteFrame>();
203 
204  for (int i = 0; i < textureAnimations.Length; ++i)
205  {
206  textureAnimations[i].Allocate();
207 
208  // First try GUIDs:
209  if (textureAnimations[i].frameGUIDs.Length >= textureAnimations[i].framePaths.Length)
210  {
211  for (int j = 0; j < textureAnimations[i].frameGUIDs.Length; ++j)
212  {
213  string path = guid2Path(textureAnimations[i].frameGUIDs[j]);
214  texList.Add((Texture2D)load(path, typeof(Texture2D)));
215  frameList.Add(textureAnimations[i].spriteFrames[j]);
216  }
217 
218  // Make sure we always use GUIDs in the future:
219  textureAnimations[i].framePaths = new string[0];
220  }
221  else
222  {
223  textureAnimations[i].frameGUIDs = new string[textureAnimations[i].framePaths.Length];
224 
225  textureAnimations[i].spriteFrames = new CSpriteFrame[textureAnimations[i].framePaths.Length];
226  for (int j = 0; j < textureAnimations[i].spriteFrames.Length; ++j)
227  textureAnimations[i].spriteFrames[j] = new CSpriteFrame();
228 
229  for (int j = 0; j < textureAnimations[i].framePaths.Length; ++j)
230  {
231  if (textureAnimations[i].framePaths[j].Length < 1)
232  continue;
233 
234  // First get a GUID and save it:
235  textureAnimations[i].frameGUIDs[j] = path2Guid(textureAnimations[i].framePaths[j]);
236 
237  texList.Add((Texture2D)load(textureAnimations[i].framePaths[j], typeof(Texture2D)));
238  frameList.Add(textureAnimations[i].spriteFrames[j]);
239  }
240  }
241  }
242 
243  // Get the static frame info:
244 
245  // First try GUID:
246  if(staticTexGUID.Length > 1)
247  {
248  staticTexPath = guid2Path(staticTexGUID);
249  }
250  else // Else, populate the GUID:
251  {
252  staticTexGUID = path2Guid(staticTexPath);
253  }
254 
255  texList.Add((Texture2D)load(staticTexPath, typeof(Texture2D)));
256  frameList.Add(_ser_stat_frame_info);
257 
258  sourceTextures = texList.ToArray();
259  spriteFrames = frameList.ToArray();
260  }
261 
262 
270  static public PackedSprite Create(string name, Vector3 pos)
271  {
272  GameObject go = new GameObject(name);
273  go.transform.position = pos;
274  return (PackedSprite)go.AddComponent(typeof(PackedSprite));
275  }
276 
285  static public PackedSprite Create(string name, Vector3 pos, Quaternion rotation)
286  {
287  GameObject go = new GameObject(name);
288  go.transform.position = pos;
289  go.transform.rotation = rotation;
290  return (PackedSprite)go.AddComponent(typeof(PackedSprite));
291  }
292 }
virtual void SetSize(float w, float h)
Sets the physical dimensions of the sprite in the plane selected
Definition: SpriteRoot.cs:1384
bool pixelPerfect
Automatically sizes the sprite so that it will display pixel-perfect on-screen. NOTE: If you change t...
Definition: SpriteRoot.cs:762
string staticTexPath
Definition: PackedSprite.cs:26
TextureAnim[] textureAnimations
The animations as defined using individual textures. See TextureAnim
Definition: PackedSprite.cs:42
override void Copy(SpriteRoot s)
Copies all the attributes of another sprite.
int index
The index of this animation in the sprite's animation list. This value is only populated by and used ...
Definition: SpriteBase.cs:103
bool autoResize
Automatically resizes the sprite based on its new UV dimensions compared to its previous dimensions...
Definition: SpriteRoot.cs:771
float width
Width of the sprite in world space.
Definition: SpriteRoot.cs:732
void CalcSize()
Recalculates the width and height of the sprite based upon the change in its UV dimensions (autoResiz...
Definition: SpriteRoot.cs:1350
int defaultAnim
Index of the animation to play by default.
Definition: SpriteBase.cs:1297
bool playAnimOnStart
When set to true, the sprite will play the default animation (see defaultAnim) when the sprite is ins...
Definition: SpriteBase.cs:1284
void PlayAnim(UVAnimation anim, int frame)
Starts playing the specified animation Note: this doesn't resume from a pause, it completely restarts...
override void Aggregate(PathFromGUIDDelegate guid2Path, LoadAssetDelegate load, GUIDFromPathDelegate path2Guid)
Collects all textures intended for packing, as well as sprite frames, together into a standard form f...
static PackedSprite Create(string name, Vector3 pos, Quaternion rotation)
Creates a GameObject and attaches this component type to it.
The root class of all sprites. Does not assume any animation capabilities or atlas packing...
Definition: SpriteRoot.cs:628
void AddAnimation(UVAnimation anim)
Adds an animation to the sprite for its use.
float height
Height of the sprite in world space.
Definition: SpriteRoot.cs:737
override void SetState(int index)
Sets the sprite to the specified state/animation.
static PackedSprite Create(string name, Vector3 pos)
Creates a GameObject and attaches this component type to it.
UVAnimation[] animations
Holds the actual UV sequences that will be used at run-time.