UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplate C# (CSharp) Method

CreateScriptAssetFromTemplate() static private method

static private CreateScriptAssetFromTemplate ( string pathName, string resourceFile ) : Object
pathName string
resourceFile string
return Object
        internal static Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
        {
            string fullPath = Path.GetFullPath(pathName);
            string contents = File.ReadAllText(resourceFile).Replace("#NOTRIM#", "");
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
            contents = contents.Replace("#NAME#", fileNameWithoutExtension);
            string newValue = fileNameWithoutExtension.Replace(" ", "");
            contents = contents.Replace("#SCRIPTNAME#", newValue);
            if (char.IsUpper(newValue, 0))
            {
                newValue = char.ToLower(newValue[0]) + newValue.Substring(1);
                contents = contents.Replace("#SCRIPTNAME_LOWER#", newValue);
            }
            else
            {
                newValue = "my" + char.ToUpper(newValue[0]) + newValue.Substring(1);
                contents = contents.Replace("#SCRIPTNAME_LOWER#", newValue);
            }
            UTF8Encoding encoding = new UTF8Encoding(true);
            File.WriteAllText(fullPath, contents, encoding);
            AssetDatabase.ImportAsset(pathName);
            return AssetDatabase.LoadAssetAtPath(pathName, typeof(Object));
        }

Usage Example

示例#1
0
        private void CreateScript()
        {
            var basePath     = Path.Combine(EditorApplication.applicationContentsPath, kResourcesTemplatePath);
            var templatePath = Path.Combine(basePath, "81-C# Script-NewBehaviourScript.cs.txt");

            ProjectWindowUtil.CreateScriptAssetFromTemplate(TargetPath(), templatePath);
            AssetDatabase.Refresh();
        }
All Usage Examples Of UnityEditor.ProjectWindowUtil::CreateScriptAssetFromTemplate