duck

duck

Jun 25th, 2010
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1.  
  2.  
  3. public class Sequence {
  4.  
  5.     private static GameObject[] _items;
  6.     public static GameObject[] items {
  7.         get {
  8.             if (_items == null)
  9.             {
  10.                 _items = FindObjectsWithTag("Sequence Item");
  11.                 _items.Sort(new SequenceObjectComparer());
  12.             }
  13.             return _items;
  14.         }
  15.     }
  16. }
  17.  
  18. public class SequenceObjectComparer : IComparer<GameObject>
  19. {
  20.     public int Compare (GameObject a, GameObject b)
  21.     {
  22.         int a_id = ParseNumberAtEnd(a.name);
  23.         int b_id = ParseNumberAtEnd(b.name);
  24.        
  25.         if (a_id > b_id) {
  26.             return 1;
  27.         } else {
  28.             return -1;
  29.         }
  30.     }
  31.    
  32.     public static int ParseNumberAtEnd(string text)
  33.     {
  34.         for (int i = text.Length-1; i >= 0; i--)
  35.         {
  36.             if (!Character.IsDigit(text[i]))
  37.             {
  38.                 if (i == text.Length - 1)
  39.                 {
  40.                     Debug.LogError("No digits at end of GameObject's name: "+text);
  41.                 }
  42.                 string digits = text.Substring(i+1);
  43.                 return int.Parse(digits);
  44.             }
  45.         }
  46.         // Text is entirely made of digits!
  47.         return int.Parse(text);
  48.     }
  49.    
  50. }
Advertisement
Add Comment
Please, Sign In to add comment