Guest User

Untitled

a guest
May 2nd, 2020
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.Reflection;
  6.  
  7.  
  8. [CustomEditor(typeof(Bot))]
  9. public class CustomBot : Editor
  10. {
  11.  
  12. static bool foldout;
  13. static bool generalSettings;
  14. List<string> fieldsArray = new List<string>();
  15. List<FieldInfo> actionArray = new List<FieldInfo>();
  16. Bot bot;
  17.  
  18.  
  19. public void OnEnable()
  20. {
  21. bot = (Bot)target; // set the bot target
  22. // keep foldouts the way they were before
  23. foldout = EditorPrefs.GetBool("foldout");
  24. generalSettings = EditorPrefs.GetBool("general");
  25.  
  26. Debug.Log("called on enable");
  27. // reset fields and action arrays
  28. fieldsArray.Clear();
  29. actionArray.Clear();
  30. foreach (FieldInfo field in typeof(Bot).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Default))
  31. {
  32.  
  33. string nume = field.Name;
  34. if (nume.EndsWith("Action") && field.FieldType == typeof(BotAction) && field.IsPrivate == false)
  35. {
  36. actionArray.Add(field);
  37. if (toggles.Count < actionArray.Count)
  38. toggles.Add(false);
  39. }
  40. else
  41. {
  42. fieldsArray.Add(nume);
  43. }
  44. }
  45. }
  46. private bool ceva;
  47. float speed;
  48. public override void OnInspectorGUI()
  49. {
  50. serializedObject.Update();
  51.  
  52. EditorGUILayout.ObjectField("Script", MonoScript.FromMonoBehaviour((Bot)target), typeof(Bot), false);
  53. foldout = EditorGUILayout.Foldout(foldout, "Behaviour Settings");
  54. EditorPrefs.SetBool("foldout", foldout);
  55. ceva = EditorGUILayout.PropertyField(serializedObject.FindProperty("ceva"));
  56.  
  57. if (foldout)
  58. {
  59. int index = 0;
  60. foreach (FieldInfo fieldInfo in actionArray)
  61. {
  62. // display the toogle
  63. GUILayout.BeginHorizontal();
  64. if (index >= bot.toggleList.Count)
  65. {
  66. bot.toggleList.Add(false);
  67. Debug.Log("adding element");
  68. }
  69. bot.toggleList[index] = EditorGUILayout.PropertyField(serializedObject.FindProperty("toggleList").GetArrayElementAtIndex(index),true);
  70. GUILayout.Space(10);
  71. index++;
  72.  
  73. string name = fieldInfo.Name;
  74. if (bot.toggleList[index - 1] == true)
  75. {
  76. bot.AddAction((BotAction)fieldInfo.GetValue(bot));
  77. EditorGUILayout.PropertyField(serializedObject.FindProperty(name), GUILayout.MinWidth(100));
  78. }
  79. else
  80. {
  81. bot.RemoveAction((BotAction)fieldInfo.GetValue(bot));
  82. string actualName = char.ToUpper(name[0]) + name.Substring(1);
  83. EditorGUILayout.LabelField(actualName);
  84. }
  85. GUILayout.EndHorizontal();
  86. }
  87. }
  88. generalSettings = EditorGUILayout.Foldout(generalSettings, "General Settings");
  89. EditorPrefs.SetBool("general", generalSettings);
  90. if (generalSettings)
  91. {
  92. foreach (string name in fieldsArray)
  93. {
  94. //Debug.Log("field name : " + name);
  95. try
  96. {
  97. EditorGUILayout.PropertyField(serializedObject.FindProperty(name), true);
  98. }
  99. catch
  100. {
  101. continue;
  102. }
  103. }
  104. }
  105. serializedObject.ApplyModifiedProperties();
  106.  
  107. }
  108. }
Add Comment
Please, Sign In to add comment