美文网首页
Unity中,基本代码模板GameSys

Unity中,基本代码模板GameSys

作者: 全新的饭 | 来源:发表于2023-07-26 14:03 被阅读0次

    在Launch中初始化GameSys
    在GameSys中初始化游戏中各具体内容(系统、管理器……)

    Launch.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Launch : MonoBehaviour
    {
        private void Start()
        {
            Init();
        }
    
        private void OnDestroy()
        {
            MyDestroy();
        }
    
        private void Init()
        {
            GameSys.Init();
        }
    
        private void MyDestroy()
        {
            GameSys.Instance?.MyDestroy();
        }
    
        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
        private static void CreateLaunch()
        {
            var go = new GameObject(nameof(Launch)).AddComponent<Launch>();
        }
    }
    

    GameSys.cs

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class GameSys
    {
        public static GameSys Instance { get; private set; }
    
        private GameSys()
        {
            // 初始化游戏内各具体内容
        }
    
        public static void Init()
        {
            if (Instance == null)
            {
                Instance = new GameSys();
            }
        }
    
        public void MyDestroy()
        {
            Instance = null;
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity中,基本代码模板GameSys

          本文链接:https://www.haomeiwen.com/subject/cnekpdtx.html