Advertisement
vexe

SerializedProperty extension methods

Dec 27th, 2013
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.39 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3.  
  4. public static class SerializedPropertyExtensions
  5. {
  6.     public static void RemoveNullObjects(this SerializedProperty prop)
  7.     {
  8.         for (int i = 0; i < prop.arraySize; i++) {
  9.             if (prop.GetAt(i).GetValue() == null) {
  10.                 prop.RemoveAt(i);
  11.                 i--;
  12.             }
  13.         }
  14.     }
  15.     public static bool Contains(this SerializedProperty prop, System.Object value)
  16.     {
  17.         for (int i = 0, size = prop.arraySize; i < size; i++) {
  18.             if (prop.GetAt(i).GetValue() == value)
  19.                 return true;
  20.         }
  21.         return false;
  22.     }
  23.     public static void MoveUp(this SerializedProperty prop, int fromIndex)
  24.     {
  25.         AssertArray(prop);
  26.         AssertNotEmpty(prop);
  27.         int previous = fromIndex - 1;
  28.         if (previous < 0) previous = prop.arraySize - 1;
  29.         prop.Swap(fromIndex, previous);
  30.     }
  31.     public static void MoveDown(this SerializedProperty prop, int fromIndex)
  32.     {
  33.         AssertArray(prop);
  34.         AssertNotEmpty(prop);
  35.         int next = (fromIndex + 1) % prop.arraySize;
  36.         prop.Swap(fromIndex, next);
  37.     }
  38.     public static void Swap(this SerializedProperty prop, int i, int j)
  39.     {
  40.         AssertArray(prop);
  41.         AssertNotEmpty(prop);
  42.         var value1 = prop.GetObjectValueAt(i);
  43.         var value2 = prop.GetObjectValueAt(j);
  44.         System.Object temp = value1;
  45.         prop.SetObjectValueAt(i, value2);
  46.         prop.SetObjectValueAt(j, temp);
  47.     }
  48.     public static bool ContainsReferenceTypes(this SerializedProperty prop)
  49.     {
  50.         AssertArray(prop);
  51.         AssertNotEmpty(prop);
  52.         return prop.GetFirst().IsReferenceType();
  53.     }
  54.     public static bool IsReferenceType(this SerializedProperty prop)
  55.     {
  56.         return prop.propertyType == SerializedPropertyType.ObjectReference;
  57.     }
  58.     public static void Add(this SerializedProperty prop, UnityEngine.Object value)
  59.     {
  60.         AssertArray(prop);
  61.         prop.arraySize++;
  62.         prop.GetAt(prop.arraySize - 1).objectReferenceValue = value;
  63.     }
  64.     public static void Add(this SerializedProperty prop)
  65.     {
  66.         AssertArray(prop);
  67.         prop.arraySize++;
  68.         prop.GetLast().SetToDefault();
  69.     }
  70.     public static void SetToDefault(this SerializedProperty prop)
  71.     {
  72.         switch (prop.propertyType) {
  73.             case SerializedPropertyType.Integer:
  74.                 prop.intValue = default(int);
  75.                 break;
  76.             case SerializedPropertyType.Float:
  77.                 prop.floatValue = default(float);
  78.                 break;
  79.             case SerializedPropertyType.Boolean:
  80.                 prop.boolValue = default(bool);
  81.                 break;
  82.             case SerializedPropertyType.Color:
  83.                 prop.colorValue = default(Color);
  84.                 break;
  85.             case SerializedPropertyType.Bounds:
  86.                 prop.boundsValue = default(Bounds);
  87.                 break;
  88.             case SerializedPropertyType.Rect:
  89.                 prop.rectValue = default(Rect);
  90.                 break;
  91.             case SerializedPropertyType.Vector2:
  92.                 prop.vector2Value = default(Vector2);
  93.                 break;
  94.             case SerializedPropertyType.Vector3:
  95.                 prop.vector3Value = default(Vector3);
  96.                 break;
  97.             case SerializedPropertyType.ObjectReference:
  98.                 prop.objectReferenceValue = null;
  99.                 break;
  100.         }
  101.     }
  102.     public static SerializedProperty GetLast(this SerializedProperty prop)
  103.     {
  104.         AssertArray(prop);
  105.         return prop.GetAt(prop.arraySize - 1);
  106.     }
  107.     public static SerializedProperty GetFirst(this SerializedProperty prop)
  108.     {
  109.         AssertArray(prop);
  110.         return prop.GetAt(0);
  111.     }
  112.     public static void AssertArray(this SerializedProperty prop)
  113.     {
  114.         if (!prop.isArray)
  115.             throw new UnityException("SerializedProperty `" + prop.name + "` is not an array. Yet you're trying to index it!");
  116.     }
  117.     public static void RemoveAt(this SerializedProperty prop, int atIndex)
  118.     {
  119.         AssertArray(prop);
  120.         AssertNotEmpty(prop);
  121.  
  122.         for (int i = atIndex, size = prop.arraySize; i < size - 1; i++) {
  123.             prop.SetObjectValueAt(i, prop.GetObjectValueAt(i + 1));
  124.         }
  125.         prop.arraySize--;
  126.     }
  127.     public static void AssertNotEmpty(this SerializedProperty prop)
  128.     {
  129.         if (prop.arraySize <= 0)
  130.             throw new UnityException("Array `" + prop.name + "` is empty. You can't do anything with it!");
  131.     }
  132.     public static System.Object GetObjectValueAt(this SerializedProperty prop, int i)
  133.     {
  134.         //AssertArray(prop);
  135.         //AssertNotEmpty(prop);
  136.         return prop.GetAt(i).GetValue();
  137.     }
  138.     public static void SetObjectValueAt(this SerializedProperty prop, int i, System.Object toValue)
  139.     {
  140.         AssertArray(prop);
  141.         AssertNotEmpty(prop);
  142.         prop.GetAt(i).SetObjectValue(toValue);
  143.     }
  144.     public static void SetObjectValue(this SerializedProperty prop, System.Object toValue)
  145.     {
  146.         switch (prop.propertyType) {
  147.             case SerializedPropertyType.Boolean:
  148.                 prop.boolValue = (bool)toValue;
  149.                 break;
  150.             case SerializedPropertyType.Bounds:
  151.                 prop.boundsValue = (Bounds)toValue;
  152.                 break;
  153.             case SerializedPropertyType.Color:
  154.                 prop.colorValue = (Color)toValue;
  155.                 break;
  156.             case SerializedPropertyType.Float:
  157.                 prop.floatValue = (float)toValue;
  158.                 break;
  159.             case SerializedPropertyType.Integer:
  160.                 prop.intValue = (int)toValue;
  161.                 break;
  162.             case SerializedPropertyType.ObjectReference:
  163.                 prop.objectReferenceValue = toValue as UnityEngine.Object;
  164.                 break;
  165.             case SerializedPropertyType.Rect:
  166.                 prop.rectValue = (Rect)toValue;
  167.                 break;
  168.             case SerializedPropertyType.String:
  169.                 prop.stringValue = (string)toValue;
  170.                 break;
  171.             case SerializedPropertyType.Vector2:
  172.                 prop.vector2Value = (Vector2)toValue;
  173.                 break;
  174.             case SerializedPropertyType.Vector3:
  175.                 prop.vector3Value = (Vector3)toValue;
  176.                 break;
  177.         }
  178.     }
  179.     public static System.Object GetValue(this SerializedProperty prop)
  180.     {
  181.         switch (prop.propertyType) {
  182.             case SerializedPropertyType.Boolean:
  183.                 return prop.boolValue;
  184.                 break;
  185.             case SerializedPropertyType.Bounds:
  186.                 return prop.boundsValue;
  187.                 break;
  188.             case SerializedPropertyType.Color:
  189.                 return prop.colorValue;
  190.                 break;
  191.             case SerializedPropertyType.Float:
  192.                 return prop.floatValue;
  193.                 break;
  194.             case SerializedPropertyType.Integer:
  195.                 return prop.intValue;
  196.                 break;
  197.             case SerializedPropertyType.ObjectReference:
  198.                 return prop.objectReferenceValue;
  199.                 break;
  200.             case SerializedPropertyType.Rect:
  201.                 return prop.rectValue;
  202.                 break;
  203.             case SerializedPropertyType.String:
  204.                 return prop.stringValue;
  205.                 break;
  206.             case SerializedPropertyType.Vector2:
  207.                 return prop.vector2Value;
  208.                 break;
  209.             case SerializedPropertyType.Vector3:
  210.                 return prop.vector3Value;
  211.             default: return null;
  212.         }
  213.     }
  214.     public static T GetValue<T>(this SerializedProperty prop)
  215.     {
  216.         return (T)prop.GetValue();
  217.     }
  218.     public static SerializedProperty GetAt(this SerializedProperty prop, int i)
  219.     {
  220.         //AssertArray(prop);
  221.         return prop.GetArrayElementAtIndex(i);
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement