Advertisement
Guest User

StaticGraphicsObject

a guest
May 4th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class StaticGraphicsObject : MonoBehaviour
  5. {
  6.     public string GraphicsPath;
  7.     public int GraphicsIndex;
  8.  
  9.     private SpriteRenderer _renderer;
  10.  
  11.     public void OnEnable()
  12.     {
  13.         _renderer = GetComponent<SpriteRenderer>();
  14.  
  15.         GetGraphicsLayerDepth();
  16.  
  17.         UpdateGraphic();
  18.     }
  19.  
  20.     public void GetGraphicsLayerDepth()
  21.     {
  22.         bool gotDepth = false;
  23.  
  24.         GameObject currentGameObject = gameObject;
  25.  
  26.         do
  27.         {
  28.             currentGameObject = currentGameObject.GetParent();
  29.  
  30.             if (currentGameObject == null)
  31.                 break;
  32.  
  33.             if (currentGameObject.tag == "Graphics Layer")
  34.             {
  35.                 GraphicsLayer graphicsLayer = currentGameObject.GetComponent<GraphicsLayer>();
  36.                 gotDepth = true;
  37.                 _renderer.sortingLayerName = string.Format("Graphics Layer " + graphicsLayer.Depth);
  38.             }
  39.         }
  40.         while (!gotDepth);
  41.     }
  42.  
  43.     public void UpdateGraphic()
  44.     {
  45.         // Ensure we have a renderer (Used in the editor)
  46.         if (Application.isEditor)
  47.         {
  48.             _renderer = gameObject.GetComponent<SpriteRenderer>();
  49.             GetGraphicsLayerDepth();
  50.         }
  51.  
  52.         // Get the sprite sheet
  53.         Sprite[] spriteSheet = Resources.LoadAll<Sprite>(GraphicsPath);
  54.  
  55.         // Check for no sprites found
  56.         if (spriteSheet.Length == 0)
  57.             return;
  58.  
  59.         // Wrap around if longer than the number of sprites in the sprite sheet
  60.         if (GraphicsIndex < 0)
  61.             GraphicsIndex = spriteSheet.Length - 1;
  62.         GraphicsIndex %= spriteSheet.Length;
  63.  
  64.         // Update the graphic
  65.         _renderer.sprite = spriteSheet[GraphicsIndex];
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement