"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
}