private ScriptNavigatorItem ScriptNavigatorItemFor(MonoScript script, string path) { var fullPath = System.IO.Path.GetFullPath(path); // get extension var fileName = System.IO.Path.GetFileName(fullPath); var instanceID = script.GetInstanceID(); return(new ScriptNavigatorItem(fileName, instanceID, FileNavigationService)); }
public static void createScriptableObject() { if (Selection.activeObject is MonoScript) { MonoScript ms = (MonoScript)Selection.activeObject; ScriptableObject so = ScriptableObject.CreateInstance(ms.name); string path = System.IO.Directory.GetParent(AssetDatabase.GetAssetPath(ms.GetInstanceID())) + "/" + ms.name + ".asset"; int cntr = 0; while (!createIfDoesntExists(path, so)) { path = System.IO.Directory.GetParent(AssetDatabase.GetAssetPath(ms.GetInstanceID())) + "/" + ms.name + cntr.ToString() + ".asset"; cntr++; if (cntr > 10) { break; } } AssetDatabase.Refresh(); } }
private static void UpdateClassNameToMatchFile() { MonoScript[] scripts = Selection.GetFiltered <MonoScript>(SelectionMode.Assets); if (scripts.IsNullOrEmpty() || scripts.Length != 1) { Debug.LogError("One script file must be selected to update the class contained in it."); return; } MonoScript script = scripts[0]; int selectedScriptID = script.GetInstanceID(); string newClassName = script.name; ReplaceClassName(newClassName, AssetDatabase.GetAssetPath(selectedScriptID)); }
private void OnGUI() { if (!Application.isPlaying) { DisplayMessage("Application needs to be running!", Color.yellow); return; } if (StateMachineAccessor.stateMachine == null) { DisplayMessage("No state machines!", Color.yellow); return; } DisplayMessage("Top", Color.white); GUIStyle cyan = new GUIStyle(EditorStyles.label); cyan.normal.textColor = Color.cyan; string[] states = StateMachineAccessor.stateMachine.GetStateNames(); int count = states.Length; for (int i = 0; i < count; ++i) { if (GUILayout.Button(states[i], cyan)) { var results = AssetDatabase.FindAssets(states[i] + ".cs"); if (results.Length > 0) { MonoScript script = AssetDatabase.LoadAssetAtPath <MonoScript>(results[0]); if (script != null) { AssetDatabase.OpenAsset(script.GetInstanceID()); } } } } DisplayMessage("Bottom", Color.white); }
public static void Create() { try { MonoScript SelectedScript = (MonoScript)Selection.activeObject; string Path = AssetDatabase.GetAssetPath(SelectedScript.GetInstanceID()); Path = Path.Substring(0, Path.Length - 3) + ".asset"; AssetDatabase.CreateAsset(ScriptableObject.CreateInstance(SelectedScript.GetClass().Name), Path); AssetDatabase.SaveAssets(); EditorUtility.FocusProjectWindow(); Debug.Log(SelectedScript.name + ".asset created Successfully"); } catch { Debug.LogError("Selected object on Editor is not a script or ScriptableObject"); } }
/// <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); } }
// Supports the generation of runtime controllers for AXModels public static void createControllerButtonAction(AXModel model) { GameObject runtimeControllerGO = null; foreach (Transform child in model.transform) { if (child.name == "runtimeController") { runtimeControllerGO = child.gameObject; break; } } if (runtimeControllerGO == null) { // Create one runtimeControllerGO = new GameObject("runtimeController"); runtimeControllerGO.transform.parent = model.gameObject.transform; } string fullPath = ""; if (runtimeControllerGO != null) { AXRuntimeControllerBase runtimeController = runtimeControllerGO.GetComponent <AXRuntimeControllerBase>(); // 1. GET runctimeController filePath... if (runtimeController != null) { // 1A. GET FilePath from Component MonoScript ms = MonoScript.FromMonoBehaviour(runtimeController); string relativeAssetPath = AssetDatabase.GetAssetPath(ms.GetInstanceID()); fullPath = ArchimatixUtils.getAbsoluteLibraryPath(relativeAssetPath); } else { // 1B. Since a controller does not exist, we must create one from a tempalte, update it and the // let AX know that a new class will be available to add to the GameObject after scripts reload. // 1B.1. CREATE file based on a template... fullPath = EditorUtility.SaveFilePanel( "Save New Controller File", ArchimatixUtils.getAbsoluteLibraryPath("Assets"), "MyRuntimeController", "cs"); // 1B.2. LOCATE TEMPLATE file AXRuntimeControllerTemplate.cs DirectoryInfo info = new DirectoryInfo(Application.dataPath); FileInfo[] files = info.GetFiles("AXRuntimeControllerTemplate.cs", SearchOption.AllDirectories); string templateFilePath = ""; if (files != null && files.Length > 0) { templateFilePath = files[0].ToString(); } // 1B.3. COPY TEMPLATE to fullPath if (!string.IsNullOrEmpty(templateFilePath) && File.Exists(templateFilePath)) { File.Copy(templateFilePath, fullPath, true); } // 1B.4. REPLACE the classname string newClassName = System.IO.Path.GetFileNameWithoutExtension(fullPath); File.WriteAllText(fullPath, File.ReadAllText(fullPath).Replace("AXRuntimeControllerTemplate", newClassName)); // 1B.5. SET UP TO CONNECT CLASS TO GAMEOBJECT AFTER ALL SCRIPTS RELOAD // ArchimatixEngine will notice this on DidReloadAllScripts and add this class as a component to the runtimeControllerGO EditorPrefs.SetString("AddComponentByGameObjectIDAndClassname", (runtimeControllerGO.GetInstanceID() + "_" + newClassName)); } // 2. REWRITE the auto-generated region of the file // based on the model's exposed runtime parameters. if (File.Exists(fullPath)) { AXRuntimeEditor.updateControllerFile(fullPath, model); } // 3. Let the user know that this script writing // starts a Reload of all scripts EditorUtility.DisplayDialog("Reloading Scripts", "This may take a few seconds.", "Ok"); // 4. REFRESH DB - This is asynchronous and will reload all scripts. AssetDatabase.Refresh(); } }
private static string AssetPathFor(MonoScript script) { return(AssetDatabase.GetAssetPath(script.GetInstanceID())); }
static void CheckReference(Object obj, bool inHierarchy, Object asset = null) { if (obj == null || obj is Transform) { return; } SerializedObject so = new SerializedObject(obj); SerializedProperty p = so.GetIterator(); do { if (p.propertyType != SerializedPropertyType.ObjectReference || p.objectReferenceValue == null || (inHierarchy && p.propertyPath == "m_GameObject")) { continue; } bool isMatched = false; if (m_IsFindingGameObject) { if (m_SelectedObjects.Contains(p.objectReferenceValue)) { isMatched = true; } } else if (m_IsFindingAsset) { if (p.objectReferenceValue == m_SelectedObj || (m_SelectedIsMainAsset && AssetDatabase.GetAssetPath(p.objectReferenceValue) == m_SelectedObjPath)) { isMatched = true; } } if (isMatched) { if (inHierarchy) { Component c = obj as Component; HierarchyResult r = new HierarchyResult() { component = c, property = p.objectReferenceValue, propertyName = GetPropertyName(c, p) }; if (obj is MonoBehaviour) { MonoScript script = MonoScript.FromMonoBehaviour(obj as MonoBehaviour); string path = AssetDatabase.GetAssetPath(script.GetInstanceID()); if (path.StartsWith("Assets/")) { r.script = script; } } if (r.script == null && r.propertyName.StartsWith("m_")) { r.propertyName = r.propertyName.Substring(2); } SaveHierarchyResult(c.gameObject, r); } else if (asset is SceneAsset || asset is GameObject) { Component c = obj as Component; ProjectResult r = new ProjectResult() { childPath = GetPropertyPath(asset, c, p), propertyName = GetPropertyName(c, p), referenceObject = p.objectReferenceValue }; SaveProjectResult(asset, r); } else { ProjectResult r = new ProjectResult() { childPath = p.propertyPath, propertyName = p.displayName, referenceObject = p.objectReferenceValue }; SaveProjectResult(asset, r); } } }while (p.Next(true)); }