[Unity] Script Tempate 2 – Script Create Dialog


1. Download the Plugins - Script Create Dialog
2. Fix complile errors
3. Create scripts by wizard
4. Learn the method of building script template

1. Download the Plugins - Script Create Dialog

Go to AssetStore and search by keyword "Script Create Dialog", and you can find the free plugin published by Unity Technologies.

[Unity] Create Script Dialog

2. Fix compile errors

You may meet compile errors below:
`UnityEditorInternal.InternalEditorUtility' does not contain a definition for `AddScriptComponentUnchecked'

[Unity] Create Script Dialog

Let's fix it:

    private void Create()

        if (CanAddComponent())
            // Need to use reflection to access this now (it is internal)
            MethodInfo addScriptMethod = typeof(InternalEditorUtility).GetMethod(
                BindingFlags.Static | BindingFlags.NonPublic);
            addScriptMethod.Invoke(null, new Object[] {m_GameObjectToAddTo,
            AssetDatabase.LoadAssetAtPath(TargetPath(), typeof (MonoScript)) as MonoScript});

3. Create scripts by wizard

Now you can using wizard to create scripts.
Assets => Create => Script...

[Unity] Create Script Dialog

This wizard can let you custom the class name, inherit class, save path, default functions...etc. And it also support ScriptableObject, CustomEditor, EditorWindow!

[Unity] Create Script Dialog

4. Learn the method of building script template

Let's take a look of MonoBehaviour template.

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

public class $ClassName : MonoBehaviour {

The first line BASECLASS=MonoBehaviour is mainly for recognize by EditorWindow, you can find the usage in NewScriptWindow.cs

private string GetBaseClass(string templateContent)
    string firstLine = templateContent.Substring(0, templateContent.IndexOf("\n"));
    if (firstLine.Contains("BASECLASS"))
        string baseClass = firstLine.Substring(10).Trim();
        if (baseClass != string.Empty)
            return baseClass;
    return null;

Another two keywords $ClassName and $Functions, can be found in NewScriptGenerator.cs - ToString() function.

m_Text = m_ScriptPrescription.m_Template;
m_Writer = new StringWriter ();
m_Writer.NewLine = "\n";
// Make sure all line endings are Unix (Mac OS X) format
m_Text = Regex.Replace (m_Text, @"\r\n?", delegate(Match m) { return "\n"; });
// Class Name
m_Text = m_Text.Replace ("$ClassName", ClassName);
m_Text = m_Text.Replace ("$NicifiedClassName", ObjectNames.NicifyVariableName (ClassName));

歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: ,