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

Unity编辑器之inspector处理

2016-04-08 10:57 1051 查看
<pre name="code" class="html">using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(MaterialsChangeContainer))] //需要编辑的脚本控件
public class MaterialsChangeContainerEditor : Editor
{
private static GUIContent deleteContent = new GUIContent("Del");
private static GUIContent addContent = new GUIContent("Add");
private SerializedProperty mList;
void OnEnable()
{
//FindProperty找到类中的某个变量
mList = serializedObject.FindProperty("MaterialsChangeInfoList");//MaterialsChangeInfoList 脚本里面的list变量
}

public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.PropertyField(mList.FindPropertyRelative("Array.size"));//显示数组长度
for (int i = 0; i < mList.arraySize; i++)
{
EditorGUILayout.BeginHorizontal();
SerializedProperty prop = mList.GetArrayElementAtIndex(i);
SerializedProperty innerOld = prop.FindPropertyRelative("OldMaterials");
SerializedProperty innerNew = prop.FindPropertyRelative("NewMaterials");
Material matOld = innerOld.objectReferenceValue as Material;
Material matNew = innerNew.objectReferenceValue as Material;
EditorGUILayout.PropertyField(prop, new GUIContent(string.Format("{0}     to     {1}", matOld.name, matNew.name)), prop.isExpanded);
if (GUILayout.Button(deleteContent, EditorStyles.miniButtonRight, GUILayout.Width(40)))
{
mList.DeleteArrayElementAtIndex(i);
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button(addContent))
{
mList.InsertArrayElementAtIndex(mList.arraySize);
}
EditorGUILayout.EndHorizontal();
serializedObject.ApplyModifiedProperties();
}
}
常用函数
var Obj = serializedObject.targetObject as ClassTest;//获得这个类
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: