/// <summary> /// Updates all the components. /// </summary> public virtual void UpdateComponents() { if (Prefab == null) { return; } if (!ShouldUpdate) { return; } ShouldUpdate = false; Scale = Prefab.transform.localScale; AmountToSpawn = 1; SpawnAtStart = true; UpdateCachedComponents(); vp_AI aiComponent = Prefab.GetComponent <vp_AI>(); UpdateValuesFromComponentNonMonoBehavior(ref CachedDamageHandlerComponent); UpdateValuesFromComponentNonMonoBehavior(ref CachedMovementComponent); UpdateValuesFromComponentNonMonoBehavior(ref CachedSensesComponent); UpdateValuesFromComponentNonMonoBehavior(ref CachedCombatComponent); UpdateValuesFromComponent(ref aiComponent); }
/// <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> public override void Awake(vp_AI ai) { base.Awake(ai); m_Seeker = m_EventHandler.GetComponent <Seeker>(); //Make sure we receive callbacks when paths complete m_Seeker.pathCallback += OnPathComplete; }
/// <summary> /// This is equivalent to Unity's Awake and will /// be called by the AI components Awake method. /// The AI component needs to be passed to this method. /// </summary> public virtual void Awake(vp_AI ai) { m_Enabled = Enabled; m_AI = ai; m_EventHandler = (vp_AIEventHandler)m_AI.EventHandler; m_Transform = ai.transform; m_GameObject = ai.gameObject; m_Rigidbody = ai.rigidbody; m_Audio = ai.audio; m_Controller = m_Transform.root.GetComponentInChildren <CharacterController>(); }
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> /// Updates the cached components. /// </summary> public virtual void UpdateCachedComponents() { if (Prefab == null) { return; } vp_AI aiComponent = Prefab.GetComponent <vp_AI>(); if (aiComponent.DamageHandler != null) { CachedDamageHandlerComponent = aiComponent.DamageHandler; } if (aiComponent.Movement != null) { CachedMovementComponent = aiComponent.Movement; } if (aiComponent.Combat != null) { CachedCombatComponent = aiComponent.Combat; } }
/// <summary> /// in 'Awake' we do things that need to be run once at the /// very beginning. NOTE: 1) this method must be run using /// 'base.Awake();' on the first line of the 'Awake' method /// in any derived class. 2) keep in mind that as of Unity 4, /// gameobject hierarchy can not be altered in 'Awake' /// </summary> public override void Awake(vp_AI ai) { base.Awake(ai); SetupWaypoints(true); }
/// <summary> /// /// </summary> public override void Awake(vp_AI ai) { base.Awake(ai); }
/// <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); }