Open3

[Unity] デザインパターン

gonCgonC

スクリプタブルオブジェクト

ScriptableObjectSample
[CreateAssetMenu(menuName = "CreateDataSO", fileName = "Data")]
    public class DataSO : ScriptableObject
    {
        public List<HogeHoge> hogehogeList = new List<HogeHoge>();

        [Serializable]
        public class HogeHoge
        {
            public string name;
            public int id;
        }
    }
DataBaseManager
public class DataBaseManager : MonoBehaviour
    {
        public static DataBaseManager instance;
        public DataSO dataSO;

        private void Awake()
        {
            if (instance == null)
            {
                instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }
gonCgonC

オブジェクトプール

ObjectPool
using System.Collections;
using UnityEngine;
using UnityEngine.Pool;

public class ObjectPool : MonoBehaviour
{
    ObjectPool<GameObject> _sampleObjectPool;

    [SerializeField] private GameObject _sampleObjectPrefab;

    public static ObjectPool instance;

    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
        InitSampleObjectPool();
    }

    private void InitSampleObjectPool()
    {
        _sampleObjectPool = new ObjectPool<GameObject>(
            OnCreateSampleObject,  // createFunc
            OnTakeFromPool,        // actionOnGet
            OnReturnedToPool,      // actionOnRelease
            OnDestroyPoolObject,   // actionOnDestroy
            true,                  // collectionCheck
            10,                    // defaultCapacity
            30                     // maxSize
        );
    }

    void OnTakeFromPool(GameObject go)
    {
        go.SetActive(true);
    }
    void OnReturnedToPool(GameObject go)
    {
        go.SetActive(false);
    }
    void OnDestroyPoolObject(GameObject go)
    {
        Destroy(go);
    }
    GameObject OnCreateSampleObject()
    {
        return Instantiate(_sampleObjectPrefab, transform);
    }

    public GameObject GetSampleObject(Vector3 position)
    {
        GameObject obj = _sampleObjectPool.Get();
        Transform tf = obj.transform;
        tf.position = position;
        return obj;
    }
    public IEnumerator ReleaseSampleObject(GameObject obj)
    {
        yield return new WaitForSeconds(2.0f);
        _sampleObjectPool.Release(obj);
    }
}
gonCgonC

MVP

Model
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.SceneManagement;
using System;
using UniRx;

namespace Test
{
    public class Model : MonoBehaviour
    {
        // イベントを発行する核となるインスタンス
        private ReactiveProperty<int> _timeReactiveProperty = new ReactiveProperty<int>();
        // イベントの購読側だけを発行
        public IObservable<int> OnTimeChanged => _timeReactiveProperty;
        private int MaxTime => 300;

        void Start()
        {
            _timeReactiveProperty.Value = MaxTime;
        }
       
        private IEnumerator PlayTimer()
        {
            while (_timeReactiveProperty.Value > 0)
            {
                _timeReactiveProperty.Value--;
                
                if (_timeReactiveProperty.Value < 1)
                {
                    Time.timeScale = 0;
                }
                yield return new WaitForSeconds(1);
            }
        }
View
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UniRx;

namespace Test
{
    public class View : MonoBehaviour
    {
        [SerializeField]
        private Text txtTimer;

        [SerializeField]
        private Model model ;

        private void Start()
        {
            raidBattleManager.OnTimeChanged.Subscribe(time => UpdateFleetRaidBattleTimer(time)).AddTo(raidBattleManager.gameObject);
        }

        private void Updateimer(int time)
        {
            string m = ((int)time / 60).ToString("00");
            string s = ((int)time % 60).ToString("00");
            txtTimer.text = string.Format("{0}:{1}", m, s);
        }
    }
}