Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class StaticGraphicsObject : MonoBehaviour
- {
- public string GraphicsPath;
- public int GraphicsIndex;
- private SpriteRenderer _renderer;
- public void OnEnable()
- {
- _renderer = GetComponent<SpriteRenderer>();
- GetGraphicsLayerDepth();
- UpdateGraphic();
- }
- public void GetGraphicsLayerDepth()
- {
- bool gotDepth = false;
- GameObject currentGameObject = gameObject;
- do
- {
- currentGameObject = currentGameObject.GetParent();
- if (currentGameObject == null)
- break;
- if (currentGameObject.tag == "Graphics Layer")
- {
- GraphicsLayer graphicsLayer = currentGameObject.GetComponent<GraphicsLayer>();
- gotDepth = true;
- _renderer.sortingLayerName = string.Format("Graphics Layer " + graphicsLayer.Depth);
- }
- }
- while (!gotDepth);
- }
- public void UpdateGraphic()
- {
- // Ensure we have a renderer (Used in the editor)
- if (Application.isEditor)
- {
- _renderer = gameObject.GetComponent<SpriteRenderer>();
- GetGraphicsLayerDepth();
- }
- // Get the sprite sheet
- Sprite[] spriteSheet = Resources.LoadAll<Sprite>(GraphicsPath);
- // Check for no sprites found
- if (spriteSheet.Length == 0)
- return;
- // Wrap around if longer than the number of sprites in the sprite sheet
- if (GraphicsIndex < 0)
- GraphicsIndex = spriteSheet.Length - 1;
- GraphicsIndex %= spriteSheet.Length;
- // Update the graphic
- _renderer.sprite = spriteSheet[GraphicsIndex];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement