用代码控制unityeditor的暂停/播放状态


 1 public enum PlayModeState
 2  {
 3      Stopped,
 4      Playing,
 5      Paused
 6  }
 7  
 8  [InitializeOnLoad]
 9  public class EditorPlayMode
10  {
11      private static PlayModeState _currentState = PlayModeState.Stopped;
12  
13      static EditorPlayMode()
14      {
15          EditorApplication.playmodeStateChanged = OnUnityPlayModeChanged;
16      }
17  
18      public static event Action<PlayModeState, PlayModeState> PlayModeChanged;
19  
20      public static void Play()
21      {
22          EditorApplication.isPlaying = true;
23      }
24  
25      public static void Pause()
26      {
27          EditorApplication.isPaused = true;
28      }
29  
30      public static void Stop()
31      {
32          EditorApplication.isPlaying = false;
33      }
34  
35  
36      private static void OnPlayModeChanged(PlayModeState currentState, PlayModeState changedState)
37      {
38          if (PlayModeChanged != null)
39              PlayModeChanged(currentState, changedState);
40      }
41  
42      private static void OnUnityPlayModeChanged()
43      {
44          var changedState = PlayModeState.Stopped;
45          switch (_currentState)
46          {
47              case PlayModeState.Stopped:
48                  if (EditorApplication.isPlayingOrWillChangePlaymode)
49                  {
50                      changedState = PlayModeState.Playing;
51                  }
52                  break;
53              case PlayModeState.Playing:
54                  if (EditorApplication.isPaused)
55                  {
56                      changedState = PlayModeState.Paused;
57                  }
58                  else
59                  {
60                      changedState = PlayModeState.Stopped;
61                  }
62                  break;
63              case PlayModeState.Paused:
64                  if (EditorApplication.isPlayingOrWillChangePlaymode)
65                  {
66                      changedState = PlayModeState.Playing;
67                  }
68                  else
69                  {
70                      changedState = PlayModeState.Stopped;
71                  }
72                  break;
73              default:
74                  throw new ArgumentOutOfRangeException();
75          }
76  
77          // Fire PlayModeChanged event.
78          OnPlayModeChanged(_currentState, changedState);
79  
80          // Set current state.
81          _currentState = changedState;
82      }
83  
84  }

以上代码来自stephenlautier  

http://answers.unity3d.com/questions/447701/event-for-unity-editor-pause-and-playstop-events.html

调试程序时,触发状态后EditorApplication.isPaused = true;,编辑器暂停,很方便.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM