# Generalフォルダに入っているコードの設計 ## SceneController ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; //シーン管理に必須 public static class SceneController { public static void ChangeSceneToTitle() { SceneManager.LoadScene("Title"); Debug.Log("Titleに移動しました"); } public static void ChangeSceneToScene01() { SceneManager.LoadScene("Scene01"); Debug.Log("Scene01に移動しました"); } public static void ChangeSceneToScene02() { SceneManager.LoadScene("Scene02"); Debug.Log("Scene02に移動しました"); } public static void ChangeSceneToScene03() { SceneManager.LoadScene("Scene03"); Debug.Log("Scene03に移動しました"); } public static void ChangeSceneToScene04() { SceneManager.LoadScene("Scene04"); Debug.Log("Scene04に移動しました"); } } /*シーンの追加方法*/ ///1,Unityエディタ左上のFileからNew Sceneを選択 ///2,Unityエディタ左上のFileからSave as...でScenesフォルダにSceneを保存 ///3,Unityエディタ左上のBuild settingsからAdd open scenesで現在のシーンを追加する ///4,ここに関数を書く(static修飾子を忘れずに!) ///5,クラス名.関数名でシーンを呼び出す ``` #### 特記事項 * staticなクラスなので、どこからでも参照できます。 ## PureSingleton ```csharp= using System; using System.Dynamic; public abstract class PureSingleton<T> : IDisposable where T : class, new() { private static T instance = null; //Instanceのシンタックスシュガー(糖衣構文) 要するに、シンプルでわかりやすく書き換えること //Iを介してインスタンスを取得することができる public static T I => Instance; public static T Instance { get { createInstance(); return instance; } } //インスタンスを生成する public static void createInstance() { if(instance == null) { instance = new T(); } } //Singletonが存在するか public static bool isExists() { return instance != null; } //Singletonを破棄する public virtual void Dispose() { instance = null; } } /* reference -> https://qiita.com/Cova8bitdot/items/29b7064c7472a6f34972 */ /* * Tにクラス名を入れて継承する */ ``` #### UML図 ```mermaid classDiagram direction RL class IDisposable{ <<interface>> +onClick() void } class SceneButton{ <<abstract>> -sceneName : string +onClick() void } IDisposable <|.. SceneButton ``` #### 特記事項 * 抽象クラスのため、実体を作成してください。 ## SingletonWithMonoBehaviour ```csharp= using System; using UnityEngine; using UnityEngine.Assertions; //ジェネリックであるTYpeはMonoBehaviourを継承したクラスを指定 //IDisposableはinterface public abstract class SingletonWithMonoBehaviour<TYpe> : MonoBehaviour, IDisposable where TYpe : MonoBehaviour { private static TYpe instance; public static TYpe Instance { get //instanceがnull出ないことをアサートし、インスタンスを返す { //アサート(Assert)は、プログラムの実行時に特定の条件が成り立っていることを検証するための仕組み //今回はnullの場合、エラーを表示しプログラムの実行を停止する。 Assert.IsNotNull(instance, "There is no object attached" + typeof(TYpe).Name); return instance; } } //一番最初に自動で実行される private void Awake() { if (instance != null && instance.gameObject != null) { Destroy(this.gameObject); return; } instance = this as TYpe; onAwakeProcess(); } //Singletonが存在するか public static bool isExist() { return instance != null ? true : false; } //Destroy処理時に実行する private void onDestroy() { if (instance != (this as TYpe)) return; onDestroyProcess(); Dispose(); } //自身を破棄する public virtual void Dispose() { if(isExist() == true) { instance = null; } } /*この関数をいじって追加処理を加える*/ //Destroy処理時の追加処理を実行 protected virtual void onDestroyProcess() { } //派生先での初期化処理を実行 protected virtual void onAwakeProcess() { } } /* reference -> https://qiita.com/Cova8bitdot/items/29b7064c7472a6f34972 */ /*実装例 *using System; *using UnityEngine; *using UnityEngine.Assertions; * *public class SoundManager : SingletonMonoBehaviour<SoundManager> *{ * (中略) *} */ /* * IDisposableは、リソースの開放やクリーンアップを行うためのインターフェイス。 * GC(ガベージコレクタ)がオブジェクトを解放するまでの間、アンマネージドリソース(ネットワーク接続など)が解放されることを保証する */ ``` #### UML図 ```mermaid classDiagram direction RL class IDisposable{ <<interface>> +onClick() void } class SceneButton{ <<abstract>> -sceneName : string +onClick() void } IDisposable <|.. SceneButton ``` #### 特記事項 * 抽象クラスのため、実体を作成してください
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up