UnityTips
本文收集一些实用的Unity Tips
设置在非播放情况下编译脚本
可以在Preferences->General中设置
OnDrawGizmosSelected
您可以使用OnDrawGizmosSelected()来对所选GameObject进行调试。
Debug.Log的第二个参数可以用来定位是哪个GameObject调用的该方法
Debug.Break()可以暂停编辑器
Inspector中的变量标签 Header, Tooltip and Space
对任意类扩展方法
可以对任意类进行扩展方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
public SpriteRenderer sr;
public Material mat;
void Start()
{
sr.transform.ResetTransform();
sr.color = sr.color.ChangeAlpha(.3f);
mat.color = mat.color.ChangeAlpha(.4f);
}
}
public static class Extension
{
public static void ResetTransform(this Transform transform)
{
transform.localPosition = Vector3.zero;
transform.localScale = Vector3.one;
transform.localRotation = Quaternion.identity;
}
public static Color ChangeAlpha(this Color color, float alpha)
{
color.a = alpha;
return color;
}
}
Built-in shaders
下载 Unity’s built-in shaders (including all CGinc files) Unity’s Archive download page
MinMax Attribute
最小值和最大值属性,可以用来选取一段中间值
OnValidate
变量的合法性检测与修改
Rich Text
You can use rich text in Unity’s console.
Read more about Rich Text.
ContextMenu
使用ContextMenu属性从Inspector调用方法。可以在Inspector中的组件菜单中直接调用方法。
SerializeField & HideInInspector
通过使用属性SerializeField和HideInInspector选择要显示和序列化的变量。
FormerlySerializedAs
在修改一个Inspector面板上的变量名时,使用FormerlySerializedAs标签定义之前的名称,可以防止修改时的赋值丢失问题。
Read more about FormerlySerializedAs attribute.
AddComponentMenu
使用AddComponentMenu属性在检查器的“ AddComponent”菜单中组织脚本。
Read more about AddComponent.