Unity - Editor Extensions

Foreword

For many extension method Unity editor, the paper starts with some of the most commonly used methods, such as expansion of Inspector script column, the expansion of the top menu bar, complete with illustrations describes its usage.

Much of this paper finishing from independent game developers - indienova book Unity to use skill sets , but only a select few of the most commonly used method, and implemented on their own projects.

Project Address: UnityEditor - SouthBegonia

This article is for learning exchanges, removed immediately if there is any infringement.

Scripts column extension

The method of the extension portion of the script in the Inspector panel concentrated, mainly in standardized script code for visual variable column, facilitation

[Header] property title

Subsequent preparation of a title area code, and an overview of the region for distinguishing Code Meaning

[Header("武器")]
public int weapon;
public int ammunition;
public int aurability;

[Tooltip] property tips

When implemented in the Inspector, the mouse is over the variable name, suggesting that the description of the variable

[Tooltip("玩家的名字,肯定不再是JOJO对吧")]
public string playerName;

[Space] blank line Properties

Create a blank line in the script Inspector page at spaced vertical and horizontal viewing parameters

[Space]
public int health = 100;

[The Range ()] attribute range values

Such that the value of the variable can be modified only within this range, and may be presented slidably modify variables in effect page Inspector

[Range(0, 1000)]
public int exp = 0;

[Foldout] Folding property

Implemented so that the plurality of variables set, foldable effect Inspector page. (Note: This method is not Unity comes, but from the project InspectorFoldoutGroup - PixeyeHQ , To use this method, only need to configure the script project to the next item to their unity)

[Pixeye.Unity.Foldout("Enemys")]
public GameObject a, b, c, d, e;

[SerializeField] forced serialization

Make private variables visible in the Inspector script pages, but also known as mandatory serialization

[SerializeField]
private int coins;

[HideInInspector] Hide property

Making public variables are not visible in the Inspector page, thus achieving protection variables

[HideInInspector]
public int maxHealth = 100;

[The TextArea] input field

For longer word strings, which expand the size of the editing area in the Inspector (originally only a single line, and can not wrap)

[TextArea]
public string gameDescribe = "";

[AddComponentMenu] add components to the menu

Written before the class name, you can directly add the class to Add Component menu

[AddComponentMenu("Managers/demo1")]
public class demo1 : MonoBehaviour
{
	// ...
}

The OnValidate () data checking

Function edit mode, the data entered Inspector checking the

private void OnValidate()
{
    if (health < 0)
    {
        Debug.LogError("生命值不可为负");
        health = 0;
    }        
    else if (health > 100)
    {
        Debug.LogError("生命值不可超过最大值 " + maxHealth);          
        health = 100;
    }        
}

[The ContextMenu] Context Menu

The class can add a contextual pop-up menu in the Inspector on the current page script right (or click the script icon to the right three vertical dots) to pop up the custom context menu

[ContextMenu("显示当前生命值")]
public void PrintHealth()
{
    Debug.Log("Health = " + health);
}

Extended menu bar

In edit mode, you can visit the top menu bar item customized for specific functions that extend the menu bar item

[MenuItem("调试/查看版本信息")]
static void PrintSomething()
{
    // 注意:仅有静态函数才可使用该属性
    Debug.Log("当前Unity版本:" + Application.unityVersion);
    
}

reference

Guess you like

Origin www.cnblogs.com/SouthBegonia/p/12637261.html