DLFramwork框架(Unity3d)

DLFramwork是一个全面的游戏开发框架,包括系统管理、UI、事件、游戏逻辑、音频、数据存储等多个模块。ISys是系统基类,GameBase是游戏基类,UISys是UI管理核心,而EventModules和GameModules则分别负责事件管理和游戏逻辑。此外,框架还集成了音频管理、数据存储以及DOTween和Json解析等插件,提供手势输入支持。
摘要由CSDN通过智能技术生成


本专栏的所有游戏都是基于这个框架实现!

SysModules(系统管理模块)

这是主要的系统管理模块,是使用所有系统的入口!

ISys (系统基类)

这是所有系统的基类,所有系统都是继承于它!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    public class ISys
    {
        public ISys()
        {
            _InitSys();
        }
        public virtual void _InitSys() { }
        public virtual void _UpdateSys() { }
    }
}

GameBase (游戏基类)

这是游戏基类,游戏所有的使用mono模块都建议继承它!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    public class GameBase : MonoBehaviour
    {
        public virtual void _Init() { }
        public virtual void _OnStart() { }
        public virtual void _FixedUpdate() { }
        public virtual void _Update() { }
        public virtual void _LateUpdate() { }
        public virtual void _Destroy() { }
        public virtual void _OnApplicationQuit() { }
        public virtual void _OnApplicationPause() { }
        public virtual void _OnMouseDown() { }
        public virtual void _OnMouseDrag() { }
        public virtual void _OnMouseUp() { }
        public virtual void _OnApplicationFocus() { }

        public void OnApplicationFocus(bool focus)
        {
            _OnApplicationFocus();
        }
        public void OnApplicationPause(bool pause)
        {
            _OnApplicationPause();
        }

        public void OnMouseDown()
        {
            _OnMouseDown();
        }

        public void OnMouseDrag()
        {
            _OnMouseDrag();
        }

        public void OnMouseUp()
        {
            _OnMouseUp();
        }

        public void Awake()
        {
            _Init();
        }
        public void Start()
        {
            _OnStart();
        }
        public void FixedUpdate()
        {
            _FixedUpdate();
        }
        public void Update()
        {
            _Update();
        }
        public void LateUpdate()
        {
            _LateUpdate();
        }
        public void OnDestroy()
        {
            _Destroy();
        }

        public void OnApplicationQuit()
        {
            _OnApplicationQuit();
        }
    }
}

ObjectBase (对象基类)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    public class ObjectBase : GameBase
    {
        public void DestoryObj(GameObject go)
        {
            Destroy(go);
        }

        public GameObject InstantiateGO(GameObject go)
        {
            GameObject gameObject = Instantiate(go);
            return gameObject;
        }

        public void StartCortinue(IEnumerator enumerator)
        {
            StartCoroutine(enumerator);
        }
    }
}

SysManager (系统管理类)

系统管理类,管理所有的其他系统

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    /// <summary>
    /// 系统类型
    /// </summary>
    public enum SysEnum
    {
        /// <summary>
        /// 声音系统
        /// </summary>
        AudioSys,
        /// <summary>
        /// 事件系统
        /// </summary>
        EventSys,
        /// <summary>
        /// UI系统
        /// </summary>
        UISys,
        /// <summary>
        /// 存储系统
        /// </summary>
        GameDataSys,
        /// <summary>
        /// 游戏系统
        /// </summary>
        GameSys,
        /// <summary>
        /// 对象池系统
        /// </summary>
        PoolSys,
        /// <summary>
        /// 配置表Excel系统
        /// </summary>
        ExcelSys,
        /// <summary>
        /// 输入
        /// </summary>
        InputSys
    }

    public static class SysManager
    {
        private static Dictionary<SysEnum, ISys> Sys = new Dictionary<SysEnum, ISys>();
        private static GameObject SysGame;
        public static ObjectBase ObjectBase;

        /// <summary>
        /// 初始化系统
        /// </summary>
        public static void InitSys()
        {
            if (SysGame == null)
            {
                Debug.Log("初始化系统!");
                SysGame = new GameObject();
                SysGame.name = "LDFramwork";
                SysGame.AddComponent<ObjectBase>();
                ObjectBase = SysGame.GetComponent<ObjectBase>();
            }
        }

        /// <summary>
        /// 加載系统
        /// </summary>
        public static void LoadSys<T>(SysEnum sysEnum) where T : ISys, new()
        {
            T t = new T();
            foreach (SysEnum key in Sys.Keys)
            {
                if (key == sysEnum)
                {
                    Debug.Log("已經加載" + sysEnum.ToString() + "系統");
                    return;
                }
            }
            Debug.Log("加載" + sysEnum.ToString() + "系統");
            Sys.Add(sysEnum, t);
        }

        /// <summary>
        /// 获取当前系统
        /// </summary>
        /// <param name="sysEnum"></param>
        public static T GetSys<T>(SysEnum sysEnum) where T : ISys, new()
        {
            ISys sys = new T();
            foreach (SysEnum item in Sys.Keys)
            {
                if (item == sysEnum)
                {
                    Sys.TryGetValue(item, out sys);
                    return sys as T;
                }
            }
            return sys as T;
        }       

        /// <summary>
        /// 更新系统
        /// </summary>
        public static void UpdateSys()
        {
            foreach (ISys item in Sys.Values)
            {
                item._UpdateSys();
            }
        }
    }
}

