get set 使用起来很方便,但是编辑时在Inspector视图中问题就来了,因为get/set的属性即使是public了,但是在Inspector视图中依然不显示。
第一种方法
[SerializeField, SetProperty("Number")]
private float number;
public float Number
{
get
{
return number;
}
private set
{
number = Mathf.Clamp01(value);
}
}
第二种方法
TestInspector.cs放在Editor目录下
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
[CustomEditor(typeof(Test))]
public class TestInspector : Editor {
Test model;
public override void OnInspectorGUI(){
model=target as Test;
int width=EditorGUILayout.IntField("Width",model.width);
if(model.width!=width){
model.width=width;
}
base.DrawDefaultInspector();
}
}
Test挂在任意游戏对象上。
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour
{
public int width
{
get {
return _width;
}
set {
Debug.Log("set :" + value);
_width = value;
}
}
private int _width;
}
网友评论