public static void CreateEditorScript(params MonoScript[] selectedScripts)
        {
            if (selectedScripts == null)
            {
                throw new ArgumentNullException(nameof(selectedScripts));
            }

            if (selectedScripts.Length <= 0)
            {
                return;
            }

            using (new ReloadAssembliesLock())
            {
                string[] editorScriptTemplate  = File.ReadAllLines(_editorTemplatePath);
                string   lastCreatedScriptPath = null;

                foreach (var selectedScript in selectedScripts)
                {
                    var  selectedScriptPath         = AssetDatabase.GetAssetPath(selectedScript);
                    var  selectedScriptAssemblyPath = CompilationPipeline.GetAssemblyDefinitionFilePathFromScriptPath(selectedScriptPath);
                    bool isEditorAssemblyRequired   = selectedScriptAssemblyPath != null;

                    var scriptSavePath = PathUtility.GetEditorScriptPath(selectedScriptAssemblyPath, selectedScriptPath);
                    if (!IsOverrideAllowed(scriptSavePath))
                    {
                        continue;
                    }

                    //deep copy to prevent template modifications
                    var editorScriptCode = editorScriptTemplate.ToList();
                    var relatedScript    = AssetDatabase.LoadAssetAtPath <MonoScript>(selectedScriptPath);
                    EditorScriptGenerator.CreateEditorScript(editorScriptCode, scriptSavePath, relatedScript);

                    if (isEditorAssemblyRequired)
                    {
                        AssemblyDefinitionGenerator.UpdateOrCreateAssemblyDefinitionAsset(selectedScriptAssemblyPath, scriptSavePath);
                    }

                    lastCreatedScriptPath = scriptSavePath;
                }

                AssetDatabase.Refresh();
                SelectLastCreatedAsset(lastCreatedScriptPath);
            }
        }
Exemple #2
0
        private static string CreateEditorScript(MonoScript sourceScript, List <string> editorScriptCode)
        {
            var sourceScriptPath   = AssetDatabase.GetAssetPath(sourceScript);
            var sourceAssemblyPath = CompilationPipeline.GetAssemblyDefinitionFilePathFromScriptPath(sourceScriptPath);

            var scriptSavePath = PathUtility.GetEditorScriptPath(sourceAssemblyPath, sourceScriptPath);

            if (File.Exists(scriptSavePath) &&
                !IsOverrideAllowed(scriptSavePath))
            {
                return(null);
            }

            EditorScriptGenerator.CreateEditorScript(editorScriptCode, scriptSavePath, sourceScript);

            bool isEditorAssemblyRequired = sourceAssemblyPath != null;

            if (isEditorAssemblyRequired)
            {
                AssemblyDefinitionGenerator.UpdateOrCreateAssemblyDefinitionAsset(sourceAssemblyPath, scriptSavePath);
            }

            return(scriptSavePath);
        }