using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Thousandto.Core.Base
{
///
/// 一帧之内不作多次变化的数据缓存 -- 根据帧决定更新缓存
///
///
public class CacheDataByFrame 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;
}
}
}
}