/// <summary> /// Handles foldouts from vp_AIBeginFoldout /// </summary> public static bool SetFoldoutValue(FieldInfo info, vp_AIField attribute) { EditorGUILayout.Space(); bool foldout = EditorGUI.Foldout(new Rect(GUILayoutUtility.GetLastRect().xMin + 5, GUILayoutUtility.GetLastRect().yMin, 225, 20), (bool)info.GetValue(m_AI), " " + attribute.Title, true, (bool)info.GetValue(m_AI) ? SubFoldoutStyle : new GUIStyle("Foldout")); EditorGUILayout.Space(); EditorGUILayout.Space(); info.SetValue(m_AI, foldout); return(foldout); }
/// <summary> /// Handles UnityEngine.Objects /// </summary> public static void SetObjectValue(FieldInfo info, vp_AIField attribute, bool allowSceneObjects) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.ObjectField((UnityEngine.Object)info.GetValue(m_AI), info.FieldType, true)); } else { info.SetValue(m_AI, EditorGUILayout.ObjectField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (UnityEngine.Object)info.GetValue(m_AI), info.FieldType, true)); } }
/// <summary> /// Displays a range field /// </summary> public static void RangeField(FieldInfo info, vp_AIField attribute) { GUILayout.BeginHorizontal(); EditorGUILayout.LabelField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip)); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); float min = ((Vector2)info.GetValue(m_AI)).x; float max = ((Vector2)info.GetValue(m_AI)).y; min = EditorGUILayout.FloatField(min, GUILayout.MaxWidth(100)); EditorGUILayout.MinMaxSlider(ref min, ref max, attribute.MinValue, attribute.MaxValue); max = EditorGUILayout.FloatField(max, GUILayout.MaxWidth(100)); info.SetValue(m_AI, new Vector2((float)System.Math.Round(min, 2), (float)System.Math.Round(max, 2))); GUILayout.EndHorizontal(); }
/// <summary> /// Handles custom method execution /// </summary> public virtual void CustomMethod(FieldInfo plugin, vp_AIField attribute) { List <Type> types = (from System.Type t in Assembly.GetExecutingAssembly().GetTypes() where t.IsSubclassOf(typeof(vp_AIEditor)) select t).ToList(); types.Add(this.GetType()); Type type = this.GetType(); string className = ((vp_AICustomMethod)attribute).ClassName != "" ? ((vp_AICustomMethod)attribute).ClassName : "vp_AI" + plugin.Name + "Editor"; Type tempType = types.FirstOrDefault(t => t.ToString() == className); if (tempType != null) { type = tempType; } MethodInfo methodInfo = type.GetMethod(((vp_AICustomMethod)attribute).MethodName); if (methodInfo != null) { methodInfo.Invoke(this, null); } }
/// <summary> /// Displays a plugin from it's FieldInfo /// which is automatically obtained from vp_AI /// </summary> public void DisplayPlugin(FieldInfo plugin) { int foldoutCount = 0; int foldoutsOpen = 0; if (!BeginPlugin(ref ((vp_AIPlugin)plugin.GetValue(m_AI)).MainFoldout, ref ((vp_AIPlugin)plugin.GetValue(m_AI)).Enabled, ((vp_AIPlugin)plugin.GetValue(m_AI)).Title, plugin.Name, this.GetType())) { return; } List <FieldInfo> fields = m_AI.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(fi => fi.Name.IndexOf(plugin.Name) != -1).ToList(); foreach (FieldInfo info in fields) { bool end = false; // position any vp_AIEndFoldout at the end of the attributes list object[] tempAttr = info.GetCustomAttributes(typeof(vp_AIField), true); List <object> attributes = new List <object>(); object endFoldout = null; for (int i = 0; i < tempAttr.Length; i++) { if (tempAttr[i].GetType() == typeof(vp_AIEndFoldout)) { endFoldout = tempAttr[i]; } else { attributes.Add(tempAttr[i]); } } if (endFoldout != null) { attributes.Add(endFoldout); } // loop through the attributes for (int i = 0; i < attributes.Count; i++) { vp_AIField attribute = (vp_AIField)attributes[i]; // vp_AIBeginFoldout if (attribute.GetType() == typeof(vp_AIBeginFoldout)) { foldoutCount++; if (SetFoldoutValue(info, attribute)) { foldoutsOpen++; } if (foldoutsOpen == foldoutCount) { EditorGUI.indentLevel++; } } // vp_AIEndFoldout if (attribute.GetType() == typeof(vp_AIEndFoldout)) { end = true; } if (foldoutsOpen != foldoutCount && !end) { continue; } // vp_AICustomMethod Attribute if (attribute.GetType() == typeof(vp_AICustomMethod)) { CustomMethod(plugin, attribute); } // vp_AIField Attribute if (attribute.GetType() == typeof(vp_AIField)) { if (info.FieldType.BaseType == typeof(Component) || info.FieldType.BaseType == typeof(UnityEngine.Object)) { SetObjectValue(info, attribute, true); } else { SetStandardValue(info, attribute); } } // vp_AIAnimationStateField Attribute if (attribute.GetType() == typeof(vp_AIAnimationStateField)) { info.SetValue(m_AI, GetAnimationState(attribute.Title, (vp_AIState)info.GetValue(m_AI), attribute.Tooltip)); } // vp_AIAudioStateField Attribute if (attribute.GetType() == typeof(vp_AIAudioStateField)) { info.SetValue(m_AI, GetAudioState(attribute.Title, (vp_AIState)info.GetValue(m_AI), attribute.Tooltip)); } // vp_AIRangeField if (attribute.GetType() == typeof(vp_AIRangeField)) { RangeField(info, attribute); } if (end) { if (foldoutsOpen == foldoutCount) { GUILayout.Space(10); EditorGUI.indentLevel--; } foldoutCount--; if (foldoutCount == 0) { foldoutsOpen = 0; } else { foldoutsOpen--; } } } } EndPlugin(); }
/// <summary> /// Handles all the standard types /// </summary> public static void SetStandardValue(FieldInfo info, vp_AIField attribute) { // Integer if (info.FieldType == typeof(int)) { if (attribute.Title == "" || attribute.Title == null) { if (attribute.Slider) { info.SetValue(m_AI, EditorGUILayout.IntSlider((int)info.GetValue(m_AI), (int)attribute.MinValue, (int)attribute.MaxValue)); } else { info.SetValue(m_AI, EditorGUILayout.IntField((int)info.GetValue(m_AI))); } } else if (attribute.Slider) { info.SetValue(m_AI, EditorGUILayout.IntSlider(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (int)info.GetValue(m_AI), (int)attribute.MinValue, (int)attribute.MaxValue)); } else { info.SetValue(m_AI, EditorGUILayout.IntField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (int)info.GetValue(m_AI))); } } // Float if (info.FieldType == typeof(float)) { if (attribute.Title == "" || attribute.Title == null) { if (attribute.Slider) { info.SetValue(m_AI, EditorGUILayout.Slider((float)info.GetValue(m_AI), attribute.MinValue, attribute.MaxValue)); } else { info.SetValue(m_AI, EditorGUILayout.FloatField((float)info.GetValue(m_AI))); } } else if (attribute.Slider) { info.SetValue(m_AI, EditorGUILayout.Slider(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (float)info.GetValue(m_AI), attribute.MinValue, attribute.MaxValue)); } else { info.SetValue(m_AI, EditorGUILayout.FloatField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (float)info.GetValue(m_AI))); } } // Bool if (info.FieldType == typeof(bool)) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.Toggle((bool)info.GetValue(m_AI))); } else { info.SetValue(m_AI, EditorGUILayout.Toggle(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (bool)info.GetValue(m_AI))); } } // String if (info.FieldType == typeof(string)) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.TextField((string)info.GetValue(m_AI))); } else { info.SetValue(m_AI, EditorGUILayout.TextField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (string)info.GetValue(m_AI))); } } // Vector2 if (info.FieldType == typeof(Vector2)) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.Vector2Field(info.Name, (Vector2)info.GetValue(m_AI))); } else { EditorGUILayout.LabelField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip)); GUILayout.Space(-20); info.SetValue(m_AI, EditorGUILayout.Vector2Field("", (Vector2)info.GetValue(m_AI))); } } // Vector3 if (info.FieldType == typeof(Vector3)) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.Vector3Field(info.Name, (Vector3)info.GetValue(m_AI))); } else { EditorGUILayout.LabelField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip)); GUILayout.Space(-20); info.SetValue(m_AI, EditorGUILayout.Vector3Field("", (Vector3)info.GetValue(m_AI))); } } // Vector4 if (info.FieldType == typeof(Vector4)) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.Vector4Field(info.Name, (Vector4)info.GetValue(m_AI))); } else { EditorGUILayout.LabelField(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip)); GUILayout.Space(-20); info.SetValue(m_AI, EditorGUILayout.Vector4Field("", (Vector4)info.GetValue(m_AI))); } } // Enum if (info.FieldType.IsEnum) { if (attribute.Title == "" || attribute.Title == null) { info.SetValue(m_AI, EditorGUILayout.EnumPopup((Enum)info.GetValue(m_AI))); } else { info.SetValue(m_AI, EditorGUILayout.EnumPopup(new GUIContent(attribute.Title, attribute.Title + "\n\n" + attribute.Tooltip), (Enum)info.GetValue(m_AI))); } } // LayerMask if (info.FieldType == typeof(LayerMask)) { info.SetValue(m_AI, LayerMaskField(attribute.Title, (LayerMask)info.GetValue(m_AI), attribute.Title + "\n\n" + attribute.Tooltip)); } }