Unity,C#初心者です。
現在、シーンマネージャークラスを作っており、
最終的には「RuntimeInitializeOnLoadMethod」で
常駐してシーンを管理してもらうことを想定しています。
テスト用に現在作っているシーンマネージャーのメンバであるAsyncOparationが
遷移先(内部で行うシーン遷移)でNullになってしまっているようで、
(もしかするとマネージャークラス自体の問題かもしれません…)
試行錯誤してみても、結果が変わらず困っております。
C#
1// using UnityEngine.SceneManagement は付けています。 2class Scene_Manager { 3 4 public class SceneState{ 5 6 protected Scene_Manager scene_Manager; 7 public SceneState( 8 Scene_Manager scene_Manager, 9 string sceneName){ 10 this.sceneName = sceneName; 11 this.scene_Manager = scene_Manager; 12 } 13 public string sceneName; 14 public virtual void Init() { } 15 public virtual void Run() { } 16 public virtual void End() { } 17 } 18 19 public SceneState state; 20 public AsyncOperation Async; 21 22 public Scene_Manager(){ 23 Async = new AsyncOperation(); 24 state = new Title(this); 25 } 26 27 /* シーンの各ステート*/ 28 // タイトルクラス 29 class Title : SceneState{ 30 31 public Title(Scene_Manager scene_Manager) : 32 base(scene_Manager, "Title") { } 33 34 public override void Init() { } 35 public override void Run(){ } 36 public override void End() 37 { 38 scene_Manager.state = new Main(scene_Manager); 39 } 40 } 41 // ゲームメインクラス 42 class Main : SceneState{ 43 44 public Main(Scene_Manager scene_Manager) : 45 base(scene_Manager, "Main") {} 46 47 public override void Init(){ 48 scene_Manager.LoadScene(this.sceneName); 49 } 50 public override void Run(){ 51 scene_Manager.StatScene(); 52 } 53 public override void End(){ 54 scene_Manager.state = new Result(scene_Manager); 55 } 56 } 57 // リザルトクラス 58 class Result : SceneState { 59 60 public Result(Scene_Manager scene_Manager) : 61 base(scene_Manager, "Result") { } 62 63 public override void Init(){ 64 scene_Manager.LoadScene(this.sceneName); 65 } 66 public override void Run(){ 67 scene_Manager.StatScene(); 68 } 69 public override void End() 70 { 71 } 72 } 73 // シーンの読み込み 74 public IEnumerator LoadScene(string sceceName) 75 { 76 yield return 77 Async = SceneManager.LoadSceneAsync 78 (sceceName, LoadSceneMode.Additive); 79 // 非同期読み込みするも、自動切替は行わない 80 Async.allowSceneActivation = false; 81 } 82 // 読み込み後の任意のタイミングでのスタート 83 public IEnumerator StatScene() 84 { // 読み込みが完了したら切り替える 85 while (Async.progress < 1.0f){ 86 yield return null; 87 } 88 Async.allowSceneActivation = true; 89 } 90 public void StateChange() 91 { 92 state.Init(); 93 state.Run(); 94 state.End(); 95 } 96 97}
こんなお粗末なコードで大変恐縮ではございますが、
なにか原因が分かりましたら、お教えいただけると幸いです。
また、「こうした方がいい」というアドバイスなどもあれば、
とても助かります。
どうぞ、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。