您的位置:首页 > 移动开发 > Unity3D

Unity属性窗口编辑器

2016-10-14 16:09 375 查看
    Unity中允许用户对脚本的属性进行自定义编辑。


serializedproperty是获取编辑属性的通用方法,如://获取属性
serializedObject.FindProperty("age");
//显示属性,类型会自动获取
EditorGUILayout.PropertyField(ageProp, new GUIContent("age"));
//应用修改项
serializedObject.ApplyModifiedProperties();
//属性需要一直更新
serializedObject.Update();

下面放代码,将RoleController.cs挂在物体上,将EditorInspector.cs放在工程的Editor目录:

using UnityEngine;
using System.Collections;

public class RoleController :MonoBehaviour
{

public string roleName = "asadasd";
public int age;
public float range;
public float jumpHight;
public Texture2D pic;
public string picName;
public moveType ac;
public bool isBoy;

}

public enum moveType
{
jump,
move,
attack
}

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(RoleController))]
[CanEditMultipleObjects]
public class EditorInspector : Editor
{
private RoleController role;

bool toggle;
SerializedProperty ageProp;
SerializedProperty nameProp;
SerializedProperty picProp;
SerializedProperty rangeProp;
SerializedProperty boolProp;
SerializedProperty enumProp;
void OnEnable()
{
// Setup the SerializedProperties.
ageProp = serializedObject.FindProperty("age");
nameProp = serializedObject.FindProperty("roleName");
picProp = serializedObject.FindProperty("pic");
rangeProp = serializedObject.FindProperty("range");
boolProp = serializedObject.FindProperty("isBoy");
enumProp = serializedObject.FindProperty("ac");
}

public override void OnInspectorGUI()
{
serializedObject.Update();

role = target as RoleController;
GUILayout.Space(6f);

//role.age = EditorGUILayout.IntSlider("Age", role.age, 0, 100);
//role.roleName = EditorGUILayout.TextField("角色名字", role.roleName);

EditorGUILayout.PropertyField(ageProp, new GUIContent("age"));
EditorGUILayout.PropertyField(nameProp, new GUIContent("name"));
EditorGUILayout.PropertyField(boolProp, new GUIContent("isBoy"));
EditorGUILayout.PropertyField(enumProp, new GUIContent("enum"));

if (EditorGUILayout.Foldout(toggle, "折叠"))
{
EditorGUILayout.PropertyField(picProp, new GUIContent("pic"));
}

EditorGUILayout.Slider(rangeProp, 0, 100, new GUIContent("range"));
ProgressBar(rangeProp.floatValue/100, "range");

serializedObject.ApplyModifiedProperties();
}

// Custom GUILayout progress bar.
void ProgressBar(float value, string label)
{
// Get a rect for the progress bar using the same margins as a textfield:
Rect rect = GUILayoutUtility.GetRect(18, 18, "TextField");
EditorGUI.ProgressBar(rect, value, label);
EditorGUILayout.Space();
}

}



只是一个基础模版,大伙可以自行扩展。TKY
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: