Unity NaughtyAttributes--InInspector编辑器插件

安装方式

1.github https://github.com/dbrizov/NaughtyAttributes
2. AssetStore https://assetstore.unity.com/packages/tools/utilities/naughtyattributes-129996
常用特性

用法展示

Reorderablelist 数组可重排

public class NaughtyComponent : MonoBehaviour
{
	[ReorderableList]
	public int[] intArray;

	[ReorderableList]
	public List<float> floatArray;
}

Unity NaughtyAttributes--InInspector编辑器插件

Button 按钮

public class NaughtyComponent : MonoBehaviour
{
	[Button]
	private void MethodOne() { }

	[Button("Button Text")]
	private void MethodTwo() { }
}

Unity NaughtyAttributes--InInspector编辑器插件
BoxGroup 方框分组

public class NaughtyComponent : MonoBehaviour
{
	[BoxGroup("Integers")]
	public int firstInt;
	[BoxGroup("Integers")]
	public int secondInt;

	[BoxGroup("Floats")]
	public float firstFloat;
	[BoxGroup("Floats")]
	public float secondFloat;
}

Unity NaughtyAttributes--InInspector编辑器插件

ShowIf / HideIf 显示/隐藏

public class NaughtyComponent : MonoBehaviour
{
	public bool showInt;

	[ShowIf("showInt")]
	public int myInt;

	[ShowIf("AlwaysShow")]
	public float myFloat;

	[ShowIf("NeverShow")]
	public Vector3 myVector;

	public bool AlwaysShow() { return true; }

	public bool NeverShow => false;
}

Unity NaughtyAttributes--InInspector编辑器插件
ShowAssetPreview 显示预制体

public class NaughtyComponent : MonoBehaviour
{
	[ShowAssetPreview]
	public Sprite sprite;

	[ShowAssetPreview(128, 128)]
	public GameObject prefab;
}

Unity NaughtyAttributes--InInspector编辑器插件
Foldout 折叠

public class NaughtyComponent : MonoBehaviour
{
	[Foldout("Integers")]
	public int firstInt;
	[Foldout("Integers")]
	public int secondInt;
}

Unity NaughtyAttributes--InInspector编辑器插件
CurveRange 曲线

public class NaughtyComponent : MonoBehaviour
{
[CurveRange(-1, -1, 1, 1)]
public AnimationCurve curve;

[CurveRange(EColor.Orange)]
public AnimationCurve curve1;

[CurveRange(0, 0, 5, 5, EColor.Red)]
public AnimationCurve curve2;

}
Unity NaughtyAttributes--InInspector编辑器插件EnumFlags 多选枚举

public enum Direction
{
	None = 0,
	Right = 1 << 0,
	Left = 1 << 1,
	Up = 1 << 2,
	Down = 1 << 3
}

public class NaughtyComponent : MonoBehaviour
{
	[EnumFlags]
	public Direction flags;
}

Unity NaughtyAttributes--InInspector编辑器插件

Required 要求对象不为空

public class NaughtyComponent : MonoBehaviour
{
	[Required]
	public Transform myTransform;

	[Required("Custom required text")]
	public GameObject myGameObject;
}

Unity NaughtyAttributes--InInspector编辑器插件
InfoBox 提示盒信息

public class NaughtyComponent : MonoBehaviour
{
	[InfoBox("This is my int", EInfoBoxType.Normal)]
	public int myInt;

	[InfoBox("This is my float", EInfoBoxType.Warning)]
	public float myFloat;

	[InfoBox("This is my vector", EInfoBoxType.Error)]
	public Vector3 myVector;
}

Unity NaughtyAttributes--InInspector编辑器插件EnableIf / DisableIf 是否可以被修改

public class NaughtyComponent : MonoBehaviour
{
	public bool enableMyInt;

	[EnableIf("enableMyInt")]
	public int myInt;

	[EnableIf("Enabled")]
	public float myFloat;

	[EnableIf("NotEnabled")]
	public Vector3 myVector;

	public bool Enabled() { return true; }
	public bool NotEnabled => false;
}

Unity NaughtyAttributes--InInspector编辑器插件

更多用法查看github文档,以及插件里的例子Scene

上一篇:Unity 游戏框架搭建 2019 (四十二) MonoBehaviour 简化


下一篇:Unity中有用的特性(Attributes)