Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using System;
  5.  
  6. namespace Incendiary.Core.Character
  7. {
  8.     [CustomEditor(typeof(CharacterDatabase))]
  9.     public class CharacterDatabaseEditor : Editor
  10.     {
  11.         private SerializedProperty m_property;
  12.         private ReorderableList m_list;
  13.  
  14.         private void OnEnable()
  15.         {
  16.  
  17.             m_property = serializedObject.FindProperty("ScriptableCharacter");
  18.             m_list = new ReorderableList(serializedObject, m_property, true, true, true, true)
  19.             {
  20.                 drawHeaderCallback = DrawListHeader,
  21.                 drawElementCallback = DrawListElement,
  22.                 elementHeightCallback = index => ElementHeightCallback(m_property, index),
  23.  
  24.                 onAddCallback = (ReorderableList l) =>
  25.                 {
  26.                     var index = l.serializedProperty.arraySize;
  27.                     l.serializedProperty.arraySize++;
  28.                     l.index = index;
  29.                     ScriptableCharacter asset = CreateInstance<ScriptableCharacter>();
  30.                     AssetDatabase.CreateAsset(asset, "Assets/ScriptableCharacter/newCharacter" + (index + 1) + ".asset");
  31.                     AssetDatabase.SaveAssets();
  32.                     var item = m_list.serializedProperty.GetArrayElementAtIndex(index);
  33.                     item.objectReferenceValue = asset;
  34.                     item.isExpanded = true;
  35.                 },
  36.                 onRemoveCallback = (ReorderableList l) =>
  37.                 {
  38.                     ScriptableCharacter t = (ScriptableCharacter)m_property.GetArrayElementAtIndex(l.index).objectReferenceValue;
  39.  
  40.                     ReorderableList.defaultBehaviours.DoRemoveButton(l);
  41.                     if (t == null) return;
  42.  
  43.                     if (EditorUtility.DisplayDialog("Delete Warning", "Delete the Asset " + t.name + " to ?", "Yes", "No"))
  44.                     {
  45.                         DestroyImmediate(t, true);
  46.                     }
  47.                     l.serializedProperty.arraySize--;
  48.                 }
  49.  
  50.             };
  51.         }
  52.  
  53.         private void DrawListHeader(Rect rect)
  54.         {
  55.             GUI.Label(rect, "Character Database");
  56.         }
  57.         float ElementHeightCallback(SerializedProperty property, int index)
  58.         {
  59.             var item = m_property.GetArrayElementAtIndex(index);
  60.  
  61.             return m_list.elementHeight = !item.isExpanded ? EditorGUIUtility.singleLineHeight : EditorGUIUtility.singleLineHeight * 5;
  62.         }
  63.         private void DrawListElement(Rect rect, int index, bool isActive, bool isFocused)
  64.         {
  65.             var item = m_property.GetArrayElementAtIndex(index);
  66.  
  67.             //Scriptable Object Field - Slot
  68.             EditorGUI.PropertyField(new Rect(rect.x + 25, rect.y, EditorGUIUtility.labelWidth * 1.5f, EditorGUIUtility.singleLineHeight), item, GUIContent.none);
  69.  
  70.             if (item.objectReferenceValue == null)
  71.             {
  72.                 item.isExpanded = false;
  73.                 return;
  74.             }
  75.  
  76.             //FoldOut Button
  77.             item.isExpanded = EditorGUI.Foldout(new Rect(rect.x + 15, rect.y + 2, rect.width, rect.height), item.isExpanded, GUIContent.none, false);
  78.  
  79.  
  80.             if (item.isExpanded)
  81.             {
  82.  
  83.                 SerializedObject target = new SerializedObject(item.objectReferenceValue);
  84.                 rect.y += 3;
  85.  
  86.  
  87.                 EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight, rect.width, EditorGUIUtility.singleLineHeight),
  88.                  target.FindProperty("StringTest"), new GUIContent("Test"));
  89.                 rect.y += 3;
  90.                 EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight * 2, rect.width, EditorGUIUtility.singleLineHeight),
  91.                  target.FindProperty("Health"), new GUIContent("Health"));
  92.                 rect.y += 3;
  93.                 EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight * 3, rect.width, EditorGUIUtility.singleLineHeight),
  94.                  target.FindProperty("smthg"), new GUIContent("Sprite"));
  95.  
  96.             }
  97.  
  98.         }
  99.  
  100.         public override void OnInspectorGUI()
  101.         {
  102.             serializedObject.Update();
  103.             EditorGUILayout.Space();
  104.             m_list.DoLayoutList();
  105.             serializedObject.ApplyModifiedProperties();
  106.  
  107.         }
  108.  
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement