Pro_Unit

MoveArrayElementEditor

Jan 7th, 2020
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1. using UnityEditor;
  2. using UnityEngine;
  3. using Object = UnityEngine.Object;
  4.  
  5. namespace AlienCore
  6. {
  7.     public static class MoveArrayElementEditor
  8.     {
  9.         [InitializeOnLoadMethod]
  10.         static void Start ()
  11.         {
  12.             // For more information visit: https://docs.unity3d.com/ScriptReference/EditorApplication-contextualPropertyMenu.html
  13.             EditorApplication.contextualPropertyMenu += OnPropertyContextMenu;
  14.         }
  15.  
  16.         static void OnPropertyContextMenu (GenericMenu menu, SerializedProperty property)
  17.         {
  18.             if (property == null) return;
  19.  
  20.             // Path of an array is "PropertyName.Array.data[arraylength]", so let's look if the property is an array
  21.             string propertyPath = property.propertyPath;
  22.             if (!propertyPath.Contains (".Array.data[") || !propertyPath.EndsWith ("]")) return;
  23.  
  24.             // Split the property path and find out the array length
  25.             string[] fullPathSplit = propertyPath.Split ('.');
  26.             string ending = fullPathSplit [fullPathSplit.Length - 1];
  27.             int index = 0;
  28.             if (!int.TryParse (ending.Replace ("data[", "").Replace ("]", ""), out index)) return;
  29.  
  30.             // Rebuild the path without the ".Array.data[arraylength]" stuff
  31.             string pathToArray = string.Empty;
  32.             for (int i = 0; i < fullPathSplit.Length - 2; i++)
  33.             {
  34.                 if (i < fullPathSplit.Length - 3)
  35.                 {
  36.                     pathToArray = string.Concat (pathToArray, fullPathSplit [i], ".");
  37.                 }
  38.                 else
  39.                 {
  40.                     pathToArray = string.Concat (pathToArray, fullPathSplit [i]);
  41.                 }
  42.             }
  43.  
  44.             // Get the serialized target object and the property of the array with the path
  45.             Object targetObject = property.serializedObject.targetObject;
  46.             SerializedObject serializedTargetObject = new SerializedObject (targetObject);
  47.  
  48.             SerializedProperty serializedArray = serializedTargetObject.FindProperty (pathToArray);
  49.             int arrayLength = serializedArray.arraySize;
  50.  
  51.             if (serializedArray == null) return;
  52.  
  53.             // Show context menu entry only if the user can move it up or down
  54.             if (index > 0)
  55.             {
  56.                 menu.AddItem (new GUIContent ("Move Array Element Down"), false,
  57.                               () => MoveDown (serializedTargetObject, serializedArray, index));
  58.             }
  59.  
  60.             if (index < arrayLength - 1)
  61.             {
  62.                 menu.AddItem (new GUIContent ("Move Array Element Up"), false,
  63.                               () => MoveUp (serializedTargetObject, serializedArray, index));
  64.             }
  65.         }
  66.  
  67.         static public void MoveDown (SerializedObject target, SerializedProperty array, int index)
  68.         {
  69.             array.MoveArrayElement (index, index - 1);
  70.             target.ApplyModifiedProperties ();
  71.         }
  72.  
  73.         static public void MoveUp (SerializedObject target, SerializedProperty array, int index)
  74.         {
  75.             array.MoveArrayElement (index, index + 1);
  76.             target.ApplyModifiedProperties ();
  77.         }
  78.     }
  79. }
Add Comment
Please, Sign In to add comment