Unity属性窗口编辑器
2016-10-14 16:09
375 查看
Unity中允许用户对脚本的属性进行自定义编辑。
serializedproperty是获取编辑属性的通用方法,如://获取属性
serializedObject.FindProperty("age");
下面放代码,将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
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
相关文章推荐
- Unity编辑器中Status窗口详解
- 【Unity编辑器】UnityEditor多重弹出窗体与编辑器窗口层级管理
- unity编辑器对对象属性的编辑
- 拓展编辑器功能--Unity自定义编辑器窗口
- Qt编程之实现属性窗口编辑器
- unity 编辑器扩展 创建一个窗口
- Unity 3D编辑器扩展介绍、教程(二) —— 创建窗口
- Unity创建自定义窗口编辑器
- Unity之编辑器类将摄像机视图(targetTexture)绘制在窗口中
- 在Unity内制作工具(1)--MenuItem,Resouces.FindObjectsofTypeAll<>与EditorWindow自制编辑器窗口
- [Unity] Unity3D研究院编辑器之独立Inspector属性
- 【Unity】编辑器属性
- Unity编辑器拓展【视图窗口扩展】
- Unity编辑器中Status窗口详解
- Unity编辑器拓展【自定义窗口】
- unity 编辑器扩展 扩展摄像机的属性查看器
- unity编辑器学习,创建自己的窗口
- 20_Shell语言―――VIM编辑器基础知识三之窗口属性定制、配置文件及查找替换功能
- Unity 自定义编辑器窗口的使用
- Unity烂笔头1-自定义INSPECTOR属性窗口节点项