第02章 标准编辑器扩展 Standard Editor Extensions

原地址: http://anchan828.github.io/editor-manual/web/part2-standardextension.html

使用预制的附加特性

2.1 更改 inspector 外观

01. Range

这是一个允许使用滑动条更改 int, float, long 和 double 数值的函数
在开发的时候, 可以使我们 public 的一些字段控制在一定范围, 而不是随意的修改值.
操作

  1. 附加下面脚本到游戏物体.
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
    [Range(1, 10)]
    public int num1;

    [Range(1, 10)]
    public float num2;

    [Range(1, 10)]
    public long num3;

    [Range(1, 10)]
    public double num4;
}

对比

未添加 Range 和添加 Range 对比

02. Multiline / TextArea

文本区默认是一行的, 但是更改为多行文本区, Multiline 和 TextAreal 几乎拥有相同的功能, 但是 Multiline 有些限制, 比如: 没有自动换行适应, 没有滚动条.
推荐使用 TextAreal, 除非有特殊需求.
操作

  1. 附加脚本
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
    [Multiline(5)]
    public string multiline;
    [TextArea(3, 5)]
    public string textArea;
}

效果

普通文本, Multiline 和 TextArea 对比

2.2 Adding functions handled by Inspector

01. ContextMenuItem

inspector 中, 在 public 字段处添加一个 context menu.
必须想要单独充值一个字段值, 那么此处会用到..

`在字段处单击右键弹出`

操作

  1. 添加如下代码
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    [ContextMenuItem ("Random", "RandomNumber")]
    [ContextMenuItem ("Reset", "ResetNumber")]
    public int number;

    void RandomNumber ()    {
        number = Random.Range (0, 100);
    }

    void ResetNumber ()  {
        number = 0;
    }
}
02. ColorUsage

We use a color picker to change color. ColorUsage enables you to enable / disable alpha use in the color picker or change it to a color picker for HDR.

左: 默认颜色选择器, 中: 不带 alpha, 右: HDR 选择器

操作

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public Color color1;

    [ColorUsage (false)]
    public Color color2;

    [ColorUsage (true, true, 0, 8, 0.125f, 3)]
    public Color color3;
}

2.3 调整 inspector 显示.

此小结并不会取直接修改属性, 却会使它看起来更工整写.

01. Header

如图:

image.png

操作:

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

public class HeaderTest : MonoBehaviour {

    [Header("__ PlayerSettings __")]
    public Player player;
    [Serializable]
    public class Player
    {
        public string name;
        [Range(0, 100)]
        public int hp;
    }
    [Header("__ GameSettings __")]
    public Color background;
}

02. Space

给两个字段设置边距.
如图:

image.png

操作:

public class SpaceTest : MonoBehaviour {
    public string str1;
    [Space(5)]
    public string str2;
    [Space(10)]
    public string str3;
}
03. Tooltip

你也可以给字段添加一段描述..


image.png
public class ToolTipTest : MonoBehaviour {
    [Tooltip("__ display Tooltip __")]
    public long toolTip;
}
04. 在 inspector 中隐藏字段
image.png
public class NewBehaviourScript : MonoBehaviour
{
    public string str1;

    [HideInInspector]
    public string str2;
}

2.4 让 inspector 更方便些..

01. RequireComponent

当在脚本组件中需要 Animator 组件的时候, 假如忘记了添加 Animator, 那就会出错, 此时可以使用 RequireComponent 提前防止此错误.
添加使用 RequireComponent 附加的脚本时候也会自动添加, 如果已经添加, 那什么都不会做, 当尝试删除这个组件时候, 就会弹出"无法删除"对话框.

当尝试删除 Animator 的时候会有弹窗提示.
[RequireComponent(typeof(Animator))]
public class NewBehaviourScript : MonoBehaviour
{
    Animator animator;

    void Awake ()
    {
        animator = GetComponent ();
    }
}
02. DisallowMultipleComponent (禁用重复组件)

这是一个禁止将相同组件添加到游戏物体的特性..


尝试附加相同脚本时会显示一个对话框
[DisallowMultipleComponent]
public class Base : MonoBehaviour
{
}
03. Formerly SerializedAs

在更改变量名的时候, 将变量值转到新的变量..
在 Inspector 中现实 SerializeField 的字段. 如果在这儿修改了字段名, 那么数据也会丢失.

public class SerializeAsTest : MonoBehaviour {
    [SerializeField]
    string hoge;
}

在 inspector 中输入值


image.png

将字段名从 hoge 更改为 fuga

public class SerializeAsTest : MonoBehaviour {
    [SerializeField]
    string fuga;
}
~

使用 FormerlySeriallizedAs

using UnityEngine;
using UnityEngine.Serialization;

public class NewBehaviourScript : MonoBehaviour
{
    [SerializeField]
    [FormerlySerializedAs("hoge")]
    string fuga;
}

The point to watch out here is that changing the variable name, adding the FormerlySerializedAs attribute, and specifying oldName must be done at the same time . Data that is not needed by compiling the script is discarded, and even if you changed the variable name (compiled for the first time), you forgot to specify FormerlySerializedAs and even if you add hastily (compile the second time) It has been destroyed. This is because data is discarded in the second compilation.

04. AddComponentMenu

在"Component" 菜单中添加项
所有的脚本/组件都会在 component 中进行分组, 如果我们像把自己定义的脚本组件归类到 component 中, 使用 AddComponentMenu;

[AddComponentMenu("MyUI/MyTweenColor")]
public class AddComponentMenuTest : MonoBehaviour {
}
image.png

此时选中游戏物体, 点击菜单就会添加我们自定义的脚本组件.
如果未选中游戏无图, 菜单是灰色未激活的状态..

2.5 Ease the development of games

01. 在 Editor 模式下 执行

即使游戏未运行, 那么也可以执行 继承Monobehaviour的脚本.
测试后, 脚本添加到游戏物体时候会进行执行, update 会执行多次,
当有脚本更改后, Unity 进行编译, Update 会再次执行.

using UnityEngine;

[ExecuteInEditMode]
public class NewBehaviourScript : MonoBehaviour
{
    [Range(0,10)]
    public int number;
    void Awake ()    {
        Debug.Log ("Awake");
    }
    void Start (){
        Debug.Log ("Start");
    }
    void Update ()    {
        Debug.Log ("Update");
    }
}
02. ContextMenu;

在组件的 Context Menu 中执行 方法,


image.png
public class ContextMenuTest : MonoBehaviour {
    [Range(0, 10)]
    public int number = 0;

    [ContextMenu("MyRandomNumber")]
    void RandomNumber()    {
        number = Random.Range(0, 10);
    }

    [ContextMenu("MyResetNumber")]
    void ResetNumber()    {        
        number = 0;
    }
}
03. SelectionBase

在场景中选择游戏物体时使用它, 指定要选择的游戏物体, 或者游戏物体的选择顺序,

  1. 新建空游戏物体,
  2. 新建子物体 Cube
    此时:


    当在场景中选择游戏物体时候, 默认选择 cube

给 GameObject 空游戏物体附加脚本

[SelectionBase]
public class NewBehaviourScript : MonoBehaviour
{
}

此时再点击选择场景中的 cube 游戏物体, 会优先选择 Cube 的父物体.再次点击 cube Hierarchy 中才会选中 Cube 游戏物体.

你可能感兴趣的:(第02章 标准编辑器扩展 Standard Editor Extensions)