HKI Core
ContainerEditor.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
4 using System;
5 using System.Reflection;
6 using System.Collections.Generic;
7 using HKI.Core.Variables;
8 
9 namespace HKI.Core.Variables.Editor
10 {
14  [CustomEditor(typeof(Container), true)]
15  public class ContainerEditor : UnityEditor.Editor
16  {
17  // Private variables
18  const string containerSPName = "container";
19 
20  HKIVar_ContainerHeader header = null;
21  HKIVar_ContainerReorderableList containerRL = null;
22 
23  // Static Interface functions
24  public static T AddHKIVarToContainer<T>(Container container, string variableName, params object[] parameters) where T : HKIVar
25  {
26  if(container == null)
27  {
28  Debug.LogError("Container null");
29  return null;
30  }
31 
32  HKIVar variable = (HKIVar)ScriptableObject.CreateInstance<T>();
33  variable.name = variableName;
34 
35  AssetDatabase.AddObjectToAsset(variable, container);
36  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(variable));
37 
38  SerializedObject containerSO = new SerializedObject(container);
39  containerSO.Update();
40 
41  SerializedProperty containerSP = containerSO.FindProperty(containerSPName);
42 
43  containerSP.InsertArrayElementAtIndex(containerSP.arraySize);
44  SerializedProperty elementSP = containerSP.GetArrayElementAtIndex(containerSP.arraySize - 1);
45 
46  elementSP.objectReferenceValue = variable;
47 
48  containerSO.ApplyModifiedProperties();
49 
50  if(parameters != null && parameters.Length != 0)
51  {
52  MethodInfo setWithParametersMethode = typeof(T).GetMethod("SetWithParameters", BindingFlags.NonPublic | BindingFlags.Instance);
53  if(setWithParametersMethode != null)
54  setWithParametersMethode.Invoke(variable, new object[] { parameters });
55  }
56 
57  return (T)variable;
58  }
59 
60 
61  // OnEnable function
62  void OnEnable()
63  {
64  // Get ConfigSystem
65  header = new HKIVar_ContainerHeader(serializedObject, target);
66  containerRL = new HKIVar_ContainerReorderableList(serializedObject, Repaint, target);
67  }
68 
69  // OnInspectorGUI function
70  public override void OnInspectorGUI()
71  {
72  serializedObject.Update();
73 
74  header.DrawHeader();
75 
76  EditorGUILayout.Space();
77 
78  containerRL.DoLayoutList();
79 
80  serializedObject.ApplyModifiedProperties();
81  }
82  }
83 
85  {
86  static float lineCount = 1;
87 
88  // Interface functions for container drawer
89  public static float GetContainerDrawerLineCount()
90  {
91  return lineCount;
92  }
93 
94  static public void DrawContainerDrawer(Rect guiRect, SerializedProperty sp)
95  {
96  EditorGUI.LabelField(guiRect, ((Container)sp.objectReferenceValue).ToString());
97  }
98  }
99 }
This class containes the header code for the custom inspector of the Container.
This class extends the Unity ReorderableList with features so it can be used by HKIVar. HKIVars are longer than one line so the height of each ReorderableList element must be adjusted accordingly.
This abstract class of a ScriptableObject is provides child classes with on value changed callback ca...
Definition: HKIVar.cs:12
Custom inspector for the Container class. The code is split into two subclasses for better readabilit...
This container is itself a HKIVar but it purpose is to hold other HKIVars together at one place...
Definition: Container.cs:12
static void DrawContainerDrawer(Rect guiRect, SerializedProperty sp)