get set 方法

1问题描述:在inspector面板中修改一个参数的值,但是set并没有执行,及面板中修改的值没有放到set中。

随便挂在一个物体之上就行

using UnityEngine;
using System.Collections;

public class GetSet : MonoBehaviour
{
 public int width
 {
  get {
   return _width;
  }
  set {
   Debug.Log("set :" + value);//通过输出来鉴别是否修改成功了
   _width = value;
  }
 }
    private int _width;
}

2.使用unity自带的editor把值写出在面板中,并更新它的值,这样就可以实现面板中和set中的值同步了

using UnityEngine;
using UnityEditor;
using System.Collections;


[CustomEditor(typeof(GetSet))]
public class TestInspector : Editor
{
    GetSet gt;
    public override void OnInspectorGUI()
    {
        gt = target as GetSet;
        int width = EditorGUILayout.IntField("Width", gt.width);
        if(gt.width != width)
        {
            gt.width = width;
        }
        base.OnInspectorGUI();
    }

}

原文地址:https://www.cnblogs.com/xwwFrank/p/4434238.html