我们想要在编辑模式下在Inspector中编辑属性.
public int width{
get {
return _width;
}
set {
print ("set:" + value);
_width = value;
}
}
[SerializeField]//序列化一个区域,在Inspector视图中显示出来.
private int _width;
在编辑模式下用鼠标修改Width的值,发现set压根就没有执行.因为我们修改的是private _width而不是public width.我们需要的是修改后者.
将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;
}
将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();
}
}
在编辑模式下用鼠标修改width的值,log输出说明get/set已经响应了.
另一种实现方式:这里
ResourceFrom:这里