LoginSignup
1
2

More than 5 years have passed since last update.

【Unity Editor拡張】スクリプトからゲームを再生させたい

Last updated at Posted at 2017-03-31

Editor拡張のメニューからスクリプトを実行してプレイモードに移行したい時がある。

例えば別のシーンからの遷移が前提で、そのシーン再生前に必要なデータをオブジェクトに受け渡す必要があったり、データをリフレッシュする必要があったりする場合だ。

自分の場合は、マップをエディタで編集して即、プレイして動作を確認したかったので実装した。

EditorApplication.ExecuteMenuItem("Edit/Play")

でプレイボタンを押さずともゲームを実行できる

今回は[DEMO PLAY]と言う名前でContextMenuを追加する

qiita.cs
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

namespace Sample
{
    [ExecuteInEditMode]
    public class SampleClass : MonoBehaviour
    {
        [ContextMenu ("DEMO PLAY")]
        void StartDemoPlay ()
        {
            # 任意の再生前の初期化処理などを記述
       ・
       ・
       ・

            # スクリプトでゲームを再生させる
            EditorApplication.ExecuteMenuItem("Edit/Play");
        }
    }
}
#endif

こちらにプレイ以外のステップ実行や停止などもまとまっておりました
スクリプトからエディタを再生、停止、ポーズ、再開 Unityエディタ拡張

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2