Version: 2020.1

Texture.updateCount

切换到手册
public uint updateCount ;

描述

更新纹理时,此计数器会递增。

注意:如果从 GPU 端进行更新,您应自行使计数器递增。(例如,在通过 Blit 操作进入 RenderTexture 时)。(请参阅 IncrementUpdateCount)。

using UnityEngine;
using System.Collections.Generic;

public class MyTextureCache { struct TextureCacheMeta { internal int index; internal uint updateCount; }

Dictionary<Texture, TextureCacheMeta> m_TextureMetas = new Dictionary<Texture, TextureCacheMeta>(); RenderTexture m_Cache;

public Texture cache { get { return m_Cache; } }

public int CacheTexture(Texture texture) { var index = -1; TextureCacheMeta meta; if (m_TextureMetas.TryGetValue(texture, out meta)) { if (meta.updateCount != texture.updateCount) { // Texture has changed since last caching // So blit again into the cache texture BlitTextureAt(meta.index, texture); meta.updateCount = texture.updateCount; m_TextureMetas[texture] = meta; } } else { index = GetNextIndex(); if (index < 0) { Debug.LogError("Invalid index"); return -1; }

m_TextureMetas[texture] = new TextureCacheMeta { index = index, updateCount = texture.updateCount }; } return index; }

void BlitTextureAt(int index, Texture texture) { /* copy pixels in cache */ } int GetNextIndex() { return -1; /* Get next index to use in the cache */ } }