Files
Main/Assets/Plugins/References/FuncellBase/Container/CacheDataByFrame.cs

58 lines
1.4 KiB
C#
Raw Normal View History

2025-01-25 04:38:09 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Thousandto.Core.Base
{
/// <summary>
/// 一帧之内不作多次变化的数据缓存 -- 根据帧决定更新缓存
/// </summary>
/// <typeparam name="T"></typeparam>
public class CacheDataByFrame<T> where T : UnityEngine.Object
{
public delegate T GetDataMethod();
private GetDataMethod _getMethod = null;
private int _frameCounter = -1; // 缓存数据更新控制
private T _cacheData;
public CacheDataByFrame(GetDataMethod getFunc)
{
_getMethod = getFunc;
if (_getMethod != null)
{
_cacheData = _getMethod();
}
}
public T GetCacheData()
{
if (_frameCounter != Time.frameCount)
{
// not the same frame -- get from method
_frameCounter = Time.frameCount;
if (_getMethod != null)
{
_cacheData = _getMethod();
}
}
// same frame get cache
return _cacheData;
}
public void Release()
{
if (_cacheData != null)
{
_cacheData = null;
}
}
}
}