private bool DrawCreateScriptButton() { if (GUILayout.Button("Create Script")) { string thisPath = AssetDatabase.GetAssetPath(this); //string initialPath = Path.GetDirectoryName(thisPath); string fileName = Path.GetFileNameWithoutExtension(thisPath).Replace(" Udon C# Program Asset", "").Replace(" ", "").Replace("#", "Sharp"); string chosenFilePath = EditorUtility.SaveFilePanelInProject("Save UdonSharp File", fileName, "cs", "Save UdonSharp file"); if (chosenFilePath.Length > 0) { string fileContents = UdonSharpSettings.GetProgramTemplateString(Path.GetFileNameWithoutExtension(chosenFilePath)); File.WriteAllText(chosenFilePath, fileContents); AssetDatabase.ImportAsset(chosenFilePath, ImportAssetOptions.ForceSynchronousImport); AssetDatabase.Refresh(); sourceCsScript = AssetDatabase.LoadAssetAtPath <MonoScript>(chosenFilePath); return(true); } } return(false); }
private static void CreateUSharpScript() { string folderPath = "Assets/"; if (Selection.activeObject != null) { folderPath = AssetDatabase.GetAssetPath(Selection.activeObject); if (Selection.activeObject.GetType() != typeof(UnityEditor.DefaultAsset)) { folderPath = Path.GetDirectoryName(folderPath); } } else if (Selection.assetGUIDs.Length > 0) { folderPath = AssetDatabase.GUIDToAssetPath(Selection.assetGUIDs[0]); } folderPath = folderPath.Replace('\\', '/'); string chosenFilePath = EditorUtility.SaveFilePanelInProject("Save UdonSharp File", "", "cs", "Save UdonSharp file", folderPath); if (chosenFilePath.Length > 0) { string chosenFileName = Path.GetFileNameWithoutExtension(chosenFilePath).Replace(" ", "").Replace("#", "Sharp"); string assetFilePath = Path.Combine(Path.GetDirectoryName(chosenFilePath), $"{chosenFileName}.asset"); if (AssetDatabase.LoadAssetAtPath <UdonSharpProgramAsset>(assetFilePath) != null) { if (!EditorUtility.DisplayDialog("File already exists", $"Corresponding asset file '{assetFilePath}' already found for new UdonSharp script. Overwrite?", "Ok", "Cancel")) { return; } } string fileContents = UdonSharpSettings.GetProgramTemplateString(chosenFileName); File.WriteAllText(chosenFilePath, fileContents); AssetDatabase.ImportAsset(chosenFilePath, ImportAssetOptions.ForceSynchronousImport); MonoScript newScript = AssetDatabase.LoadAssetAtPath <MonoScript>(chosenFilePath); UdonSharpProgramAsset newProgramAsset = ScriptableObject.CreateInstance <UdonSharpProgramAsset>(); newProgramAsset.sourceCsScript = newScript; AssetDatabase.CreateAsset(newProgramAsset, assetFilePath); AssetDatabase.Refresh(); } }