UISys (UI管理类)

IView (界面基类)

这是UI的界面基类,所有游戏的界面都需要继承此基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace DLBASE
{
    public class IView : MonoBehaviour
    {
        ItemView itemView;
        public virtual void Init()
        {

        }

        public virtual void ShowItemView<T>(T t,IData data) where T : ItemView
        {
            if (itemView != null)
            {
                itemView.Show();
                itemView.RefreshData(data);
                return;
            }
            itemView = (ItemView)t;
            itemView.Init();
            itemView.Show();
            itemView.RefreshData(data);
        }

        public virtual void CloseItemView<T>()where T : ItemView
        {
            if (itemView != null)
            {
                itemView.Close();
                return;
            }
        }

        public virtual void RefreshData(IData data)
        {
        }

        public virtual void Show()
        {
            gameObject.SetActive(true);
        }


        public virtual void Close()
        {
            gameObject.SetActive(false);
        }
    }
}

ItemView (子页面基类)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    public class ItemView : MonoBehaviour
    {
        public virtual void Init()
        {

        }

        public virtual void RefreshData(IData data)
        {

        }
        public virtual void Show()
        {
            gameObject.SetActive(true);
        }


        public virtual void Close()
        {
            gameObject.SetActive(false);
        }
    }
}

IData (数据基类)

所有数据类都继承此类,才能用于页面的数据通信

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    //界面通信的接口
    public interface IData
    {
    }
}

UISys (UI系统)

UI系统中心,所有关于UI的控制都由此发出

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// UI界面管理类
/// </summary>
namespace DLBASE
{
    public class UISys : ISys
    {
        private  Dictionary<string, IView> m_IViews = new Dictionary<string, IView>();
        /// <summary>
        /// 父物体
        /// </summary>
        public GameObject m_UIRoot;

        /// <summary>
        /// UI相机
        /// </summary>
        public Camera m_UICamera;

        public override void _InitSys()
        {
            m_UIRoot = new GameObject();
            m_UIRoot.name = "UIRoot";
            Canvas uiroot= m_UIRoot.AddComponent<Canvas>();
            CanvasScaler canvasScaler= m_UIRoot.AddComponent<CanvasScaler>();
            canvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
            canvasScaler.referenceResolution = new Vector2(Screen.width, Screen.height);
            m_UIRoot.AddComponent<GraphicRaycaster>();
            uiroot.renderMode = RenderMode.ScreenSpaceCamera;
            GameObject go = new GameObject();
            go.name = "UICamera";
            m_UICamera = go.AddComponent<Camera>();
            m_UICamera.clearFlags = CameraClearFlags.Depth;
            m_UICamera.cullingMask = 7<<5;
            uiroot.worldCamera = m_UICamera;
        }

