Unity 6.3
0 онлайн 55 гостей 3 в системе
Вход
Руководство по стилю C# для чистого и масштабируемого кода Глава 12 из 13 Оригинал, стр. 58

Приложение: шаблоны скриптов

Appendix: Script templates

Talk is cheap. Show me the code. — Linus Torvalds, creator of Linux and Git

Script templates are predefined code snippets that are used when generating new C# files like monobehaviour or Scriptable Objects in Unity (e.g., via Assets > Create > C# Script). Once you establish formatting rules for your style guide, you can thus configure your script templates to help ensure consistency to your guidelines in your codebase and help reduce some repetitive work. The files can be found here: Windows: C:\Program Files\Unity\Hub\Editor\[UnityVersion]\Editor\Data\Resources\ScriptTemplates Mac: /Applications/Unity/Hub/Editor/[UnityVersion]/Unity.app/Contents/Resources/ScriptTemplates

On macOS, reveal the Unity.app package contents to show the Resources subdirectory. Inside this path, you’ll see the default templates such as: 1-Scripting__MonoBehaviour Script-NewMonoBehaviourScript.cs.txt 2-Scripting__ScriptableObject Script-NewScriptableObjectScript.cs.txt

Whenever you make a new scripted asset in the Project window from the Create menu, Unity uses one of these templates.

If you open the file named 1-Scripting__MonoBehaviour ScriptNewMonoBehaviourScript.cs.txt with a text editor, you will see the following:

C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class #SCRIPTNAME# : MonoBehaviour {
    // Start is called before the first frame update

    void Start() { #NOTRIM# }
    // Update is called once per frame

    void Update() { #NOTRIM# }
}
Note the keywords: —

#SCRIPTNAME#: This is the name you’ve specified for the script. If you don’t customize the name, it uses the default name, e.g., NewBehaviourScript.

#NOTRIM#: This guarantees whitespace, making sure one line appears between the curly braces.

Script templates are customizable. For example, you can add a namespace or remove the default Update method. Modifying the template can save you a few keystrokes every time you create one of these scripted assets. The script template filename follows this pattern: PriorityNumber–MenuPath–DefaultName.FileExtension.txt A dash (-) character separates the different parts of the name: —

PriorityNumber is the order that the script appears in, in the Create menu. Lower numbers have higher priority.

MenuPath allows you to customize how the file appears in the Create menu. You can create categories with the double underscore(__). For example, “CustomScript__Misc__ScriptableObject” creates the menu item ScriptableObject under the Create > CustomScript > Misc menu.

DefaultName is the default name given to the asset if you don’t specify one.

FileExtension is the file extension appended to the asset name.

Also, note that each script template also has a .txt appended to the FileExtension. If you want to apply a script template to a specific Unity project, copy and paste the entire ScriptTemplates folder directly under the project’s Assets: /Assets/ScriptTemplates. Or, you can copy just the sections that you’re editing, if you prefer. Next, create new script templates or modify the originals to fit your preferences. Delete any script templates from the project if you don’t plan on changing them. You can also just copy the files you like to use. You can also change the original script templates in the application resources but exercise caution. That affects all projects using that version of Unity. See this support article for more information about customizing your script templates. Also, check the attached project for a few additional script template examples.