kingBook

导航

Unity 侦听进入播放模式、Unity加载时初始化编辑器类

"Editor"文件夹下的PlayModeStateChangedHandler.cs

using UnityEngine;
using UnityEditor;
//允许在 Unity 加载时初始化编辑器类,无需用户操作。
[InitializeOnLoadAttribute]
public static class PlayModeStateChangedHandler{
	
	//初始化类时,注册事件处理函数
	static PlayModeStateChangedHandler(){
		EditorApplication.playModeStateChanged+=OnPlayerModeStateChanged;
	}

	private static void OnPlayerModeStateChanged(PlayModeStateChange playModeState){
		Debug.LogFormat("state:{0} will:{1} isPlaying:{2}",playModeState,EditorApplication.isPlayingOrWillChangePlaymode,EditorApplication.isPlaying);
	}
	//当点"Play"按钮进入播放模式时
	//输出:
	//state:ExitingEditMode will:True isPlaying:False
	//state:EnteredPlayMode will:True isPlaying:True
}

posted on 2020-01-07 12:14  kingBook  阅读(769)  评论(0编辑  收藏  举报