经过一番百度我找到了两种方法。
一、是EditorWindow.OnHierarchyChang这种要创建一个窗口,而且这个窗口关闭了就失效了,不管那么多来看实现代码:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class HierachyChange : EditorWindow {
[MenuItem("Tools/HierachyChangeWindow")]
static void AddWindow()
{
Rect wr = new Rect(0, 0, 500, 500);
HierachyChange window = (HierachyChange)EditorWindow.GetWindowWithRect(typeof(HierachyChange), wr, true, "widow name");
window.Show();
}
void OnHierarchyChange()
{
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次,改变后保存当前场景还会调用一次");
}
}
该脚本要放在Editor文件夹下。
二、是我在雨松MOMO的一篇文章里看到的,这里我也把他的代码贴出来,有兴趣的可以去他的原文http://www.xuanyusong.com/archives/3053 注明:以下代码出自 雨松MOMO的Unity3D研究院之监听Hierachy、Project等视图结构变化的事件这篇文章:
//这个是基类
using System;
using System.Collections;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[InitializeOnLoad]
public class EditorMonoBehaviour
{
static EditorMonoBehaviour()
{
var type = Types.GetType("UnityEditor.EditorAssemblies", "UnityEditor.dll");
var method = type.GetMethod("SubclassesOf", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(Type) }, null);
var e = method.Invoke(null, new object[] { typeof(EditorMonoBehaviour) }) as IEnumerable;
foreach (Type editorMonoBehaviourClass in e)
{
method = editorMonoBehaviourClass.BaseType.GetMethod("OnEditorMonoBehaviour", BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
method.Invoke(System.Activator.CreateInstance(editorMonoBehaviourClass), new object[0]);
}
}
}
private void OnEditorMonoBehaviour()
{
EditorApplication.update += Update;
EditorApplication.hierarchyWindowChanged += OnHierarchyWindowChanged;
EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
EditorApplication.projectWindowChanged += OnProjectWindowChanged;
EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
EditorApplication.modifierKeysChanged += OnModifierKeysChanged;
// globalEventHandler
EditorApplication.CallbackFunction function = () => OnGlobalEventHandler(Event.current);
FieldInfo info = typeof(EditorApplication).GetField("globalEventHandler", BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
EditorApplication.CallbackFunction functions = (EditorApplication.CallbackFunction)info.GetValue(null);
functions += function;
info.SetValue(null, (object)functions);
EditorApplication.searchChanged += OnSearchChanged;
EditorApplication.playmodeStateChanged += () => {
if (EditorApplication.isPaused)
{
OnPlaymodeStateChanged(PlayModeState.Paused);
}
if (EditorApplication.isPlaying)
{
OnPlaymodeStateChanged(PlayModeState.Playing);
}
if (EditorApplication.isPlayingOrWillChangePlaymode)
{
OnPlaymodeStateChanged(PlayModeState.PlayingOrWillChangePlaymode);
}
};
}
public virtual void Update()
{
}
public virtual void OnHierarchyWindowChanged()
{
}
public virtual void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
}
public virtual void OnProjectWindowChanged()
{
}
public virtual void ProjectWindowItemOnGUI(string guid, Rect selectionRect)
{
}
public virtual void OnModifierKeysChanged()
{
}
public virtual void OnGlobalEventHandler(Event e)
{
}
public virtual void OnSearchChanged()
{
}
public virtual void OnPlaymodeStateChanged(PlayModeState playModeState)
{
}
public enum PlayModeState
{
Playing,
Paused,
Stop,
PlayingOrWillChangePlaymode
}
}
下面是子类他实现了上面的虚函数。
using UnityEditor;
using UnityEngine;
public class NewBehaviourScript : EditorMonoBehaviour
{
public override void Update()
{
//Debug.Log ("每一帧回调一次");
}
public override void OnPlaymodeStateChanged(PlayModeState playModeState)
{
//Debug.Log ("游戏运行模式发生改变, 点击 运行游戏 或者暂停游戏或者 帧运行游戏 按钮时触发: " + playModeState);
}
public override void OnGlobalEventHandler(Event e)
{
//Debug.Log ("全局事件回调: " + e);
}
public override void HierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
// Debug.Log (string.Format ("{0} : {1} - {2}", EditorUtility.InstanceIDToObject (instanceID), instanceID, selectionRect));
}
public override void OnHierarchyWindowChanged()
{
Debug.Log("层次视图发生变化");
}
public override void OnModifierKeysChanged()
{
// Debug.Log ("当触发键盘事件");
}
public override void OnProjectWindowChanged()
{
// Debug.Log ("当资源视图发生变化");
}
public override void ProjectWindowItemOnGUI(string guid, Rect selectionRect)
{
//根据GUID得到资源的准确路径
//Debug.Log (string.Format ("{0} : {1} - {2}", AssetDatabase.GUIDToAssetPath (guid), guid, selectionRect));
}
}
把这两个类放在Editor文件夹里就行了。