纳金网

标题: 官方样例之如何自定义Inspetor [打印本页]

作者: may    时间: 2015-10-17 23:59
标题: 官方样例之如何自定义Inspetor

要对Editor进行编辑,unity要求对此进行操作的脚本必须放在Editor文件夹内,创建一个Editor文件夹和一个CustomEditor文件夹,创建一个LevelScript在CustomEditor内,编辑脚本如下:

UnityEngine;
using System.Collections;
public class LevelScript : MonoBehaviour
{
    public int experience;
   
    public int Level
    {
        get{ return experience / 750; }}//确保exprience只读
}

接着,在Editor文件夹下创建一个LevelScriptEditor,编辑脚本如下:

using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(LevelScript))]
public class LevelScriptEditor : Editor
{
    public override void OnInspectorGUI()//重写OnInspectorGUI方法
    {
        LevelScript myTarget = (LevelScript)target;
        
        myTarget.experience = EditorGUILayout.IntField("Experience", myTarget.experience);
        EditorGUILayout.LabelField("Level", myTarget.Level.ToString());
    }
}






欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5