protected static string m_ContainerName = "AI Container"; // empty gameobject that gets created to store all the AI /// <summary> /// in 'Awake' we do things that need to be run once at the /// very beginning. NOTE: as of Unity 4, gameobject hierarchy /// can not be altered in 'Awake' /// </summary> protected override void Awake() { base.Awake(); m_EventHandler = (vp_AIEventHandler)EventHandler; // use reflection to loop through fields in this class foreach (FieldInfo info in this.GetType().GetFields()) { // check if the field is a plugin if (info.FieldType.BaseType == typeof(vp_AIPlugin)) { // get the value of the plugin vp_AIPlugin plugin = (vp_AIPlugin)info.GetValue(this); if (plugin == null) { continue; } // check if the plugin is enabled if (plugin.Enabled) { // setup the AIStates in the plugins so they have the correct values foreach (FieldInfo stateInfo in plugin.GetType().GetFields()) { if (stateInfo.FieldType == typeof(vp_AIState)) { stateInfo.SetValue(plugin, RefreshState((vp_AIState)stateInfo.GetValue(plugin))); } } // add the plugin to the list m_Plugins.Add(plugin); } } // setup any AIStates in this class so they have the correct values if (info.FieldType == typeof(vp_AIState)) { info.SetValue(this, RefreshState((vp_AIState)info.GetValue(this))); } } // order the plugins by their sort order values m_Plugins = m_Plugins.OrderBy(p => p.SortOrder).ToList(); // run Awake on all the enabled plugins foreach (vp_AIPlugin plugin in m_Plugins) { plugin.Awake(this); if (!enabled) { plugin.OnDisable(); } } if (!enabled) { if (PlaceOnGround != null) { vp_Timer.In(0.1f, delegate { PlaceOnGround.PositionOnGround(true); }); } } }
/// <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); }