码迷,mamicode.com
首页 > 其他好文 > 详细

自定义脚本模板

时间:2019-01-11 11:26:45      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:inter   encode   byte   efi   tac   custom   using   customize   .sh   

两种方法:

1)在unity安装目录下的\Editor\Data\Resources\ScriptTemplates\81-C# Script-NewBehaviourScript.cs.txt即为脚本模板,直接修改成自己需要的即可

2)第二种方法来自宣雨松的《unity3D游戏开发的》第二版,即通过扩展编辑器实现:

第一步:unity工程中新建Editor目录,并放入如下所示脚本:

using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;

public class CustomizedScriptTemplate : MonoBehaviour
{
    //脚本模板所目录
    private const string MY_SCRIPT_DEFAULT = "Assets/Editor/ScriptTemplates/C# Script-NewBehaviourScript.cs.txt";

    [MenuItem("Assets/Create/C# CustomizedScript", false, 80)]
    public static void CreatMyScript()
    {
        string locationPath = GetSelectedPathOrFallback();
        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0,
        ScriptableObject.CreateInstance<MyDoCreateScriptAsset>(),
        locationPath + "/NewBehaviourScript.cs",
        null, MY_SCRIPT_DEFAULT);
    }

    public static string GetSelectedPathOrFallback()
    {
        string path = "Assets";
        foreach (UnityEngine.Object obj in Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets))
        {
            path = AssetDatabase.GetAssetPath(obj);
            if (!string.IsNullOrEmpty(path) && File.Exists(path))
            {
                path = Path.GetDirectoryName(path);
                break;
            }
        }
        return path;
    }
}

class MyDoCreateScriptAsset : EndNameEditAction
{

    public override void Action(int instanceId, string pathName, string resourceFile)
    {
        UnityEngine.Object o = CreateScriptAssetFromTemplate(pathName, resourceFile);
        ProjectWindowUtil.ShowCreatedAsset(o);
    }

    internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
    {
        string fullPath = Path.GetFullPath(pathName);
        StreamReader streamReader = new StreamReader(resourceFile);
        string text = streamReader.ReadToEnd();
        streamReader.Close();
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
        //替换文件名
        text = Regex.Replace(text, "#NAME#", fileNameWithoutExtension);
        bool encoderShouldEmitUTF8Identifier = true;
        bool throwOnInvalidBytes = false;
        UTF8Encoding encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier, throwOnInvalidBytes);
        bool append = false;
        StreamWriter streamWriter = new StreamWriter(fullPath, append, encoding);
        streamWriter.Write(text);
        streamWriter.Close();
        AssetDatabase.ImportAsset(pathName);
        return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
    }
}

第二步:在第一步的Editor下新建目录ScriptTemplate目录,并放入脚本模板,脚本模板如下所示:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class #NAME# : MonoBehaviour {

    void MyFunction()
    {
        
    }
}

 目录结构如下所示:

技术分享图片

 

然后就可以右键Create创建自定义脚本模板了:

技术分享图片

 

自定义脚本模板

标签:inter   encode   byte   efi   tac   custom   using   customize   .sh   

原文地址:https://www.cnblogs.com/llstart-new0201/p/10253742.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!