        /// <summary>
        /// 分发事件
        /// </summary>
        public  void OpenView<T>(string name, IData data = null) where T : IView
        {
            IView view;
            foreach (string item in m_IViews.Keys)
            {
                if (item == name)
                {
                    m_IViews.TryGetValue(item, out view);
                    view.Show();
                    view.RefreshData(data);
                    return;
                }
            }
            GameObject go =  Object.Instantiate(Resources.Load("UI/" + name) as GameObject);
            go.transform.SetParent(m_UIRoot.transform, false);
            view = go.GetComponent<IView>();
            view.Init();
            view.name = name;
            view.RefreshData(data);
            m_IViews.Add(name, view);
        }

        /// <summary>
        /// 关闭界面
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        public  void CloseView<T>(string name) where T : IView
        {
            IView view;
            foreach (string item in m_IViews.Keys)
            {
                if (item == name)
                {
                    m_IViews.TryGetValue(item, out view);
                    view.Close();
                    return;
                }
            }
        }

        /// <summary>
        /// 获取指定界面
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name"></param>
        /// <returns></returns>
        public  T GetView<T>(string name) where T : IView
        {
            IView view;
            foreach (string item in m_IViews.Keys)
            {
                if (item == name)
                {
                    m_IViews.TryGetValue(item, out view);
                    return (T)view;
                }
            }
            return (T)new IView();
        }
    }
}

EventModules (事件管理)

事件管理系统

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 事件管理类
/// </summary>
namespace DLBASE
{
    public class EventSys : ISys
    {
        public delegate void EventDelget(IData data);
        public Dictionary<string, List<EventDelget>> m_Dictionary = new Dictionary<string, List<EventDelget>>();

        /// <summary>
        /// 派发事件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="data"></param>
        public void TriggerEvent(string name, IData data = null)
        {
            List<EventDelget> eventDelget;
            foreach (string key in m_Dictionary.Keys)
            {
                if (key == name)
                {
                    //已经注册该事件
                    m_Dictionary.TryGetValue(key, out eventDelget);
                    for (int i = 0; i < eventDelget.Count; i++)
                    {
                        eventDelget[i]?.Invoke(data);
                    }
                    return;
                }
            }
        }

        /// <summary>
        /// 监听事件
        /// </summary>
        /// <param name="name"></param>
        /// <param name="eventDelget"></param>
        public void AddListionter(string name, EventDelget callback)
        {
            List<EventDelget> eventDelget;
            foreach (string key in m_Dictionary.Keys)
            {
                if (key == name)
                {
                    m_Dictionary.TryGetValue(key, out eventDelget);
                    eventDelget.Add(callback);
                    return;
                }
            }
            eventDelget = new List<EventDelget>();
            eventDelget.Add(callback);
            m_Dictionary.Add(name, eventDelget);
        }

        /// <summary>
        /// 移除监听
        /// </summary>
        /// <param name="name"></param>
        public void RemoveListionter(string name)
        {
            foreach (string key in m_Dictionary.Keys)
            {
                if (key == name)
                {
                    //已经注册该事件
                    m_Dictionary.Remove(key);
                    return;
                }
            }
        }
    }
}

GameModules (游戏管理)

IGame (游戏基类)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    public class IGame 
    {
        public void _InitGame()
        {

        }

        public void _StartGame()
        {

        }

        public void _EndGame()
        {

        }

        public void _UpdateGame()
        {

        }
    }
}

GameSys (游戏系统)

管理游戏系统

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace DLBASE
{
    public class GameSys : ISys
    {
        private IGame m_Game;
        public GameObject m_GameRoot;
        public Camera m_GameCamera;

        /// <summary>
        /// 初始化系统
        /// </summary>
        public override void _InitSys()
        {            
            m_GameRoot = new GameObject();
            m_GameRoot.name = "GameRoot";
        }

        /// <summary>
        /// 创建新游戏
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public T CreatGame<T>()where T: IGame, new ()
        {
            if (m_Game == null)
            {
                m_Game = new T();
            }
            return m_Game as T;
        }

        /// <summary>
        /// 获取游戏
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetGame<T>()where T : GameSys, new()
        {
            return m_Game as T;
        }

        /// <summary>
        /// 刷新游戏
        /// </summary>
        public virtual void UpdateGame()
        {
            m_Game._UpdateGame();
        }
    }
}

AudioModules (音频管理)

