美术有可以直接在Editor下操作Transform,我想去修正他们编辑的数值,所以我就得监听Transform。
C#
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
[CustomEditor(typeof(Transform))]
public class TransformEditor :Editor {
[InitializeOnLoadMethod]
static void IInitializeOnLoadMethod()
{
TransformEditor.onPostion =delegate(Transform transform) {
Debug.Log(string.Format("transform = {0} positon = {1}",transform.name,transform.localPosition));
};
TransformEditor.onRotation =delegate(Transform transform) {
Debug.Log(string.Format("transform = {0} rotation = {1}",transform.name,transform.localRotation.eulerAngles));
};
TransformEditor.onScale =delegate(Transform transform) {
Debug.Log(string.Format("transform = {0} scale = {1}",transform.name,transform.localScale));
};
}
public delegate void Change(Transform transform);
static public Change onPostion;
static public Change onRotation;
static public Change onScale;
private Editor editor;
private Transform transform;
private Vector3 startPostion =Vector3.zero;
private Vector3 startRotation =Vector3.zero;
private Vector3 startScale =Vector3.zero;
void OnEnable()
{
transform = target as Transform;
editor = Editor.CreateEditor(target, Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.TransformInspector",true));
startPostion = transform.localPosition;
startRotation = transform.localRotation.eulerAngles;
startScale = transform.localScale;
}
public override void OnInspectorGUI ()
{
editor.OnInspectorGUI();
if(GUI.changed)
{
if(startPostion != transform.localPosition)
{
if(onPostion != null)
onPostion(transform);
}
if(startRotation != transform.localRotation.eulerAngles)
{
if(onRotation != null)
onRotation(transform);
}
if(startScale != transform.localScale)
{
if(onScale != null)
onScale(transform);
}
startPostion = transform.localPosition;
startRotation = transform.localRotation.eulerAngles;
startScale = transform.localScale;
}
}
}