protected virtual void OnEnable() { m_AI = (vp_AI)target; if (m_AI != null) { if (m_AI.DefaultState == null) { m_AI.RefreshDefaultState(); } } Plugins = m_AI.GetType().GetFields().Where(info => info.FieldType.BaseType == typeof(vp_AIPlugin)).OrderBy(info => ((vp_AIPlugin)info.GetValue(m_AI)).SortOrder).ToList(); }
/// <summary> /// Updates the values from components that are non mono behaviors. /// </summary> public virtual void UpdateValuesFromComponentNonMonoBehavior <T>(ref T field, string prefix = "") { if (field == null) { return; } Dictionary <string, object> dict = new Dictionary <string, object>(); foreach (FieldInfo info in field.GetType().GetFields()) { dict.Add(info.Name, info.GetValue(field)); } foreach (string k in dict.Keys) { FieldInfo p = this.GetType().GetField(k); if (p != null) { p.SetValue(this, dict[k]); } } if (prefix == "") { return; } vp_AI ai = Prefab.GetComponent <vp_AI>(); foreach (FieldInfo info in ai.GetType().GetFields()) { if (info.Name.IndexOf(prefix) != -1 && info.Name.IndexOf("Foldout") == -1) { FieldInfo p = this.GetType().GetField(info.Name); if (info != null && p != null) { this.GetType() .GetField(info.Name) .SetValue(this, info.GetValue(ai)); } } } }
/// <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 the display of a plugins foldout, enabled button and reset button /// </summary> public static bool BeginPlugin(ref bool foldout, ref bool enabled, string title, string prefix, Type editor = null) { GUIStyle style = new GUIStyle("Button"); style.fontSize = 9; style.alignment = TextAnchor.MiddleCenter; GUILayout.Space(10); GUILayout.BeginHorizontal(); GUILayout.Space(0); foldout = EditorGUI.Foldout(new Rect(GUILayoutUtility.GetLastRect().xMin + 5, GUILayoutUtility.GetLastRect().yMin, 225, 20), foldout, " " + title, true, FoldoutStyle); GUILayout.FlexibleSpace(); if (foldout) { enabled = ButtonToggle("Enabled", "Disabled", enabled, "Determines whether this plugin is enabled or not"); if (GUILayout.Button(new GUIContent("Reset", "Reset all values back to the defaults."), style, GUILayout.Width(60), GUILayout.Height(17))) { if (EditorUtility.DisplayDialog("Are You Sure?", "This will revert all " + title + " Settings back to the defaults. Are you sure you want to do this", "Yes", "No")) { vp_AI newAI = m_AI.gameObject.AddComponent <vp_AI>(); foreach (FieldInfo info in newAI.GetType().GetFields()) { if (info.Name.IndexOf(prefix) != -1) { m_AI.GetType().GetField(info.Name).SetValue(m_AI, info.GetValue(newAI)); if (info.Name == prefix) { if (info.FieldType.BaseType == typeof(vp_AIPlugin)) { vp_AIPlugin comp = (vp_AIPlugin)info.GetValue(m_AI); comp.MainFoldout = true; } } } } // optional reset from this class MethodInfo method = editor.GetMethod("Reset" + prefix); if (method != null) { method.Invoke(editor, null); } // optional editor reset if (editor != null) { method = editor.GetMethod("Reset"); if (method != null) { method.Invoke(editor, null); } } // attempt to check for an editor class for this plugin List <Type> types = (from System.Type t in Assembly.GetExecutingAssembly().GetTypes() where t.IsSubclassOf(typeof(vp_AIEditor)) select t).ToList(); Type type = types.FirstOrDefault(t => t.ToString() == "vp_AI" + prefix + "Editor"); if (type != null) { method = type.GetMethod("Reset"); if (method != null) { method.Invoke(type, null); } } DestroyImmediate(newAI); } } } else { GUIStyle label = new GUIStyle("Label"); label.fontSize = 11; label.normal.textColor = new Color(.4f, .4f, .4f); EditorGUILayout.LabelField(!enabled ? "Disabled" : "", label, GUILayout.Width(60)); } GUILayout.EndHorizontal(); if (!foldout) { DrawSeparator(2); return(false); } GUILayout.Space(10); GUI.enabled = enabled; EditorGUI.indentLevel++; return(true); }