美文网首页
Unity的程序基础框架---缓存池模块

Unity的程序基础框架---缓存池模块

作者: Cheney_ | 来源:发表于2025-11-12 11:17 被阅读0次

缓存池模块

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

/// <summary>

/// 缓存池模块

/// 1.Dictionary  List

/// 2.GameObject Resources

/// </summary>

public class PoolMgr : BaseManager<PoolMgr>

{

    public Dictionary<string, List<GameObject>> poolDic = new Dictionary<string, List<GameObject>>();

    public GameObject GetObj(string name)

    {

        GameObject obj = null;

        if (poolDic.ContainsKey(name) && poolDic[name].Count > 0)

        {

            obj = poolDic[name][0];

            poolDic[name].RemoveAt(0);

        }

        else

        {

            obj = GameObject.Instantiate(Resources.Load<GameObject>(name));

            obj.name = name;

        }

        obj.GetOrAddComponent<DelayPush>();

        obj.SetActive(true);

        return obj;

    }

    public void PushObj(string name, GameObject obj)

    {

        obj.SetActive(false);

        if (poolDic.ContainsKey(name))

        {

            poolDic[name].Add(obj);

        }

        else

        {

            poolDic.Add(name, new List<GameObject>() { obj });

        }

    }

}

测试

加载

void Update()

    {

        if (Input.GetMouseButtonDown(0))

        {

            PoolMgr.GetInstance().GetObj("Cube");

        }

        if (Input.GetMouseButtonDown(1))

        {

            PoolMgr.GetInstance().GetObj("Sphere");

        }

    }

回收

private void OnEnable()

    {

        Invoke(nameof(Push), 1f);

    }

    void Push()

    {

        PoolMgr.GetInstance().PushObj(this.gameObject.name, this.gameObject);

    }

拓展

GetOrAddComponent的实现

using UnityEngine;

static public class MethodExtensionForUnity

{

    /// <summary>

    /// Gets or add a component. Usage example:

    /// BoxCollider boxCollider = transform.GetOrAddComponent<BoxCollider>();

    /// </summary>

    static public T GetOrAddComponent<T>(this Component child, bool set_enable = false) where T : Component

    {

        T result = child.GetComponent<T>();

        if (result == null)

        {

            result = child.gameObject.AddComponent<T>();

        }

        var bcomp = result as Behaviour;

        if (set_enable)

        {

            if (bcomp != null) bcomp.enabled = true;

        }

        return result;

    }

    static public T GetOrAddComponent<T>(this GameObject go) where T : Component

    {

        T result = go.transform.GetComponent<T>();

        if (result == null)

        {

            result = go.AddComponent<T>();

        }

        var bcomp = result as Behaviour;

        if (bcomp != null) bcomp.enabled = true;

        return result;

    }

    public static void walk(this GameObject o, System.Action<GameObject> f)

    {

        f(o);

        int numChildren = o.transform.childCount;

        for (int i = 0; i < numChildren; ++i)

        {

            walk(o.transform.GetChild(i).gameObject, f);

        }

    }

}

相关文章

  • 架构&框架

    图片缓存 怎样设计一个图片缓存框架 图片管理者模块:内存缓存模块、磁盘缓存模块、网络图片下载模块 图片处理:图片解...

  • 图片链接工具

    Unity3d生命周期:Unity3d生命周期.png XJGame内存池:内存池.png XJGameUI框架i...

  • Unity 简单的缓存池

  • iOS-常用框架解读

    一、框架&架构作用 模块化 分层 解耦 降低代码重合度 二、图片缓存框架 怎样设计一个图片缓存框架? 2.1 图片...

  • 组件化方案实践总结

    1.模块分离 1.基础模块基础模块主要封装网络请求,日志框架,路由设置等信息 业务模块业务模块依赖基础模块,实现不...

  • iOS面试12 - 结构/框架

    框架/ 架构 模块化 分层 解耦 降低代码的重合度 图片缓存 如何设计一个图片缓存框架? 2019-02-16 下...

  • SQlite

    基础 缓存数据 第三方框架

  • Unity框架设计系列专题1.1 Unity 如何设计网络框架

    在Unity框架设计中与游戏服务器对接的网络框架也是非常重要的一个模块,本文給大家分享如何来基于Unity来设计一...

  • 如何评价《守望先锋》架构设计?

    在Unity框架设计中与游戏服务器对接的网络框架也是非常重要的一个模块,本文給大家分享如何来基于Unity来设计一...

  • Unity-对象池模块

    为什么使用对象池 当一个游戏需要频繁的创建和销毁对象时,为了不增加GC的性能消耗,可以考虑使用回收对象来打到复用的...

网友评论

      本文标题:Unity的程序基础框架---缓存池模块

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