LoginSignup
17
13

More than 5 years have passed since last update.

エディットモード中に実行せずに一定時間おきに更新処理を行う方法

Posted at

スクリプトから動的にモデルの頂点をいじったりする処理を作っていましたが…
実行しないと確認できない…。
実行中にパラメータ調整しても停止したら消える…。
と、非常に作業効率が悪かったので、エディットモード中に実行せずに更新処理を行って確認する処理を作ってみました。

要点

  • クラス宣言の上に [ExecuteInEditMode] をつける(エディットモード中にスクリプト実行する属性)
  • EditorApplication.update デリゲートに更新用メソッドを追加する。
  • 更新用メソッドの中で経過時間を見て、更新処理を行う。
  • EditorApplication.update だけでは画面の書き換えは発生しないので、強制的に SceneView.RepaintAll() を呼ぶ。

サンプル

#if UNITY_EDITOR
   static double waitTime = 0;

   void OnEnable()
   {
       waitTime = EditorApplication.timeSinceStartup;
       EditorApplication.update += EditorUpdate;
   }

   void OnDisable()
   {
       EditorApplication.update -= EditorUpdate;
   }

   // 更新処理
   void EditorUpdate()
   {
       foreach (var go in Selection .gameObjects)
       {
           // 選択中のオブジェクトのみ更新
           if (go == this.gameObject)
           {
               // 1/60秒に1回更新
               if (( EditorApplication.timeSinceStartup - waitTime) >= 0.01666f )
               {
                   // 君だけの更新処理を書こう!
                   UpdateFunc();

                   SceneView.RepaintAll(); // シーンビュー更新
                   waitTime = EditorApplication.timeSinceStartup;
                   break;
               }
           }
       }
   }
#endif

おまけ

応用すればエディターウィンドウ上で動くテトリスとか作れるかもしれませんね。
https://www.assetstore.unity3d.com/jp/#!/content/2796

17
13
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
17
13