/// <summary> /// Unity callback to draw a custom inspector. /// </summary> public override void OnInspectorGUI() { // Workaround to update gui controls if (Event.current.type == EventType.ValidateCommand && Event.current.commandName == "UndoRedoPerformed") { GUIUtility.hotControl = 0; GUIUtility.keyboardControl = 0; } // Draw Default Inspector DrawDefaultInspector(); // Draw Variables EditorGUILayout.Space(); if (!m_VariableEditor.IsValid()) { // UnityEditor does not immediately updates the "target" property data if you change the script reference in the inspector. // Below is a workaround that uses a "null" target to get the actual Blackboard: m_VariableEditor = new VariableEditor(m_Blackboard.gameObject.GetComponent <InternalBlackboard>()); } m_VariableEditor.OnGUI(); EditorGUILayout.Space(); // Draw Script Button if (target != null) { var script = m_ScriptProperty.objectReferenceValue as MonoScript; System.Type type = script != null?script.GetClass() : null; if (type != null) { EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (type.FullName == "BehaviourMachine.Blackboard") { if (GUILayout.Button(new GUIContent("Generate Script", "It will generate a C# script with properties containing the variables' id in this Blackboard. Very usefull to get variables in the Blackboard from a custom state"), GUILayout.MaxWidth(200f))) { var scriptPath = EditorUtility.SaveFilePanelInProject("Save Custom Blackboard Script", m_Blackboard.name.Replace(" ", "_") + "Blackboard.cs", "cs", "Please enter the class name of the custom Blackboard"); if (!string.IsNullOrEmpty(scriptPath)) { MonoScript newScript = BlackboardCodeGenerator.CreateOrEditCustomBlackboard(scriptPath, m_Blackboard); // Validate the newScript if (newScript != null) { m_ScriptProperty.objectReferenceValue = newScript; m_SerialObj.ApplyModifiedProperties(); // Recreate members this.OnEnable(); // Ping the script in the Project EditorGUIUtility.PingObject(newScript.GetInstanceID()); } } } } else if (GUILayout.Button(new GUIContent("Update Script", "It will update the file " + script.name + ".cs with the variables in this Blackboard, use this when you have added/removed/renamed a variable in this Blackboard."), GUILayout.MaxWidth(200f))) { var scriptPath = AssetDatabase.GetAssetPath(script.GetInstanceID()); BlackboardCodeGenerator.CreateOrEditCustomBlackboard(scriptPath, m_Blackboard); } GUILayout.FlexibleSpace(); EditorGUILayout.EndHorizontal(); } } else { EditorGUILayout.HelpBox("The component script has changed. Enter and exit in play mode to reaload this editor.", MessageType.Warning); } }
public static MonoScript CreateOrEditCustomBlackboard(string path, InternalBlackboard blackboard) { try { // opens the file if it allready exists, creates it otherwise using (FileStream stream = File.Open(path, FileMode.Create, FileAccess.Write)) { // Create the string builder var code = new CodeBuilder(); // Has Namespace bool hasNamespace = !string.IsNullOrEmpty(blackboard.Namespace); // Add header code.AppendLine(BlackboardCodeGenerator.GetHeader()); // Add namespaces code.AppendLine("using UnityEngine;"); code.AppendLine("using BehaviourMachine;"); code.AppendLine(); // Add namespace? if (hasNamespace) { code.AppendLine("namespace " + blackboard.Namespace + " {"); // Add more one tab code.tabSize += 1; } // It is the GlobalBlackboard? if (blackboard.GetType().Name == "GlobalBlackboard") { // Add Componente Menu code.AppendLine("[AddComponentMenu(\"\")]"); // Add class code.AppendLine("public class " + BlackboardCodeGenerator.GetClassName(path) + " : InternalGlobalBlackboard {"); // Add more one tab code.tabSize += 1; // Add Instance property code.AppendLine("/// <summary>"); code.AppendLine("/// The GlobalBlackboard instance."); code.AppendLine("/// </summary>"); code.AppendLine("public static new GlobalBlackboard Instance {get {return InternalGlobalBlackboard.Instance as GlobalBlackboard;}}"); code.AppendLine(); } else { // Add class code.AppendLine("public class " + BlackboardCodeGenerator.GetClassName(path) + " : Blackboard {"); // Add more one tab code.tabSize += 1; } // FloatVar FloatVar[] floatVars = blackboard.floatVars; if (floatVars.Length > 0) { code.AppendLine("// FloatVars"); for (int i = 0; i < floatVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(floatVars[i])); } } // IntVar IntVar[] intVars = blackboard.intVars; if (intVars.Length > 0) { code.AppendLine("// IntVars"); for (int i = 0; i < intVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(intVars[i])); } } // BoolVar BoolVar[] boolVars = blackboard.boolVars; if (boolVars.Length > 0) { code.AppendLine("// BoolVars"); for (int i = 0; i < boolVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(boolVars[i])); } } // StringVar StringVar[] stringVars = blackboard.stringVars; if (stringVars.Length > 0) { code.AppendLine("// StringVars"); for (int i = 0; i < stringVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(stringVars[i])); } } // Vector3Var Vector3Var[] vector3Vars = blackboard.vector3Vars; if (vector3Vars.Length > 0) { code.AppendLine("// Vector3Vars"); for (int i = 0; i < vector3Vars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(vector3Vars[i])); } } // RectVar RectVar[] rectVars = blackboard.rectVars; if (rectVars.Length > 0) { code.AppendLine("// RectVars"); for (int i = 0; i < rectVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(rectVars[i])); } } // ColorVar ColorVar[] colorVars = blackboard.colorVars; if (colorVars.Length > 0) { code.AppendLine("// ColorVars"); for (int i = 0; i < colorVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(colorVars[i])); } } // QuaternionVar QuaternionVar[] quaternionVars = blackboard.quaternionVars; if (quaternionVars.Length > 0) { code.AppendLine("// QuaternionVars"); for (int i = 0; i < quaternionVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(quaternionVars[i])); } } // GameObjectVar GameObjectVar[] gameObjectVars = blackboard.gameObjectVars; if (gameObjectVars.Length > 0) { code.AppendLine("// GameObjectVars"); for (int i = 0; i < gameObjectVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(gameObjectVars[i])); } } // TextureVar TextureVar[] textureVars = blackboard.textureVars; if (textureVars.Length > 0) { code.AppendLine("// TextureVars"); for (int i = 0; i < textureVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(textureVars[i])); } } // MaterialVar MaterialVar[] materialVars = blackboard.materialVars; if (materialVars.Length > 0) { code.AppendLine("// MaterialVars"); for (int i = 0; i < materialVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(materialVars[i])); } } // ObjectVar ObjectVar[] objectVars = blackboard.objectVars; if (objectVars.Length > 0) { code.AppendLine("// ObjectVars"); for (int i = 0; i < objectVars.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(objectVars[i])); } } // FsmEvents FsmEvent[] fsmEvents = blackboard.fsmEvents; if (fsmEvents.Length > 0) { code.AppendLine("// FsmEvents"); for (int i = 0; i < fsmEvents.Length; i++) { code.AppendLine(BlackboardCodeGenerator.GetVariableMember(fsmEvents[i])); } } // Remove one tab code.tabSize -= 1; // Close class brackets code.AppendLine("}"); // Close namespace brackets if (hasNamespace) { // Remove one tab code.tabSize -= 1; // Close brackets code.AppendLine("}"); } // Write data on file using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(code.ToString()); } } AssetDatabase.Refresh(); return(AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)) as MonoScript); } catch (System.Exception e) { Debug.LogException(e); return(null); } }