In the game, the program, art, planning and even sound are the division of labor. Many times, for the unity3d in a pile of English, everyone will see very depressed. In particular, different programmers, naming methods are not the same, or even use pinyin. Therefore, adding some Chinese display in the script will be a good solution to this problem.
First, Unity has a good Chinese display method [Header] Label for fields (field)
Like what
Using Unityengine;
public class Testscript:monobehaviour
{
[Header ("Variable a")]
public float A;
}
Shown below
But for [Header], it doesn't natively support labeling on non-fields, so there's nothing you can do to show a description of a class or a description of a class function.
In this case, we can create a custom new label Monoheaderattribute, which inherits from the header, and restricts only the classes and functions. Of course, we can also not inherit the header, we can write a tag that inherits only from attribute. The idea is the same. The former is used here.
Using system;using unityengine; [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method)]public class monoheaderattribute:headerattribute{public Monoheaderattribute (string Header): Base (header) {}}
Then create an editor directory in Unity, create a display script Monodescriptioneditor in the editor directory, inherit from editor, and set the customeditor tag to Monobehaviour. Note that the second argument of Customeditor must be true to indicate that it is valid for subclasses that inherit from Monobehaviour.
In its Onenable event, the Monoheaderattribute information is obtained through reflection and then displayed in Oninspectorgui. Of course monodescriptioneditor is completely free to define, and the Helpbox is used here to display it.
Using unityeditor;using unityengine; [Customeditor (typeof (Monobehaviour), true)]public class monodescriptioneditor:editor{private string M_res; public void Onenable () {m_res = ""; var type = target. GetType (); var atts = type. GetCustomAttributes (typeof (Monoheaderattribute), true); if (Atts. Length <= 0) return; var att = (monoheaderattribute) atts[0]; M_res = Att.header + "\ n"; var methods = type. GetMethods (); foreach (Var method in methods) {var Matts = method. GetCustomAttributes (typeof (Monoheaderattribute), true); if (Matts. Length > 0) {var matt = (Monoheaderattribute) matts[0]; if (method. DeclaringType = null) M_res + = $ "\n{method. Declaringtype.name}. {method. Name}\n{matt.header}\n "; }}} private static bool S_fold = TRUE; public override void Oninspectorgui () {base. OninspectOrgui (); if (string. IsNullOrEmpty (M_res)) return; var color = Gui.color; Gui.contentcolor = Color.cyan; S_fold = Editorguilayout.foldout (s_fold, "description"); if (s_fold) Editorguilayout.helpbox (M_res, messagetype.info); Gui.contentcolor = color; }}
Go back to Testscript and add some test code. On class and on some functions, some monoheader are added.
Using Unityengine; [Monoheader ("This is a test script: Testscript")]public class testscript:monobehaviour{ internal void Start () { } [Monoheader ("This is a Test function")] public void TestA () { } [Monoheader ("second function")] public void TESTB () { } [Header ("Variable A")] public float A;
The final results are shown below
(PS: Try to use Monoheader to replace the header to ensure unity, but unity for the field only to recognize the header, ultimately no effect, so monoheader for classes and methods of description, the description of the field with the header)
Add script in the Unity editor Chinese description