转载:http://blog.csdn.net/liqiangeastsun/article/details/42173029
一般情况下,在Inspector面板上只能显示public类型的基本变量如 float int string 等
例如 Rect Color Texture等类型的,声明为 public也不会显示出来,下面利用编辑器类来让不能显示的类型显示在Inspector面板
如下显示了 Color, AnimationCurve, Rect,Texture
点击 颜色跳出如下 窗口来修改颜色
点击曲线跳出如下窗口
可以点击曲线的灰色部分右键, 点击 Add Key 添加一个点,然后调整位置
本例创建两个脚本,一个在Editor文件夹下 创建为 InspectorTest
另一个不放在Editor文件夹下 Test
Test脚本如下
using UnityEngine;
using System.Collections;
public class Test : MonoBehaviour {
public Color myColor;
public AnimationCurve curveX = AnimationCurve.Linear(0, 0, 10, 10);
public AnimationCurve curveY = AnimationCurve.Linear(0, 0, 10, 10);
public AnimationCurve curveZ = AnimationCurve.Linear(0, 0, 10, 10);
public Rect myRect;
public Texture myTexture;
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.A))
{
print("MyColor "+myColor); //打印颜色
print("curveX "+ curveX.Evaluate(Time.time)); //利用时间获取曲线的值
print("curveY "+curveY.Evaluate(Time.time)); //利用时间获取曲线的值
print("curveZ "+ curveZ.Evaluate(Time.time));
print("myRect "+myRect); //打印 Rect的 x ,y ,width, height
print("myTexture "+myTexture); //打印Texture
}
//根据三个曲线,实时的获取他们的值,来设置该对象的位置
transform.position = new Vector3(curveX.Evaluate(Time.time), curveY.Evaluate(Time.time), curveZ.Evaluate(Time.time));
}
}
放在Editor文件夹中的 InspectorTest 脚本
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(Test))]
public class InspectorTest : Editor {
public override void OnInspectorGUI() //该方法会绘制在Inspector面板上
{
Test myTest = (Test)target;
//颜色
myTest.myColor = EditorGUILayout.ColorField("New Color", myTest.myColor);
//曲线
myTest.curveX = EditorGUILayout.CurveField("Animation On X", myTest.curveX);
myTest.curveY = EditorGUILayout.CurveField("Animation On Y", myTest.curveY);
myTest.curveZ = EditorGUILayout.CurveField("Animation On Z", myTest.curveZ);
// Rect
myTest.myRect = EditorGUILayout.RectField("窗口 ", myTest.myRect);
//Texture
myTest.myTexture = EditorGUILayout.ObjectField("tie tu", myTest.myTexture, typeof(Texture), true) as Texture;
}
}