Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. using UnityEditorInternal;
  9. #endif
  10.  
  11. public class TestScriptWithReorderableList : MonoBehaviour
  12. {
  13. public string standardField;
  14.  
  15. public SimpleStateMachine stateMachine;
  16. }
  17.  
  18. [System.Serializable]
  19. public class SimpleStateMachine
  20. {
  21. [SerializeField]
  22. private int _serialId = -1;
  23.  
  24. public SimpleStateMachineState[] states = new SimpleStateMachineState[0];
  25. }
  26.  
  27. [System.Serializable]
  28. public class SimpleStateMachineState
  29. {
  30. public int id; // use this when saving current state etc.
  31. public string name;
  32.  
  33. public override string ToString()
  34. {
  35. return name;
  36. }
  37. }
  38.  
  39. #if UNITY_EDITOR
  40. [CustomPropertyDrawer(typeof(SimpleStateMachine))]
  41. public class SimpleStateMachineInspector : PropertyDrawer
  42. {
  43. private const float FIELD_PADDING = 2f;
  44.  
  45. private const string LIST_PROPERTY_NAME = "states";
  46. private const string NAME_PROPERTY = "name";
  47. private const string ID_PROPERTY = "id";
  48. private const string SERIAL_ID_PROPERTY = "_serialId";
  49.  
  50. private ReorderableList _stateList;
  51.  
  52. public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
  53. {
  54. var list = GetList(property);
  55. list.DoList(position);
  56. }
  57.  
  58. public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
  59. {
  60. var states = GetList(property);
  61. return states.GetHeight();
  62. }
  63.  
  64. private ReorderableList GetList(SerializedProperty property)
  65. {
  66. if (_stateList == null)
  67. {
  68. var listProperty = property.FindPropertyRelative(LIST_PROPERTY_NAME);
  69. _stateList = new ReorderableList(property.serializedObject, property.FindPropertyRelative(LIST_PROPERTY_NAME));
  70.  
  71. _stateList.drawHeaderCallback = (Rect rect) =>
  72. {
  73. EditorGUI.LabelField(rect, "States");
  74. };
  75.  
  76. _stateList.drawElementCallback =
  77. (Rect rect, int index, bool isActive, bool isFocused) =>
  78. {
  79. rect.y += FIELD_PADDING;
  80.  
  81. string name = listProperty.GetArrayElementAtIndex(index).FindPropertyRelative(NAME_PROPERTY).stringValue;
  82.  
  83. listProperty.GetArrayElementAtIndex(index).FindPropertyRelative(NAME_PROPERTY).stringValue = EditorGUI.TextField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), name);
  84. };
  85.  
  86. _stateList.onAddCallback = (ReorderableList list) =>
  87. {
  88. var states = property.FindPropertyRelative(LIST_PROPERTY_NAME);
  89.  
  90. // increment serial id
  91. int id = property.FindPropertyRelative(SERIAL_ID_PROPERTY).intValue;
  92. id++;
  93. property.FindPropertyRelative(SERIAL_ID_PROPERTY).intValue = id;
  94.  
  95. int length = states.arraySize;
  96. states.InsertArrayElementAtIndex(length);
  97. states.GetArrayElementAtIndex(length).FindPropertyRelative(ID_PROPERTY).intValue = id;
  98. states.GetArrayElementAtIndex(length).FindPropertyRelative(NAME_PROPERTY).stringValue = "<Unnamed State>";
  99. };
  100. }
  101.  
  102. return _stateList;
  103. }
  104. }
  105. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement