Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- using System.Reflection;
- [CustomEditor(typeof(Bot))]
- public class CustomBot : Editor
- {
- static bool foldout;
- static bool generalSettings;
- List<string> fieldsArray = new List<string>();
- List<FieldInfo> actionArray = new List<FieldInfo>();
- Bot bot;
- public void OnEnable()
- {
- bot = (Bot)target; // set the bot target
- // keep foldouts the way they were before
- foldout = EditorPrefs.GetBool("foldout");
- generalSettings = EditorPrefs.GetBool("general");
- Debug.Log("called on enable");
- // reset fields and action arrays
- fieldsArray.Clear();
- actionArray.Clear();
- foreach (FieldInfo field in typeof(Bot).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default))
- {
- string nume = field.Name;
- if (nume.EndsWith("Action") && field.FieldType == typeof(BotAction) && field.IsPrivate == false)
- {
- actionArray.Add(field);
- if (toggles.Count < actionArray.Count)
- toggles.Add(false);
- }
- else
- {
- fieldsArray.Add(nume);
- }
- }
- }
- private bool ceva;
- float speed;
- public override void OnInspectorGUI()
- {
- serializedObject.Update();
- EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((Bot)target), typeof(Bot), false);
- foldout = EditorGUILayout.Foldout(foldout, "Behaviour Settings");
- EditorPrefs.SetBool("foldout", foldout);
- ceva = EditorGUILayout.PropertyField(serializedObject.FindProperty("ceva"));
- if (foldout)
- {
- int index = 0;
- foreach (FieldInfo fieldInfo in actionArray)
- {
- // display the toogle
- GUILayout.BeginHorizontal();
- if (index >= bot.toggleList.Count)
- {
- bot.toggleList.Add(false);
- Debug.Log("adding element");
- }
- bot.toggleList[index] = EditorGUILayout.PropertyField(serializedObject.FindProperty("toggleList").GetArrayElementAtIndex(index),true);
- GUILayout.Space(10);
- index++;
- string name = fieldInfo.Name;
- if (bot.toggleList[index - 1] == true)
- {
- bot.AddAction((BotAction)fieldInfo.GetValue(bot));
- EditorGUILayout.PropertyField(serializedObject.FindProperty(name), GUILayout.MinWidth(100));
- }
- else
- {
- bot.RemoveAction((BotAction)fieldInfo.GetValue(bot));
- string actualName = char.ToUpper(name[0]) + name.Substring(1);
- EditorGUILayout.LabelField(actualName);
- }
- GUILayout.EndHorizontal();
- }
- }
- generalSettings = EditorGUILayout.Foldout(generalSettings, "General Settings");
- EditorPrefs.SetBool("general", generalSettings);
- if (generalSettings)
- {
- foreach (string name in fieldsArray)
- {
- //Debug.Log("field name : " + name);
- try
- {
- EditorGUILayout.PropertyField(serializedObject.FindProperty(name), true);
- }
- catch
- {
- continue;
- }
- }
- }
- serializedObject.ApplyModifiedProperties();
- }
- }
Add Comment
Please, Sign In to add comment