115 curFrame = frames.Length - 1;
124 if((curFrame + stepDir) >= frames.Length || (curFrame + stepDir) < 0)
127 if( stepDir>0 && loopReverse )
136 if (numLoops + 1 > loopCycles && loopCycles != -1)
174 public Vector2[]
BuildUVAnim(Vector2 start, Vector2 cellSize,
int cols,
int rows,
int totalCells,
float fps)
178 frames =
new Vector2[totalCells];
183 for(
int row=0; row < rows; ++row)
185 for(
int col=0; col<cols && cellCount < totalCells; ++col)
187 frames[cellCount].x = start.x + cellSize.x * ((float)col);
188 frames[cellCount].y = start.y - cellSize.y * ((float)row);
208 Vector2[] tempFrames =
frames;
210 frames =
new Vector2[frames.Length + anim.Length];
211 tempFrames.CopyTo(frames, 0);
212 anim.CopyTo(frames, tempFrames.Length);
246 public bool autoUpdateBounds =
false;
248 protected ArrayList availableBlocks =
new ArrayList();
249 protected bool vertsChanged =
false;
250 protected bool uvsChanged =
false;
251 protected bool colorsChanged =
false;
252 protected bool vertCountChanged =
false;
253 protected bool updateBounds =
false;
255 protected ArrayList activeBlocks =
new ArrayList();
256 protected ArrayList activeBillboards =
new ArrayList();
257 protected ArrayList playingAnimations =
new ArrayList();
258 protected ArrayList spriteDrawOrder =
new ArrayList();
288 Texture t = material.GetTexture(
"_MainTex");
291 return new Vector2(xy.x / ((
float)t.width), xy.y / ((
float)t.height));
293 return new Vector2(1.0f,1.0f);
306 return PixelSpaceToUVSpace(
new Vector2((
float)x, (
float)y));
316 Vector2 p = PixelSpaceToUVSpace(xy);
328 return PixelCoordToUVCoord(
new Vector2((
float)x, (
float)y));
337 gameObject.AddComponent<MeshFilter>();
338 gameObject.AddComponent<MeshRenderer>();
340 meshFilter = (MeshFilter)GetComponent(typeof(MeshFilter));
341 meshRenderer = (MeshRenderer)GetComponent(typeof(MeshRenderer));
343 meshRenderer.GetComponent<Renderer>().material = material;
344 mesh = meshFilter.mesh;
347 EnlargeArrays(allocBlockSize);
351 transform.position = Vector3.zero;
352 transform.rotation = Quaternion.identity;
359 sprites[0] =
new Sprite();
360 vertices =
new Vector3[4];
361 UVs =
new Vector2[4];
362 colors =
new Color[4];
363 triIndices =
new int[6];
384 firstNewElement = sprites.Length;
387 Sprite[] tempSprites = sprites;
388 sprites =
new Sprite[sprites.Length + count];
389 tempSprites.CopyTo(sprites, 0);
392 Vector3[] tempVerts = vertices;
393 vertices =
new Vector3[vertices.Length + count*4];
394 tempVerts.CopyTo(vertices, 0);
397 Vector2[] tempUVs = UVs;
398 UVs =
new Vector2[UVs.Length + count*4];
399 tempUVs.CopyTo(UVs, 0);
402 Color[] tempColors = colors;
403 colors =
new Color[colors.Length + count * 4];
404 tempColors.CopyTo(colors, 0);
407 int[] tempTris = triIndices;
408 triIndices =
new int[triIndices.Length + count*6];
409 tempTris.CopyTo(triIndices, 0);
412 for (
int i = 0; i < firstNewElement; ++i)
419 for (
int i = firstNewElement; i < sprites.Length; ++i)
423 sprites[i] =
new Sprite();
424 sprites[i].
index = i;
430 sprites[i].
mv1 = i * 4 + 0;
431 sprites[i].
mv2 = i * 4 + 1;
432 sprites[i].
mv3 = i * 4 + 2;
433 sprites[i].
mv4 = i * 4 + 3;
436 sprites[i].
uv1 = i * 4 + 0;
437 sprites[i].
uv2 = i * 4 + 1;
438 sprites[i].
uv3 = i * 4 + 2;
439 sprites[i].
uv4 = i * 4 + 3;
442 sprites[i].
cv1 = i * 4 + 0;
443 sprites[i].
cv2 = i * 4 + 1;
444 sprites[i].
cv3 = i * 4 + 2;
445 sprites[i].
cv4 = i * 4 + 3;
451 availableBlocks.Add(sprites[i]);
456 triIndices[i * 6 + 0] = i * 4 + 0;
457 triIndices[i * 6 + 1] = i * 4 + 1;
458 triIndices[i * 6 + 2] = i * 4 + 3;
460 triIndices[i * 6 + 3] = i * 4 + 3;
461 triIndices[i * 6 + 4] = i * 4 + 1;
462 triIndices[i * 6 + 5] = i * 4 + 2;
466 triIndices[i * 6 + 0] = i * 4 + 0;
467 triIndices[i * 6 + 1] = i * 4 + 3;
468 triIndices[i * 6 + 2] = i * 4 + 1;
470 triIndices[i * 6 + 3] = i * 4 + 3;
471 triIndices[i * 6 + 4] = i * 4 + 2;
472 triIndices[i * 6 + 5] = i * 4 + 1;
476 spriteDrawOrder.Add(sprites[i]);
481 colorsChanged =
true;
482 vertCountChanged =
true;
484 return firstNewElement;
492 public Sprite AddSprite(GameObject client,
float width,
float height,
int leftPixelX,
int bottomPixelY,
int pixelWidth,
int pixelHeight,
bool billboarded)
494 return AddSprite(client, width, height, PixelCoordToUVCoord(leftPixelX, bottomPixelY), PixelSpaceToUVSpace(pixelWidth, pixelHeight), billboarded);
502 public Sprite AddSprite(GameObject client,
float width,
float height, Vector2 lowerLeftUV, Vector2 UVDimensions,
bool billboarded)
507 if (availableBlocks.Count < 1)
508 EnlargeArrays(allocBlockSize);
511 spriteIndex = ((
Sprite)availableBlocks[0]).index;
512 availableBlocks.RemoveAt(0);
515 Sprite newSprite = sprites[spriteIndex];
516 newSprite.
client = client;
540 activeBillboards.Add(newSprite);
543 activeBlocks.Add(newSprite);
549 UVs[newSprite.
uv1] = lowerLeftUV + Vector2.up * UVDimensions.y;
550 UVs[newSprite.
uv2] = lowerLeftUV;
551 UVs[newSprite.
uv3] = lowerLeftUV + Vector2.right * UVDimensions.x;
552 UVs[newSprite.
uv4] = lowerLeftUV + UVDimensions;
565 activeBlocks.Remove(sprite);
566 activeBillboards.Add(sprite);
572 sprite.
v1 = Vector3.zero;
573 sprite.
v2 = Vector3.zero;
574 sprite.
v3 = Vector3.zero;
575 sprite.
v4 = Vector3.zero;
577 vertices[sprite.
mv1] = sprite.
v1;
578 vertices[sprite.
mv2] = sprite.
v2;
579 vertices[sprite.
mv3] = sprite.
v3;
580 vertices[sprite.
mv4] = sprite.
v4;
586 activeBillboards.Remove(sprite);
588 activeBlocks.Remove(sprite);
593 availableBlocks.Add(sprite);
604 activeBillboards.Remove(sprite);
606 activeBlocks.Remove(sprite);
610 vertices[sprite.
mv1] = Vector3.zero;
611 vertices[sprite.
mv2] = Vector3.zero;
612 vertices[sprite.
mv3] = Vector3.zero;
613 vertices[sprite.
mv4] = Vector3.zero;
633 activeBillboards.Add(sprite);
635 activeBlocks.Add(sprite);
643 int[] indices =
new int[6];
644 int offset = spriteDrawOrder.IndexOf(s) * 6;
650 indices[0] = triIndices[offset];
651 indices[1] = triIndices[offset + 1];
652 indices[2] = triIndices[offset + 2];
653 indices[3] = triIndices[offset + 3];
654 indices[4] = triIndices[offset + 4];
655 indices[5] = triIndices[offset + 5];
658 for (
int i = offset; i < triIndices.Length - 6; i += 6)
660 triIndices[i] = triIndices[i+6];
661 triIndices[i+1] = triIndices[i+7];
662 triIndices[i+2] = triIndices[i+8];
663 triIndices[i+3] = triIndices[i+9];
664 triIndices[i+4] = triIndices[i+10];
665 triIndices[i+5] = triIndices[i+11];
667 spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 + 1];
671 triIndices[triIndices.Length - 6] = indices[0];
672 triIndices[triIndices.Length - 5] = indices[1];
673 triIndices[triIndices.Length - 4] = indices[2];
674 triIndices[triIndices.Length - 3] = indices[3];
675 triIndices[triIndices.Length - 2] = indices[4];
676 triIndices[triIndices.Length - 1] = indices[5];
679 spriteDrawOrder[spriteDrawOrder.Count - 1] = s.
index;
681 vertCountChanged =
true;
687 int[] indices =
new int[6];
688 int offset = spriteDrawOrder.IndexOf(s) * 6;
694 indices[0] = triIndices[offset];
695 indices[1] = triIndices[offset + 1];
696 indices[2] = triIndices[offset + 2];
697 indices[3] = triIndices[offset + 3];
698 indices[4] = triIndices[offset + 4];
699 indices[5] = triIndices[offset + 5];
702 for(
int i=offset; i>5; i-=6)
704 triIndices[i] = triIndices[i-6];
705 triIndices[i+1] = triIndices[i-5];
706 triIndices[i+2] = triIndices[i-4];
707 triIndices[i+3] = triIndices[i-3];
708 triIndices[i+4] = triIndices[i-2];
709 triIndices[i+5] = triIndices[i-1];
711 spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 - 1];
715 triIndices[0] = indices[0];
716 triIndices[1] = indices[1];
717 triIndices[2] = indices[2];
718 triIndices[3] = indices[3];
719 triIndices[4] = indices[4];
720 triIndices[5] = indices[5];
723 spriteDrawOrder[0] = s.
index;
725 vertCountChanged =
true;
733 int[] indices =
new int[6];
734 int offset = spriteDrawOrder.IndexOf(toMove) * 6;
735 int refOffset = spriteDrawOrder.IndexOf(reference) * 6;
741 if(offset > refOffset)
745 indices[0] = triIndices[offset];
746 indices[1] = triIndices[offset + 1];
747 indices[2] = triIndices[offset + 2];
748 indices[3] = triIndices[offset + 3];
749 indices[4] = triIndices[offset + 4];
750 indices[5] = triIndices[offset + 5];
753 for (
int i = offset; i < refOffset; i += 6)
755 triIndices[i] = triIndices[i+6];
756 triIndices[i+1] = triIndices[i+7];
757 triIndices[i+2] = triIndices[i+8];
758 triIndices[i+3] = triIndices[i+9];
759 triIndices[i+4] = triIndices[i+10];
760 triIndices[i+5] = triIndices[i+11];
762 spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 + 1];
766 triIndices[refOffset] = indices[0];
767 triIndices[refOffset+1] = indices[1];
768 triIndices[refOffset+2] = indices[2];
769 triIndices[refOffset+3] = indices[3];
770 triIndices[refOffset+4] = indices[4];
771 triIndices[refOffset+5] = indices[5];
774 spriteDrawOrder[refOffset/6] = toMove.
index;
776 vertCountChanged =
true;
784 int[] indices =
new int[6];
785 int offset = spriteDrawOrder.IndexOf(toMove) * 6;
786 int refOffset = spriteDrawOrder.IndexOf(reference) * 6;
792 if(offset < refOffset)
796 indices[0] = triIndices[offset];
797 indices[1] = triIndices[offset + 1];
798 indices[2] = triIndices[offset + 2];
799 indices[3] = triIndices[offset + 3];
800 indices[4] = triIndices[offset + 4];
801 indices[5] = triIndices[offset + 5];
804 for (
int i = offset; i > refOffset; i -= 6)
806 triIndices[i] = triIndices[i-6];
807 triIndices[i+1] = triIndices[i-5];
808 triIndices[i+2] = triIndices[i-4];
809 triIndices[i+3] = triIndices[i-3];
810 triIndices[i+4] = triIndices[i-2];
811 triIndices[i+5] = triIndices[i-1];
813 spriteDrawOrder[i / 6] = spriteDrawOrder[i / 6 - 1];
817 triIndices[refOffset] = indices[0];
818 triIndices[refOffset+1] = indices[1];
819 triIndices[refOffset+2] = indices[2];
820 triIndices[refOffset+3] = indices[3];
821 triIndices[refOffset+4] = indices[4];
822 triIndices[refOffset+5] = indices[5];
825 spriteDrawOrder[refOffset/6] = toMove.
index;
827 vertCountChanged =
true;
835 spriteDrawOrder.Sort(drawOrderComparer);
840 for (
int i = 0; i < spriteDrawOrder.Count; ++i)
842 s = (
Sprite) spriteDrawOrder[i];
845 triIndices[i * 6 + 0] = s.
mv1;
846 triIndices[i * 6 + 1] = s.
mv2;
847 triIndices[i * 6 + 2] = s.
mv4;
849 triIndices[i * 6 + 3] = s.
mv4;
850 triIndices[i * 6 + 4] = s.
mv2;
851 triIndices[i * 6 + 5] = s.
mv3;
856 for (
int i = 0; i < spriteDrawOrder.Count; ++i)
858 s = (
Sprite)spriteDrawOrder[i];
861 triIndices[i * 6 + 0] = s.
mv1;
862 triIndices[i * 6 + 1] = s.
mv2;
863 triIndices[i * 6 + 2] = s.
mv4;
865 triIndices[i * 6 + 3] = s.
mv4;
866 triIndices[i * 6 + 4] = s.
mv2;
867 triIndices[i * 6 + 5] = s.
mv3;
871 vertCountChanged =
true;
877 playingAnimations.Add(s);
882 playingAnimations.Remove(s);
887 if (i < sprites.Length)
907 Transform t = Camera.main.transform;
909 vertices[sprite.
mv1] = pos + t.TransformDirection(sprite.
v1);
910 vertices[sprite.
mv2] = pos + t.TransformDirection(sprite.
v2);
911 vertices[sprite.
mv3] = pos + t.TransformDirection(sprite.
v3);
912 vertices[sprite.
mv4] = pos + t.TransformDirection(sprite.
v4);
945 colorsChanged =
true;
958 boundUpdateInterval = seconds;
959 InvokeRepeating(
"UpdateBounds", seconds, seconds);
965 CancelInvoke(
"UpdateBounds");
978 if(playingAnimations.Count > 0)
980 animTimeElapsed = Time.deltaTime;
982 for(i=0; i<playingAnimations.Count; ++i)
984 tempSprite = (
Sprite)playingAnimations[i];
988 if (!tempSprite.
StepAnim(animTimeElapsed))
989 playingAnimations.Remove(tempSprite);
996 if (vertCountChanged)
998 vertCountChanged =
false;
999 colorsChanged =
false;
1000 vertsChanged =
false;
1002 updateBounds =
false;
1005 mesh.vertices = vertices;
1007 mesh.colors = colors;
1009 mesh.triangles = triIndices;
1015 vertsChanged =
false;
1017 if (autoUpdateBounds)
1018 updateBounds =
true;
1020 mesh.vertices = vertices;
1025 mesh.RecalculateBounds();
1026 updateBounds =
false;
1031 colorsChanged =
false;
1033 mesh.colors = colors;
Vector2 PixelSpaceToUVSpace(Vector2 xy)
void UpdateColors(Sprite sprite)
void AnimateSprite(Sprite s)
void Transform(Sprite sprite)
Vector2 PixelCoordToUVCoord(Vector2 xy)
Sprite AddSprite(GameObject client, float width, float height, int leftPixelX, int bottomPixelY, int pixelWidth, int pixelHeight, bool billboarded)
bool StepAnim(float time)
void SetSizeXZ(float width, float height)
bool m_hidden___DoNotAccessExternally
void MoveBehind(Sprite toMove, Sprite reference)
void TransformBillboarded(Sprite sprite)
void MoveInfrontOf(Sprite toMove, Sprite reference)
Vector2[] BuildUVAnim(Vector2 start, Vector2 cellSize, int cols, int rows, int totalCells, float fps)
Vector2 PixelSpaceToUVSpace(int x, int y)
void StopAnimation(Sprite s)
void ScheduleBoundsUpdate(float seconds)
void MoveToFront(Sprite s)
Vector2 PixelCoordToUVCoord(int x, int y)
void MoveToBack(Sprite s)
virtual void LateUpdate()
float boundUpdateInterval
void ShowSprite(Sprite sprite)
void HideSprite(Sprite sprite)
Transform clientTransform
void SetBuffers(Vector3[] v, Vector2[] uv)
void SetSizeXY(float width, float height)
void SetAnim(Vector2[] anim)
void AppendAnim(Vector2[] anim)
MeshRenderer meshRenderer
void UpdateUV(Sprite sprite)
void SetSizeYZ(float width, float height)
Sprite AddSprite(GameObject client, float width, float height, Vector2 lowerLeftUV, Vector2 UVDimensions, bool billboarded)
void SetBillboarded(Sprite sprite)
bool GetNextFrame(ref Vector2 uv)
int EnlargeArrays(int count)
void RemoveSprite(Sprite sprite)
void CancelBoundsUpdate()