HKI Core
HKIVar_ContainerHeader.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEditor;
3 using HKI.Core.Variables;
5 
6 namespace HKI.Core.Variables.Editor
7 {
12  {
13  // Private variables
14  const float buttonHeight = 14.0f;
15  int varTypesSlideId = 1;
16  int selectedVarTypeId = 0;
17 
18  bool isLimited = false;
19  int limitedVarTypesSlideId = 0;
20 
21  const string containerSPName = "container";
22  SerializedProperty containerSP = null;
23 
24  UnityEngine.Object container = null;
25 
26  // Interface function
27  public HKIVar_ContainerHeader(SerializedObject serializedObject, UnityEngine.Object container, bool isLimited = false, int limitedVarTypesSlideId = 0)
28  {
29  this.container = container;
30  this.isLimited = isLimited;
31  this.limitedVarTypesSlideId = limitedVarTypesSlideId;
32 
33  containerSP = serializedObject.FindProperty(containerSPName);
34  }
35 
36  public void DrawHeader()
37  {
38  EditorGUILayout.LabelField("Create new element in this container", EditorStyles.boldLabel);
39 
40  EditorGUILayout.BeginHorizontal();
41 
42  GUI.enabled = !isLimited;
43  int newVarTypesSlideId = EditorGUILayout.Popup(isLimited ? limitedVarTypesSlideId : varTypesSlideId, HKIVarTypes.VarTypesCollectionNames.ToArray());
44  GUI.enabled = true;
45  if (newVarTypesSlideId != varTypesSlideId)
46  {
47  varTypesSlideId = newVarTypesSlideId;
48  selectedVarTypeId = 0;
49  }
50 
51  selectedVarTypeId = EditorGUILayout.Popup(selectedVarTypeId, HKIVarTypes.VarTypesCollections[varTypesSlideId].TypeNames.ToArray());
52 
53  if (GUILayout.Button("Add", GUILayout.Width(50.0f), GUILayout.Height(buttonHeight)))
54  {
55  HKIVar newVariable = (HKIVar)ScriptableObject.CreateInstance(HKIVarTypes.VarTypesCollections[varTypesSlideId].Types[selectedVarTypeId]);
56  newVariable.name = "New " + HKIVarTypes.VarTypesCollections[varTypesSlideId].TypeNames[selectedVarTypeId];
57 
58  AssetDatabase.AddObjectToAsset(newVariable, container);
59  AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(newVariable));
60 
61  containerSP.InsertArrayElementAtIndex(containerSP.arraySize);
62  SerializedProperty elementSP = containerSP.GetArrayElementAtIndex(containerSP.arraySize - 1);
63 
64  elementSP.objectReferenceValue = newVariable;
65  }
66 
67  EditorGUILayout.EndHorizontal();
68  }
69  }
70 }
static List< string > VarTypesCollectionNames
Definition: HKIVarTypes.cs:74
This class containes the header code for the custom inspector of the Container.
This abstract class of a ScriptableObject is provides child classes with on value changed callback ca...
Definition: HKIVar.cs:12
HKIVar_ContainerHeader(SerializedObject serializedObject, UnityEngine.Object container, bool isLimited=false, int limitedVarTypesSlideId=0)
This class is for container editor to find out what HKIVar value types are existing.
Definition: HKIVarTypes.cs:14
static List< HKIVarTypesCollection > VarTypesCollections
Definition: HKIVarTypes.cs:73