说明
Unity 使用技巧集合会整理和收集 Unity 开发相关的技巧和经验。
本次开发技巧的提供者是独立游戏开发者 Federico Bellucci,本部分内容的翻译已获得他本人授权。
Federico Bellucci 一直在免费提供 Unity 开发技巧和教程,同时也有一些内容需要 Patreon 支持才能获得,如果您喜欢他提供的内容,不妨支持一下。
Federico Bellucci 的相关链接:
TextArea
可以将 string 输入设置为 TextArea,并且可以更改尺寸,更加方便编辑。

Header, Tooltip 和 Space
通过使用 Header, Tooltip 以及 Space,可以使得 Inspector 更加容易阅读,大家平时可能用 Header 会比较多一些吧。

自定义平台参数 #defines
可以自行定义平台的参数,比如“CHEATS”,然后在运行时可以使用特定的代码以符合平台参数,更加便于调试代码。

改变编辑器中拖拽数值的速度
在编辑器中拖拽改变某一数值的时候,同时按下 Alt(变慢)或者 Shift(变快)可以改变数值变化的速度。

MinMax 属性
使用 Federico 自己编写的 MinMax 代码来实现更好的 Min - Max 编辑。

MinMaxAttribute.cs
using UnityEngine;
// ! -> 可以放入除了 Editor 文件夹以外的任何地方
// ! -> Put this anywhere, but not inside an Editor Folder
// By @febucci : https://www.febucci.com/
public class MinMaxAttribute : PropertyAttribute
{
public float minLimit = 0;
public float maxLimit = 1;
public MinMaxAttribute(int min, int max)
{
minLimit = min;
maxLimit = max;
}
}MinMaxDrawer.cs
using UnityEngine;
using UnityEditor;
// ! -> 放入 Editor 文件夹
// ! -> Put this in an Editor folder
// By @febucci : https://www.febucci.com/
[CustomPropertyDrawer(typeof(MinMaxAttribute))]
public class MinMaxDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
//Asserts that we're using the MinMax attribute on a Vector2
UnityEngine.Assertions.Assert.IsTrue(property.propertyType == SerializedPropertyType.Vector2, "Devi usare un vector2 per MinMax");
if (property.propertyType == SerializedPropertyType.Vector2)
{
MinMaxAttribute minMax = attribute as MinMaxAttribute;
//Writes the variable name on the left
Rect totalValueRect = EditorGUI.PrefixLabel(position, label);
//The left value, after the variable name
Rect leftRect = new Rect(totalValueRect.x, totalValueRect.y, 50, totalValueRect.height);
//Rect of the slider
Rect valueRect = new Rect(leftRect.xMax, totalValueRect.y, totalValueRect.width - leftRect.width * 2 - 4, totalValueRect.height);
//The right value
Rect rightRect = new Rect(totalValueRect.xMax - leftRect.width - 2, totalValueRect.y, leftRect.width, totalValueRect.height);
float minValue = property.vector2Value.x; //Current x
float maxValue = property.vector2Value.y; //Current y
EditorGUI.MinMaxSlider(valueRect, ref minValue, ref maxValue, minMax.minLimit, minMax.maxLimit);
//Assigns the value to the property
property.vector2Value = new Vector2(minValue, maxValue);
EditorGUI.LabelField(leftRect, minValue.ToString("F3")); //Writes the value on the left
EditorGUI.LabelField(rightRect, maxValue.ToString("F3")); //Writes the value on the right
}
else
{
GUI.Label(position, "You can use MinMax only on a Vector2!");
}
}
}


刚看到这个很有用的项目, https://github.com/dbrizov/NaughtyAttributes,有一些很好用的Attributes。
最近由 Evade Game 修改于:2020-03-02 14:25:34@Evade Game:404了...
@燃烧殆尽rdw:编辑器把文字误认为链接了, https://github.com/dbrizov/NaughtyAttributes