/// <summary> /// Create the BasicAttributes and add the Health attribute /// </summary> /// <param name="rMotionController"></param> /// <param name="rHealth">Health value to set; if the Health attribute already exist, then its value /// is not overwritten</param> /// <param name="rMana"></param> /// <param name="rStamina"></param> /// <returns></returns> public static BasicAttributes CreateBasicAttributes(MotionController rMotionController, float rHealth, float rMana = 0, float rStamina = 0) { BasicAttributes lAttributes = rMotionController.GetOrAddComponent <BasicAttributes>(); if (!lAttributes.AttributeExists("Health")) { lAttributes.SetAttributeValue <float>("Health", rHealth); BasicAttributeFloat lHealth = lAttributes.GetAttribute("Health") as BasicAttributeFloat; if (lHealth != null) { lHealth.MinValue = 0; lHealth.MaxValue = rHealth; } } if (rMana > 0 && !lAttributes.AttributeExists("Mana")) { lAttributes.SetAttributeValue <float>("Mana", rMana); BasicAttributeFloat lMana = lAttributes.GetAttribute("Mana") as BasicAttributeFloat; if (lMana != null) { lMana.MinValue = 0; lMana.MaxValue = rMana; } } if (rStamina > 0 && !lAttributes.AttributeExists("Stamina")) { lAttributes.SetAttributeValue <float>("Stamina", rStamina); BasicAttributeFloat lStamina = lAttributes.GetAttribute("Stamina") as BasicAttributeFloat; if (lStamina != null) { lStamina.MinValue = 0; lStamina.MaxValue = rMana; } } lAttributes.OnBeforeSerialize(); return(lAttributes); }
/// <summary> /// Called when the inspector needs to draw /// </summary> public override void OnInspectorGUI() { // Pulls variables from runtime so we have the latest values. mTargetSO.Update(); GUILayout.Space(5); EditorHelper.DrawInspectorTitle("ootii Basic Attributes"); EditorHelper.DrawInspectorDescription("Allows us to assign Attributes to actors.", MessageType.None); GUILayout.Space(5); //EditorGUILayout.LabelField("Attributes", EditorStyles.boldLabel, GUILayout.Height(16f)); GUILayout.BeginVertical(EditorHelper.GroupBox); mAttributeItemList.DoLayoutList(); if (mAttributeItemList.index >= mAttributeItemList.count) { mAttributeItemList.index = mAttributeItemList.count - 1; } if (mAttributeItemList.index >= 0) { GUILayout.Space(5f); GUILayout.BeginVertical(EditorHelper.Box); bool lListIsDirty = DrawAttributeItemDetail(mTarget.Items[mAttributeItemList.index]); if (lListIsDirty) { mIsDirty = true; } GUILayout.EndVertical(); } EditorGUILayout.EndVertical(); // Show the events GUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("Events"), EditorStyles.boldLabel)) { mTarget.EditorShowEvents = !mTarget.EditorShowEvents; } GUILayout.FlexibleSpace(); if (GUILayout.Button(new GUIContent(mTarget.EditorShowEvents ? "-" : "+"), EditorStyles.boldLabel)) { mTarget.EditorShowEvents = !mTarget.EditorShowEvents; } GUILayout.EndHorizontal(); GUILayout.BeginVertical(EditorHelper.GroupBox); EditorHelper.DrawInspectorDescription("Assign functions to be called when specific events take place.", MessageType.None); if (mTarget.EditorShowEvents) { GUILayout.BeginVertical(EditorHelper.Box); SerializedProperty lActivatedEvent = mTargetSO.FindProperty("AttributeValueChangedEvent"); if (lActivatedEvent != null) { EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(lActivatedEvent); if (EditorGUI.EndChangeCheck()) { mIsDirty = true; } } GUILayout.EndVertical(); } GUILayout.EndVertical(); GUILayout.Space(5); // If there is a change... update. if (mIsDirty) { // Flag the object as needing to be saved EditorUtility.SetDirty(mTarget); #if UNITY_4_0 || UNITY_4_0_1 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 EditorApplication.MarkSceneDirty(); #else if (!EditorApplication.isPlaying) { UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene()); } #endif // Pushes the values back to the runtime so it has the changes mTargetSO.ApplyModifiedProperties(); // Serialize to the definitions mTarget.OnBeforeSerialize(); // Clear out the dirty flag mIsDirty = false; } }