Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using UnityEditorInternal;
- using System;
- namespace Incendiary.Core.Character
- {
- [CustomEditor(typeof(CharacterDatabase))]
- public class CharacterDatabaseEditor : Editor
- {
- private SerializedProperty m_property;
- private ReorderableList m_list;
- private void OnEnable()
- {
- m_property = serializedObject.FindProperty("ScriptableCharacter");
- m_list = new ReorderableList(serializedObject, m_property, true, true, true, true)
- {
- drawHeaderCallback = DrawListHeader,
- drawElementCallback = DrawListElement,
- elementHeightCallback = index => ElementHeightCallback(m_property, index),
- onAddCallback = (ReorderableList l) =>
- {
- var index = l.serializedProperty.arraySize;
- l.serializedProperty.arraySize++;
- l.index = index;
- ScriptableCharacter asset = CreateInstance<ScriptableCharacter>();
- AssetDatabase.CreateAsset(asset, "Assets/ScriptableCharacter/newCharacter" + (index + 1) + ".asset");
- AssetDatabase.SaveAssets();
- var item = m_list.serializedProperty.GetArrayElementAtIndex(index);
- item.objectReferenceValue = asset;
- item.isExpanded = true;
- },
- onRemoveCallback = (ReorderableList l) =>
- {
- ScriptableCharacter t = (ScriptableCharacter)m_property.GetArrayElementAtIndex(l.index).objectReferenceValue;
- ReorderableList.defaultBehaviours.DoRemoveButton(l);
- if (t == null) return;
- if (EditorUtility.DisplayDialog("Delete Warning", "Delete the Asset " + t.name + " to ?", "Yes", "No"))
- {
- DestroyImmediate(t, true);
- }
- l.serializedProperty.arraySize--;
- }
- };
- }
- private void DrawListHeader(Rect rect)
- {
- GUI.Label(rect, "Character Database");
- }
- float ElementHeightCallback(SerializedProperty property, int index)
- {
- var item = m_property.GetArrayElementAtIndex(index);
- return m_list.elementHeight = !item.isExpanded ? EditorGUIUtility.singleLineHeight : EditorGUIUtility.singleLineHeight * 5;
- }
- private void DrawListElement(Rect rect, int index, bool isActive, bool isFocused)
- {
- var item = m_property.GetArrayElementAtIndex(index);
- //Scriptable Object Field - Slot
- EditorGUI.PropertyField(new Rect(rect.x + 25, rect.y, EditorGUIUtility.labelWidth * 1.5f, EditorGUIUtility.singleLineHeight), item, GUIContent.none);
- if (item.objectReferenceValue == null)
- {
- item.isExpanded = false;
- return;
- }
- //FoldOut Button
- item.isExpanded = EditorGUI.Foldout(new Rect(rect.x + 15, rect.y + 2, rect.width, rect.height), item.isExpanded, GUIContent.none, false);
- if (item.isExpanded)
- {
- SerializedObject target = new SerializedObject(item.objectReferenceValue);
- rect.y += 3;
- EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight, rect.width, EditorGUIUtility.singleLineHeight),
- target.FindProperty("StringTest"), new GUIContent("Test"));
- rect.y += 3;
- EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight * 2, rect.width, EditorGUIUtility.singleLineHeight),
- target.FindProperty("Health"), new GUIContent("Health"));
- rect.y += 3;
- EditorGUI.PropertyField(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight * 3, rect.width, EditorGUIUtility.singleLineHeight),
- target.FindProperty("smthg"), new GUIContent("Sprite"));
- }
- }
- public override void OnInspectorGUI()
- {
- serializedObject.Update();
- EditorGUILayout.Space();
- m_list.DoLayoutList();
- serializedObject.ApplyModifiedProperties();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement