프로젝트뷰에서 마우스 오른쪽 누르면 나오는 미리정의된 스크립트 템플릿 부분도 커스터마이즈가 가능하다.


만약 윈도우즈를 사용하고 있다면 C:\Program Files\Unity\Editor\Data\Resources\ScriptTemplates 경로에

아래와 같이 템플릿 파일들이 있을 것이다.

여기에 원하는 형식의 템플릿을 만들어 넣고 유니티를 재실행하면 내가 만든 템플릿이 유니티에서 뜰 것이다.

앞의 숫자는 순서라고 생각하면 된다.

내 커스텀스크립트는 C# Script와 Shader 사이에 뜨기를 원했기에 81과 83 사이인 82번으로 설정하였다.


폴더를 구분하기위해서는 파일명에 __ (언더바 2개)를 넣으면 된다.

순서-(폴더__)템플릿이름-생성기본파일명 순으로 저장하면 된다.


스크립트 이름은 기본적으로 #SCRIPTNAME#이라고 되어있는데 그대로 사용하면된다.


----------------------------------------------------------------------------------------


위와 같이 #SCRIPTNAME#처럼 키워드를 추가하려면 아래와 같이 스크립트를 만든다.

UnityEditor.AssetModificationProcessor를 상속받은 클래스를 만든 후

static으로 OnWillCreateAsset함수를 만들면 에디터에서 씬을 저장하거나 스크립트를 만들거나 할시에 이 함수가 불린다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class KeywordReplace : UnityEditor.AssetModificationProcessor
{
    public static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta""");
        // path에 .이 포함되어 있다면 해당 index를 받아옴
        int index = path.LastIndexOf(".");
        // 그렇지않다면 -1이 index로 들어오므로 return
        if (index < 0)
            return;
 
        // .이 포함된 index로 path를 나눠서 확장자가 .cs가 아니면 return
        string file = path.Substring(index);
        if (file != ".cs")
            return;
 
        // 유니티 에셋 데이터베이스 상의 주소를 실제 주소로 변경
        index = Application.dataPath.LastIndexOf("Assets");
        path = Application.dataPath.Substring(0, index) + path;
 
        if (!System.IO.File.Exists(path))
            return;
 
        // 스크립트 내용을 메모리에 불러오기
        string fileContent = System.IO.File.ReadAllText(path);
 
        // 키워드 대체
        fileContent = fileContent.Replace("#DATE#"System.DateTime.Now.ToString("yyyy년 MM월 dd일 tt h시 mm분"System.Globalization.CultureInfo.CreateSpecificCulture("ko-KR")));
        DeveloperInformation information = AssetDatabase.LoadAssetAtPath<DeveloperInformation>("Assets/Editor/DeveloperInformation.asset");
        if (information)
        {
            fileContent = fileContent.Replace("#AUTHOR#", information.Name);
        }
        else
        {
            Debug.LogError("Failed to load DeveloperInformation");
        }
 
        // 대체가 끝나면 다시 파일에 쓰기
        System.IO.File.WriteAllText(path, fileContent);
 
        // 다하고 나면 꼭 호출
        AssetDatabase.Refresh();
    }
}
cs

그러면 해당 경로를 실제 주소로 변경하여 그 주소의 실제 파일을 읽은 후 #DATE#같은 커스텀 키워드를

해당 내용으로 replace 해주면된다.


나는 작성일자와 저자를 표시해주고싶어서 #DATE#와 #AUTHOR#를 만들었고

#DATE#는 System.DateTime.Now를 사용하였는데 오전 오후가 한글로 표시되기를 원해 위의 코드처럼 작성하였다.

#AUTHOR#는 Asset을 하나 만들어서 해당 내용을 읽어와서 띄워주게하였다.


해당 부분은 아래와 같이 CreateAssetMenu를 이용해서 DeveloperInformation이라는 ScriptableObject를 만들었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
[CreateAssetMenu(fileName = "DeveloperInformation", menuName= "DeveloperInformation",order =0)]
public class DeveloperInformation : ScriptableObject
{
    [SerializeField]
    private string name;
 
    public string Name
    {
        get
        {
            return name;
        }
    }
}
 
cs


이렇게 클래스를 만들고나면 에디터상에서 아래와 같이 에셋을 만들 수 있는 메뉴가 뜬다.


이렇게 에셋을 하나 만들어주고 name을 입력해주면 위에서 #AUTHOR#가 해당 name으로 replace될 것이다.


이제 템플릿을 아래와 같이 작성하면 스크립트를 만들었을 시 자동으로 작성일자와 작성자가 들어가게 된다.


Posted by misty_
,