CustomEditor

自定义Inspector和Scene view的一些行为

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]//这个标签表示Update方法在编辑器下也可以执行,不一定非要在游戏运行状态下才能执行
public class LookAtPoint : MonoBehaviour {

    public Vector3 lookAtPoint = Vector3.zero;

    // Update is called once per frame
    void Update () {
        transform.LookAt (lookAtPoint);
    }
}

自定义脚本面板的参数设置

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

//定义一个自定义编辑器,指向LookAtPoint类
[CustomEditor(typeof(LookAtPoint))]
public class LookAtPointEditor : Editor {

    //实现Inspector参数设置
    void OnInspectorGUI () {
        LookAtPoint lp = target as LookAtPoint;
        lp.lookAtPoint = EditorGUILayout.Vector3Field ("Look at point", lp.lookAtPoint);
        if (GUI.changed) {
            EditorUtility.SetDirty (target);
        }
    }

    //实现Scene参数设置
    void OnSceneGUI () {
        LookAtPoint lp = target as LookAtPoint;
        lp.lookAtPoint = Handles.PositionHandle (lp.lookAtPoint, Quaternion.identity);
        if (GUI.changed) {
            EditorUtility.SetDirty (target);
        }
    }
}

results matching ""

    No results matching ""