using System.Collections; using System.Collections.Generic; using Unity.Collections; using UnityEngine; using UnityEngine.UI;
publicunsafeclassTestTexture : MonoBehaviour { public RawImage imgTarget; private Texture2D dataCenter; private NativeArray<Color> ptr; private Random rad = new Random(); // Start is called before the first frame update unsafevoidStart() { dataCenter = new Texture2D(100,100,TextureFormat.RGBAFloat, 0,true); dataCenter.filterMode = FilterMode.Point; ptr = dataCenter.GetRawTextureData<Color>();
imgTarget.texture = dataCenter; } }
然后当我们在 CPU 端更改 Texture 的数据后,调用 Apply 方法就可以将数据更新到 GPU 上了。
using System.Collections; using System.Collections.Generic; using Unity.Collections; using UnityEngine; using UnityEngine.UI;
publicunsafeclassTestTexture : MonoBehaviour { public RawImage imgTarget; private Texture2D dataCenter; private NativeArray<Color> ptr; private Random rad = new Random(); // Start is called before the first frame update unsafevoidStart() { dataCenter = new Texture2D(100,100,TextureFormat.RGBAFloat, 0,true); dataCenter.filterMode = FilterMode.Point; ptr = dataCenter.GetRawTextureData<Color>();
imgTarget.texture = dataCenter; }
// Update is called once per frame voidUpdate() { for (int i = 0; i < 100 * 100; ++i) { ptr[i] = new Color(Random.Range(0, 1.0f), Random.Range(0, 1.0f), Random.Range(0, 1.0f), 1); } dataCenter.Apply(); } }