Neko250

SpriteGroups.js

Dec 10th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // SpriteGroups.js was created by Not Quite Black and White for the upcoming point and click adventure This is No Time for Games
  2. // but the script can be used in your Unity projects, license free.
  3. // If you want to find out more about This is No Time for Games please visit ThisisNoTimeforGames.com
  4.  
  5. // This script is for editing the colour, sorting layer and sorting order of a group of sprite renderers simultaneously.
  6. // Multiple groups of sprite renderers can be contained within the variable "spriteGroups" and edited separately in the editor.
  7. // To allow the sprite groups to be edited at runtime too, just delete the if (!(Application.isPlaying)) on lines 38, 25 and 24.
  8.  
  9. // For instructions on use please attach this to a game object and see the tooltips in the inspector.
  10.  
  11. #pragma strict
  12.  
  13. import System.Collections.Generic;
  14.  
  15. @script ExecuteInEditMode()
  16. public class SpriteGroups extends MonoBehaviour
  17. {
  18.     @Tooltip ("To create a new group of sprites that can be edited simultaneously, click the arrow and type the number of groups of sprites you want to create into the field.")
  19.     public var spriteGroups : List.<SpriteGroupInfo>;
  20.  
  21.     #if UNITY_EDITOR
  22.     public function Update()
  23.     {
  24.         if (!(Application.isPlaying))
  25.         {
  26.             if (spriteGroups != null)
  27.             {
  28.                 for (var i=0; i < spriteGroups.Count; i++)
  29.                 {
  30.                     for (var j=0; j < spriteGroups[i].spriteRendererList.Count; j++)
  31.                     {
  32.                         if (spriteGroups[i].updateColour) spriteGroups[i].spriteRendererList[j].color = spriteGroups[i].colour;
  33.                         if (spriteGroups[i].updateSortingLayer) spriteGroups[i].spriteRendererList[j].sortingLayerName = spriteGroups[i].sortingLayer;
  34.                         if (spriteGroups[i].updateSortingOrder) spriteGroups[i].spriteRendererList[j].sortingOrder = spriteGroups[i].sortingOrder;
  35.                     }
  36.                 }
  37.             }
  38.         }
  39.     }
  40.     #endif
  41.  
  42.     // Set colour at runtime e.g. SetColour(0, Color.black);
  43.     public function SetColour(spriteGroupsIndex : int, colour : Color)
  44.     {
  45.         if (spriteGroups != null)
  46.         {
  47.             if (spriteGroups.Count >= spriteGroupsIndex + 1)
  48.             {
  49.                 for (var i=0; i < spriteGroups[spriteGroupsIndex].spriteRendererList.Count; i++)
  50.                 {
  51.                     spriteGroups[spriteGroupsIndex].spriteRendererList[i].color = colour;
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.     // Set sorting layer at runtime e.g SetSortingLayer(0, "Default");
  58.     public function SetSortingLayer(spriteGroupsIndex : int, sortingLayer : String)
  59.     {
  60.         if (spriteGroups != null)
  61.         {
  62.             if (spriteGroups.Count >= spriteGroupsIndex + 1)
  63.             {
  64.                 for (var i=0; i < spriteGroups[spriteGroupsIndex].spriteRendererList.Count; i++)
  65.                 {
  66.                     spriteGroups[spriteGroupsIndex].spriteRendererList[i].sortingLayerName = sortingLayer;
  67.                 }
  68.             }
  69.         }
  70.     }
  71.  
  72.     // Set sorting order at runtime e.g SetSortingOrder(0, 10);
  73.     public function SetSortingOrder(spriteGroupsIndex : int, sortingOrder : int)
  74.     {
  75.         if (spriteGroups != null)
  76.         {
  77.             if (spriteGroups.Count >= spriteGroupsIndex + 1)
  78.             {
  79.                 for (var i=0; i < spriteGroups[spriteGroupsIndex].spriteRendererList.Count; i++)
  80.                 {
  81.                     spriteGroups[spriteGroupsIndex].spriteRendererList[i].sortingOrder = sortingOrder;
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. public class SpriteGroupInfo
  89. {
  90.     @Tooltip ("Set the colour for this sprite group.")
  91.     public var colour : Color = Color.white;
  92.     @Tooltip ("Set the sorting layer for this sprite group.")
  93.     public var sortingLayer : String;
  94.     @Tooltip ("Set the sorting order for this sprite group.")
  95.     public var sortingOrder : int;
  96.     @Tooltip ("The sprite group: Drag game objects that have sprite renderers attached from the hierarchy into this list.")
  97.     public var spriteRendererList : List.<SpriteRenderer>;
  98.     @Tooltip ("Do you want to update the colour of this sprite group?")
  99.     public var updateColour : boolean = false;
  100.     @Tooltip ("Do you want to update the sorting layer of this sprite group?")
  101.     public var updateSortingLayer : boolean = false;
  102.     @Tooltip ("Do you want to update the sorting order of this sprite group?")
  103.     public var updateSortingOrder : boolean = false;
  104. }
Add Comment
Please, Sign In to add comment