这是音频管理系统,管理所有游戏音乐!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    /// <summary>
    /// 音频管理类
    /// </summary>
    public class AudioSys : ISys
    {
        private AudioSource audioSources;
        private bool m_Mutes;

        /// <summary>
        /// 播放音频
        /// </summary>
        /// <param name="name"></param>
        public void PlayAudio(string name)
        {
            if (m_Mutes)
            {
                return;
            }
            GameObject go = new GameObject();
            go.name = "Audio:" + name;
            AudioSource audioSource = go.AddComponent<AudioSource>();
            AudioClip audioClip = Resources.Load("Audio/" + name) as AudioClip;
            audioSource.clip = audioClip;
            audioSource.Play();
            float length = audioClip.length;
            SysManager.ObjectBase.StartCoroutine(GameUtlis.Wait(length, () =>
            {
                Object.Destroy(go);
            }));
        }

        /// <summary>
        /// 播放指定音频
        /// </summary>
        /// <param name="audioClip"></param>
        public void PlayAudio(AudioClip audioClip)
        {
            if (m_Mutes)
            {
                return;
            }
            GameObject go = new GameObject();
            go.name = "Audio:" + audioClip.name;
            AudioSource audioSource = go.AddComponent<AudioSource>();
            audioSource.clip = audioClip;
            audioSource.Play();
            float length = audioClip.length;
            SysManager.ObjectBase.StartCoroutine(GameUtlis.Wait(length, () =>
            {
                Object.Destroy(go);
            }));
        }

        /// <summary>
        /// 播放指定背景音频
        /// </summary>
        /// <param name="name"></param>
        public void PlayBGAudio(string name)
        {
            if (m_Mutes)
            {
                return;
            }
            GameObject go = new GameObject();
            AudioSource audioSource = go.AddComponent<AudioSource>();
            AudioClip audioClip = Resources.Load("Audio/" + name) as AudioClip;
            audioSource.clip = audioClip;
            audioSource.loop = true;
            audioSource.Play();
            go.name = "Audio:" + name;
            audioSources = audioSource;
        }

        /// <summary>
        /// 关闭所有音乐
        /// </summary>
        public void CloseAudio()
        {
            if (audioSources != null)
                audioSources.Pause();
        }

        /// <summary>
        /// 打开所有音乐
        /// </summary>
        public void OpenAudio()
        {
            if (audioSources != null)
                audioSources.Play();
        }

        /// <summary>
        /// 设置静音状态
        /// </summary>
        /// <param name="mute"></param>
        public void SetMute(bool mute)
        {
            m_Mutes = mute;
            if (mute)
            {
                CloseAudio();
            }
            else
            {
                OpenAudio();
            }
        }
    }
}

GameDataSys (数据存储管理)

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace DLBASE
{
    /// <summary>
    /// 游戏数据存取类
    /// </summary>
    public class GameDataSys : ISys
    {
#if UNITY_EDITOR
        public static string m_Path = Application.dataPath + "/StreamingAssets" + "/JsonTest.json";
#elif UNITY_IPHONE
        public static  string m_Path = Application.persistentDataPath + "/JsonTest.json";
#elif UNITY_ANDROID
       public static  string m_Path = Application.persistentDataPath +"/JsonTest.json";
#endif

        /// <summary>
        /// 存储数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        public void SaveGameData<T>(T data) where T : IData
        {
            //如果本地没有对应的json 文件,重新创建
            if (!File.Exists(m_Path))
            {
                Debug.LogError("创建文件成功!");
                File.Create(m_Path);
            }

            string json = JsonConvert.SerializeObject(data);
            Debug.Log(json);
            File.WriteAllText(m_Path, json);
            Debug.Log("保存成功" + json);
        }

        /// <summary>
        /// 读取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T ReadGameData<T>() where T : IData, new()
        {

            if (!File.Exists(m_Path))
            {
                File.Create(m_Path);
                Debug.LogError("读取的文件不存在!");
                return default(T);
            }
            
            string json = File.ReadAllText(m_Path);
            Debug.Log("读取成功" + json);
            T data = JsonConvert.DeserializeObject<T>(json);
            if (data == null)
            {
                data = new T();
            }
            Debug.Log("读取成功" + json);         
            return data;
        }

        /// <summary>
        /// 首次打开游戏的时候 需要先创建一个文件
        /// </summary>
        public  void CreatFile()
        {
            //创建json文件
            if (!File.Exists(m_Path))
            {
                //File.Create(m_Path).Close();
                //File.Create(m_Path);
            }
        }
    }
}

TweenModules (DOTween模块)

这是外部导入的插件!

JsonModules (json解析模块)

这是外部导入的插件!

InputModules (手势输入模块)

这是手势输入模块

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace DLBASE
{
    public enum GesturesType
    {
        UP,
        Down,
        Left,
        Right
    }
    public class InputSys : SysBase
    {
        public delegate void OnMouseDownDelegate(Transform transform);
        public delegate void OnMouseUpDelegage(Transform transform);
        public delegate void OnMouseDragDelegate(Transform transform);
        public delegate void InputDirDelegate(GesturesType gesturesType);

        private OnMouseDownDelegate m_OnMouseDownDelegate;
        private OnMouseUpDelegage m_OnMouseUpDelegage;
        private OnMouseDragDelegate m_OnMouseDragDelegate;
        private InputDirDelegate m_InputDirDelegate;
        //检测开关
        private bool m_Detection = true;
        //当前手势
        private GesturesType m_GesturesType;
        private float m_Time;
        private Vector3 m_StartPos;
        private Vector3 m_EndPos;

        /// <summary>
        /// 点击
        /// </summary>
        /// <param name="transform"></param>
        public override void _OnMouseDown(Transform transform)
        {
            base._OnMouseDown(transform);
            if (!m_Detection) return;
            m_OnMouseDownDelegate?.Invoke(transform);
            m_StartPos = Input.mousePosition;
            m_Time = 0;
        }
        /// <summary>
        /// 抬起
        /// </summary>
        /// <param name="transform"></param>
        public override void _OnMouseUp(Transform transform)
        {
            base._OnMouseUp(transform);
            if (!m_Detection) return;
            m_OnMouseUpDelegage?.Invoke(transform);
        }
        /// <summary>
        /// 拖动
        /// </summary>
        /// <param name="transform"></param>
        public override void _OnMouseDrag(Transform transform)
        {
            base._OnMouseDrag(transform);
            if (!m_Detection) return;
            m_OnMouseDragDelegate?.Invoke(transform);
            if (m_Time < 0.1f)
            {
                m_Time += Time.deltaTime;
            }
            else
            {
                m_EndPos = Input.mousePosition;
                Vector3 dir = (m_EndPos - m_StartPos).normalized;
                float angle = SignedAngleBetween(dir, Vector3.right);
                if((angle>0&&angle<=45)|| (angle > 315 && angle <= 360))
                {
                    m_GesturesType = GesturesType.Right;
                }else if ((angle > 45 && angle <= 135) )
                {
                    m_GesturesType = GesturesType.UP;
                }
                else if ((angle > 135 && angle <= 225))
                {
                    m_GesturesType = GesturesType.Left;
                }
                else if ((angle > 225 && angle <= 315))
                {
                    m_GesturesType = GesturesType.Down;
                }
                m_InputDirDelegate?.Invoke(m_GesturesType);
            }
        }

        public static float SignedAngleBetween(Vector3 a, Vector3 b)
        {
            float angle = Vector3.Angle(a, b);
            if (a.y < 0)
            {
                angle = 360 - angle;
            }
            return angle;
        }

        public void OnInputDir(InputDirDelegate inputDirDelegate)
        {
            m_InputDirDelegate += inputDirDelegate;
        }

        public void OnMouseDown(OnMouseDownDelegate onMouseDownDelegate)
        {
            m_OnMouseDownDelegate += onMouseDownDelegate;
        }

        public void OnMouseUp(OnMouseUpDelegage onMouseUpDelegage)
        {
            m_OnMouseUpDelegage += onMouseUpDelegage;
        }

        public void OnMouseDrag(OnMouseDragDelegate onMouseDragDelegate)
        {
            m_OnMouseDragDelegate += onMouseDragDelegate;
        }

        public void Open()
        {
            Debug.Log("开启检测!!!!");
            m_Detection = true;
        }
        public void Close()
        {
            Debug.Log("关闭检测!!!!");
            m_Detection = false;
        }
    }
}

项目地址

DLFramwork

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值