Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. namespace Agens.Editor
  5. {
  6.  
  7. public static class MoveComponentContext
  8. {
  9. internal enum Destination
  10. {
  11. Top,
  12. Bottom
  13. }
  14.  
  15. private const string ComponentArrayName = "m_Component";
  16. private const int FirstComponentIndex = 1;
  17.  
  18.  
  19. [MenuItem("CONTEXT/Component/Move to Top")]
  20. public static void Top(MenuCommand command)
  21. {
  22. Move(command.context, Destination.Top);
  23. }
  24.  
  25.  
  26. [MenuItem("CONTEXT/Component/Move to Bottom")]
  27. public static void Bottom(MenuCommand command)
  28. {
  29. Move(command.context, Destination.Bottom);
  30. }
  31.  
  32.  
  33. private static void Move(Object target, Destination destination)
  34. {
  35. var component = target as Component;
  36. if (component == null)
  37. {
  38. Debug.LogWarning("Cant move " + target + " to " + destination, target);
  39. return;
  40. }
  41. var serializedObject = new SerializedObject(component.gameObject);
  42. var componentArray = serializedObject.FindProperty(ComponentArrayName);
  43.  
  44. var lastComponentIndex = componentArray.arraySize - 1;
  45. var targetIndex = destination == Destination.Top ? FirstComponentIndex : lastComponentIndex;
  46.  
  47. for (int index = FirstComponentIndex; index <= lastComponentIndex; ++index)
  48. {
  49. var iterator = componentArray.GetArrayElementAtIndex(index);
  50. iterator.Next(true);
  51. iterator.Next(true);
  52.  
  53. if (iterator.objectReferenceValue == target)
  54. {
  55. componentArray.MoveArrayElement(index, targetIndex);
  56. serializedObject.ApplyModifiedProperties();
  57.  
  58. break;
  59. }
  60. }
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement