Created
September 21, 2022 04:14
-
-
Save DraconInteractive/9b51eaee76d2577636d7975cce3e35f6 to your computer and use it in GitHub Desktop.
WebGL Disappearing RawImage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using Random = UnityEngine.Random; | |
public class Test : MonoBehaviour | |
{ | |
public RawImage rawImage; | |
public int XRESOLUTION = 15, YRESOLUTION = 10; | |
public int pixelsPerCell; | |
private RenderTexture rt; | |
private Texture2D tex; | |
private void Start() | |
{ | |
int _xRes = XRESOLUTION * pixelsPerCell; | |
int _yRes = YRESOLUTION * pixelsPerCell; | |
rt = new RenderTexture(_xRes, _yRes, 0); | |
tex = new Texture2D(_xRes, _yRes, TextureFormat.ARGB32, false); | |
rt.filterMode = FilterMode.Point; | |
rt.wrapMode = TextureWrapMode.Clamp; | |
rt.format = RenderTextureFormat.ARGB32; | |
rt.useMipMap = false; | |
rawImage.texture = rt; | |
} | |
private void FixedUpdate() | |
{ | |
int _xRes = XRESOLUTION * pixelsPerCell; | |
int _yRes = YRESOLUTION * pixelsPerCell; | |
RenderTexture.active = rt; | |
var pixels = tex.GetPixels(0, 0, _xRes, _yRes); | |
int pIndex = 0; | |
// i think the weird display comes in here. We need to be doing pixels per cell implementation | |
for (int y = 0; y < YRESOLUTION; y++) | |
{ | |
for (int x = 0; x < XRESOLUTION; x++) | |
{ | |
pixels[pIndex++] = Random.ColorHSV(); | |
} | |
} | |
tex.SetPixels(pixels); | |
tex.Apply(); | |
Graphics.CopyTexture(tex, 0, 0, 0, 0, _xRes, _yRes, rt, 0, 0, 0, 0); | |
RenderTexture.active = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment