Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEditor;
- using UnityEngine;
- using System.Collections.Generic;
- using AC;
- [CustomEditor(typeof(AssetParameters))]
- public class ParametersEditor : Editor
- {
- public override void OnInspectorGUI()
- {
- var t = target as AssetParameters;
- var actionList = t.GetComponent<ActionList>();
- // Check if ActionList is attached
- if (actionList == null || actionList.source != ActionListSource.AssetFile || actionList.assetFile == null)
- {
- t.SetParametersNumber(0);
- EditorGUILayout.HelpBox("An ActionListAsset is required to use this component!", MessageType.Warning);
- return;
- }
- // Check if parameters used by ActionList
- if (actionList.assetFile.parameters.Count == 0)
- {
- t.SetParametersNumber(0);
- EditorGUILayout.HelpBox("No parameters used by attached ActionList.", MessageType.Warning);
- return;
- }
- // Set appropriate parameters number
- if (t.parameters.Count != actionList.assetFile.parameters.Count)
- {
- t.SetParametersNumber(actionList.assetFile.parameters.Count);
- EditorUtility.SetDirty(t);
- }
- // For each parameter, draw parameter fields
- bool isUpdated = false;
- EditorGUILayout.BeginVertical("button");
- for (int i = 0; i < actionList.assetFile.parameters.Count; i++)
- {
- string parameterLabel = actionList.assetFile.parameters[i].label + ":";
- switch (actionList.assetFile.parameters[i].parameterType)
- {
- // Draw boolean popup
- case ParameterType.Boolean:
- int newBool = EditorGUILayout.IntPopup(parameterLabel, t.parameters[i].intValue > 0 ? 1 : 0, new string[] { "False", "True" }, new int[] { 0, 1 });
- if ((newBool > 0) != (t.parameters[i].intValue > 0))
- {
- t.parameters[i].intValue = newBool;
- isUpdated = true;
- }
- break;
- // Draw float field
- case ParameterType.Float:
- float newFloat = EditorGUILayout.FloatField(parameterLabel, t.parameters[i].floatValue);
- if (newFloat != t.parameters[i].floatValue)
- {
- t.parameters[i].floatValue = newFloat;
- isUpdated = true;
- }
- break;
- // Draw integer field
- case ParameterType.Integer:
- int newInt = EditorGUILayout.IntField(parameterLabel, t.parameters[i].intValue);
- if (newInt != t.parameters[i].intValue)
- {
- t.parameters[i].intValue = newInt;
- isUpdated = true;
- }
- break;
- // Draw string field
- case ParameterType.String:
- string newString = EditorGUILayout.TextField(parameterLabel, t.parameters[i].stringValue);
- if (newString != t.parameters[i].stringValue)
- {
- t.parameters[i].stringValue = newString;
- isUpdated = true;
- }
- break;
- // Draw global variable popup
- case ParameterType.GlobalVariable:
- if (AdvGame.GetReferences() && AdvGame.GetReferences().variablesManager)
- {
- VariablesManager variablesManager = AdvGame.GetReferences().variablesManager;
- int newGlobalVar = ShowVarSelectorGUI(parameterLabel, variablesManager.vars, t.parameters[i].intValue);
- if (newGlobalVar != t.parameters[i].intValue)
- {
- t.parameters[i].intValue = newGlobalVar;
- isUpdated = true;
- }
- }
- else
- {
- EditorGUILayout.HelpBox("A Variables Manager is required to pass Global Variables.", MessageType.Warning);
- }
- break;
- // Draw local variable popup
- case ParameterType.LocalVariable:
- if (KickStarter.localVariables)
- {
- int newLocalVar = ShowVarSelectorGUI(parameterLabel, KickStarter.localVariables.localVars, t.parameters[i].intValue);
- if (newLocalVar != t.parameters[i].intValue)
- {
- t.parameters[i].intValue = newLocalVar;
- isUpdated = true;
- }
- }
- else
- {
- EditorGUILayout.HelpBox("A GameEngine prefab is required to pass Local Variables.", MessageType.Warning);
- }
- break;
- // Draw inventory popup
- case ParameterType.InventoryItem:
- if (AdvGame.GetReferences() && AdvGame.GetReferences().inventoryManager)
- {
- InventoryManager inventoryManager = AdvGame.GetReferences().inventoryManager;
- int newInventoryItem = ShowInvItemSelectorGUI(parameterLabel, inventoryManager.items, t.parameters[i].intValue);
- if (newInventoryItem != t.parameters[i].intValue)
- {
- t.parameters[i].intValue = newInventoryItem;
- isUpdated = true;
- }
- }
- else
- {
- EditorGUILayout.HelpBox("An Inventory Manager is required to pass Inventory items.", MessageType.Warning);
- }
- break;
- // Draw unity object selection field
- case ParameterType.UnityObject:
- Object newObj = (Object) EditorGUILayout.ObjectField(parameterLabel, t.parameters[i].objectValue, typeof(Object), true);
- if (newObj != t.parameters[i].objectValue)
- {
- t.parameters[i].objectValue = newObj;
- EditorUtility.SetDirty(t);
- }
- break;
- // Draw game object selection field
- case ParameterType.GameObject:
- // If gameObject is not set, try to find it by constantID
- if (t.parameters[i].gameObject == null)
- t.parameters[i].SetValue(Utils.GetObjectByID(t.parameters[i].intValue), t.parameters[i].intValue);
- EditorGUILayout.BeginHorizontal();
- GameObject newGO = EditorGUILayout.ObjectField(parameterLabel, t.parameters[i].gameObject, typeof(GameObject), true) as GameObject;
- GUILayout.Label("[ID: " + t.parameters[i].intValue + "]", GUILayout.ExpandWidth(false));
- EditorGUILayout.EndHorizontal();
- // Save new gameObject value and constantID
- if (newGO != t.parameters[i].gameObject)
- {
- if (newGO == null)
- {
- t.parameters[i].SetValue(newGO);
- }
- else
- {
- var constantID = newGO.GetComponent<ConstantID>();
- if (constantID == null)
- {
- constantID = newGO.AddComponent<ConstantID>();
- EditorUtility.SetDirty(newGO);
- }
- t.parameters[i].SetValue(newGO, constantID.constantID);
- }
- isUpdated = true;
- }
- break;
- }
- }
- EditorGUILayout.EndVertical();
- if (isUpdated)
- {
- // If any changes made, set target dirty
- EditorUtility.SetDirty(t);
- // If changes made while game is playing, apply
- if (Application.isPlaying)
- t.ApplyValues();
- }
- }
- private int ShowInvItemSelectorGUI(string label, List<InvItem> items, int ID)
- {
- int invNumber = -1;
- List<string> labelList = new List<string>();
- labelList.Add(" (None)");
- foreach (InvItem _item in items)
- {
- labelList.Add(_item.label);
- }
- invNumber = GetInvNumber(items, ID) + 1;
- invNumber = EditorGUILayout.Popup(label, invNumber, labelList.ToArray()) - 1;
- if (invNumber >= 0)
- {
- return items[invNumber].id;
- }
- return -1;
- }
- private int GetInvNumber(List<InvItem> items, int ID)
- {
- int i = 0;
- foreach (InvItem _item in items)
- {
- if (_item.id == ID)
- {
- return i;
- }
- i++;
- }
- return -1;
- }
- private int ShowVarSelectorGUI(string label, List<GVar> vars, int ID)
- {
- int variableNumber = -1;
- List<string> labelList = new List<string>();
- labelList.Add(" (None)");
- foreach (GVar _var in vars)
- {
- labelList.Add(_var.label);
- }
- variableNumber = GetVarNumber(vars, ID) + 1;
- variableNumber = EditorGUILayout.Popup(label, variableNumber, labelList.ToArray()) - 1;
- if (variableNumber >= 0)
- {
- return vars[variableNumber].id;
- }
- return -1;
- }
- private int GetVarNumber(List<GVar> vars, int ID)
- {
- int i = 0;
- foreach (GVar _var in vars)
- {
- if (_var.id == ID)
- {
- return i;
- }
- i++;
- }
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement