UGUI框架,极简版,仅供参考

//干了几年的活,一直都是看别人写博客,前两天也注册了一个账号,一直没时间写,今天周五,抽个时间也写个极简版的伪框架,希望能帮助到一些人吧
//废话不多说,直接上代码

//最开始需要写一个单例类,单例的作用我就不多说了,可能以后的博客中我会写到

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

public class Single<T> where T : class, new()
{
    public static readonly Object lockobj = new Object();
    private static T instance;
    public static T Instance
    {
        get
        {
            if (instance == null)
            {
                lock (lockobj)
                {
                    if (instance == null)
                    {
                        instance = new T();
                    }
                }
            }
            return instance;
        }
    }

}

//首先写一个model的数据基类,主要用于对一些Model的数据进行管理

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

public abstract class ModelBase
{
    virtual public void OnLoad() { }
    virtual public void OnRlege() { }
}

//然后写一个Model的管理器,对于Model的数据进行一系列的管理作用

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

public class ModelManager : Single<ModelManager>
{
    Dictionary<Type, ModelBase> allModel = new Dictionary<Type, ModelBase>();

    public void RegisterAll()
    {
        Debug.Log("所有模型初始化注册完毕");
        return;
    }

    //数据初始化注册一下
    public void Register(ModelBase model)
    {
        if (!allModel.ContainsValue(model))
        {
            allModel.Add(model.GetType(), model);
            model.OnLoad();
        }
    }

    public T GetModel<T>() where T : ModelBase
    {
        Type type = typeof(T);
        ModelBase mb;
        allModel.TryGetValue(type, out mb);
        if (mb != null)
        {
            return (T)mb;
        }
        return null;
    }

}

//因为主要是UI界面的东西,所以我写了一个预制加载的管理器.,以便于统一管理,而不用系统中的Resources,Load<>()方法,

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

public class ResourcesManager : Single<ResourcesManager>
{
    Dictionary<string, object> resdic = new Dictionary<string, object>();

    public T LoadAssest<T>(string key) where T : Object
    {
        //判断一下,如果不包含就直接生成添加进字典
        if (!resdic.ContainsKey(key))
        {
            T mold = Resources.Load<T>(key);
            resdic.Add(key, mold);
            return mold;
        }

        return resdic[key] as T;
    }
}

//敲敲代码,鼻子出血了,好神奇哦,等我一下…嗯,好了,然后写一个UI基类,里面我只放了三个方法,打开,关闭,和获取
//所有的UI功能脚本都要继承UIBase,

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

public class UIBase : MonoBehaviour
{
    //打开方法
    virtual public void Open()
    {
        this.gameObject.SetActive(true);
    }
    //关闭方法
    virtual public void Close()
    {
        this.gameObject.SetActive(false);
    }
    //获取脚本
    virtual public GameObject GetUI()
    {
        return this.gameObject;
    }
}

//继续写一个UI的管理器.,里面也是对UI界面内的一系列操作方法

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

public class UIManager : Single<UIManager>
{
    //用一个字典储存,字符串做键,UIbase做值,因为,所有的UI功能脚本都要继承UIbase,
    Dictionary<string, UIBase> uimdic = new Dictionary<string, UIBase>();

    //路径,全局找一下路径,生成的所有UI界面都要在Canvas下面
    Transform path;

    public UIManager()
    {
        //路径,全局找一下路径
        path = GameObject.Find("Canvas").transform;
    }


    public void Open(string key)
    {
        //判断一下,如果不包含就添加进去,如果包含那就直接打开咯
        if (!uimdic.ContainsKey(key))
        {
            GameObject pfb = ResourcesManager.Instance.LoadAssest<GameObject>(key);
            GameObject clone = GameObject.Instantiate(pfb, path, false);
            uimdic.Add(key, clone.GetComponent<UIBase>());
        }
        uimdic[key].Open();
    }

    public void Close(string key)
    {
        //关闭我就不想写了,如果需要判断,你们自己加吧
        uimdic[key].Close();
    }

    //嗯,这个是获取
    public UIBase GetUI(string key)
    {
        if (uimdic.ContainsKey(key))
        {
            return uimdic[key];
        }
        return null;
    }
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值