unity的自定义属性检查器,可以让用户通过编辑器的inspector来操作代码.
1. 首先写一个js类,命名为LookAtPoint如:
//让此脚本工作在编辑模式.
@script ExecuteInEditMode
var lookAtPoint:Vector3 = Vector3.zero;
function Update () {
transform.LookAt(lookAtPoint);
}
2. 在Editor目录中建立js文件,命名为LookAtPointEditor,用@CustomEditor属性关联上此编辑器要管理的类.如:
@CustomEditor(LookAtPoint)
class LookAtPointEditor extends Editor{
//覆盖,OnInspectorGUI方法,用于更新Inspector的窗体
function OnInspectorGUI(){
target.lookAtPoint = EditorGUILayout.Vector3Field("Look At Point",target.lookAtPoint);
if(GUI.changed){
EditorUtility.SetDirty(target);
}
}
//覆盖,OnScreenGUI,用于更新Screen窗体
function OnScreenGUI(){
target.lookAtPoint = Handles.PositionHandle(target.lookAtPoint,Quaternion.identity);
if(GUI.changed){
EditorUtility.SetDirty(target);
}
}
}
3. 将LookAtPoint拖放到GameObject上.即可通过GUI来使用LookAtPoint的功能.
unity,脚本的自定义属性检查器
最新推荐文章于 2023-07-29 15:40:10 发布