翻開秘籍第一頁:
問: 什么是類物件池?
劍之初: 游戲中,我們常常會遇到頻繁得創建和銷毀大量相同物件的場景,如果我們不做任何的特殊處理,這種場景會出現兩個性能問題——大量的記憶體碎片以及頻繁的分配記憶體空間, 而物件池能供完美得解決這兩個問題,
翻開秘籍第二頁:
問: 什么原理?
劍之初: 當創建物件時,物件池將物件放入池管理的某種記憶體連續的資料結構中(陣列或者堆疊等),當不需要物件時,物件池并不銷毀物件,而是將物件回收到池中,下次需要的時候再次從池中拿出來,
因為,物件儲存在記憶體連續的資料結構中,所以解決了記憶體碎片的問題,
因為,物件每次用完以后就放回池中回圈利用而不是再次創建和銷毀,這樣就解決了頻繁的記憶體分配和銷毀的問題,
翻開秘籍代碼頁:
#region 模塊資訊
//===================================================
// Copyright (C) 2020
//
// 檔案名(File Name): ClassObjectPool.cs
// 作者(Author): 稀飯
// 郵箱(e-mail): 1144000915@qq.com
// 創建時間(CreateTime): 2020-12-22 13:53:04
// 修改者串列(modifier):
// 模塊描述(Module description): 創建腳本自動修改檔案名、作者、創建時間
//===================================================
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YouYou
{
/// <summary>
/// 類物件池
/// </summary>
public class ClassObjectPool : IDisposable
{
private Dictionary<int, Queue<object>> m_ClassObjectPoolDic;
public ClassObjectPool()
{
m_ClassObjectPoolDic = new Dictionary<int, Queue<object>>();
}
/// <summary>
/// 取出一個物件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Dequeue<T>() where T:class,new()
{
lock(m_ClassObjectPoolDic)
{
//先找到這個類對應的哈希值
int key = typeof(T).GetHashCode();
Queue<object> queue = null;
m_ClassObjectPoolDic.TryGetValue(key, out queue);
if(queue==null)
{
queue = new Queue<object>();
m_ClassObjectPoolDic[key] = queue;
}
//獲取物件
if(queue.Count>0)
{
//說明佇列中 有閑置的
return (T)queue.Dequeue();
}else
{
//如果佇列中沒有 實體化一個
return new T();
}
}
}
/// <summary>
/// 物件回池
/// </summary>
/// <param name="obj"></param>
public void Enqueue(object obj)
{
//先找到這個類對應的哈希值
int key = obj.GetType().GetHashCode();
Queue<object> queue = null;
m_ClassObjectPoolDic.TryGetValue(key, out queue);
if (queue != null)
{
queue.Enqueue(obj);
}
}
public void Dispose()
{
m_ClassObjectPoolDic.Clear();
}
}
}
翻開秘籍代碼注解頁:
問: 外部如何呼叫呢?
劍之初:
GameEntry.Pool.Dequeue(); 從類物件池中取出類物件
GameEntry.Pool.Enqueue(action); 物件回池
#region 模塊資訊
//===================================================
// Copyright (C) 2020
//
// 檔案名(File Name): TimeComponent.cs
// 作者(Author): 稀飯
// 郵箱(e-mail): 1144000915@qq.com
// 創建時間(CreateTime): 2020-12-15 16:10:51
// 修改者串列(modifier):
// 模塊描述(Module description): 時間組件
//===================================================
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YouYou
{
/// <summary>
/// 時間組件
/// </summary>
public class TimeComponent : YouYouBaseComponent, IUpdateComponent
{
protected override void OnAwake()
{
base.OnAwake();
GameEntry.AddUpdateComponent(this);
m_TimeManager = new TimeManager();
}
#region 定時器
/// <summary>
/// 定時器管理器
/// </summary>
private TimeManager m_TimeManager;
/// <summary>
/// 創建定時器
/// </summary>
/// <returns></returns>
public TimeAction CreateTimeAction()
{
return GameEntry.Pool.Dequeue<TimeAction>();
}
/// <summary>
/// 注冊定時器
/// </summary>
/// <param name="action"></param>
internal void RegisterTimeAction(TimeAction action)
{
m_TimeManager.RegisterTimeAction(action);
}
/// <summary>
/// 移除定時器
/// </summary>
/// <param name="action"></param>
internal void RemoveTimeAction(TimeAction action)
{
m_TimeManager.RemoveTimeAction(action);
GameEntry.Pool.Enqueue(action);
}
#endregion
public void OnUpdate()
{
m_TimeManager.OnUpdate();
}
public override void Shutdown()
{
m_TimeManager.Dispose();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/240128.html
標籤:其他
