Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Random = UnityEngine.Random;
- public class ThumbnailTest : MonoBehaviour {
- private Texture2D toShow = null;
- private bool run1 = false;
- private bool run2 = false;
- void OnGUI() {
- if (GUI.Button(new Rect(Screen.width / 2 - 100, 0, 200, 100), "test 1")) {
- run1 = true;
- }
- if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height * 0.5f, 200, 100), "test 2")) {
- run2 = true;
- }
- if (toShow != null) {
- GUI.DrawTexture(new Rect(0, 0, 200, 200), toShow);
- }
- }
- void Start() {
- Debug.Log("lel");
- }
- void Update() {
- if (run1 == true) {
- run1 = false;
- Debug.Log("1");
- Texture2D tex = new Texture2D(160, 160);
- for (int x = 0; x < 160; x++) {
- for (int y = 0; y < 160; y++) {
- tex.SetPixel(x, y, Random.ColorHSV());
- }
- }
- tex.Apply();
- //toShow = tex;
- byte[] png = tex.EncodeToPNG();
- Texture2D texNew = new Texture2D(1, 1);
- texNew.LoadImage(png);
- texNew.Apply();
- toShow = texNew;
- }
- if (run2 == true) {
- run2 = false;
- Debug.Log("2");
- Texture2D tex = new Texture2D(160, 160);
- for (int x = 0; x < 160; x++) {
- for (int y = 0; y < 160; y++) {
- tex.SetPixel(x, y, Random.ColorHSV());
- }
- }
- tex.Apply();
- byte[] dataAjes = Texture2DToByteArray(tex);
- Texture2D texNew = ByteArrayToTexture2D(dataAjes);
- toShow = texNew;
- }
- }
- private byte[] Texture2DToByteArray(Texture2D tex) {
- Color32[] pixels32 = tex.GetPixels32();
- int texWidth = tex.width;
- int texHeight = tex.height;
- byte[] data = Color32ArrToByteArr(pixels32, 8);
- byte[] widthBytes = BitConverter.GetBytes(texWidth);
- byte[] heightBytes = BitConverter.GetBytes(texHeight);
- data[0] = widthBytes[0];
- data[1] = widthBytes[1];
- data[2] = widthBytes[2];
- data[3] = widthBytes[3];
- data[4] = heightBytes[0];
- data[5] = heightBytes[1];
- data[6] = heightBytes[2];
- data[7] = heightBytes[3];
- return data;
- }
- private Texture2D ByteArrayToTexture2D(byte[] data) {
- Color32[] colors32 = ByteArrToColor32Arr(data, 8);
- int width = BitConverter.ToInt32(data, 0);
- int height = BitConverter.ToInt32(data, 4);
- Texture2D texNew = new Texture2D(width, height, TextureFormat.RGBA32, false);
- texNew.SetPixels32(colors32);
- texNew.Apply();
- return texNew;
- }
- private byte[] Color32ArrToByteArr(Color32[] pixels32, int extraHeaderRooom) {
- byte[] data = new byte[pixels32.Length * 4 + extraHeaderRooom];
- for (var i = 0; i < pixels32.Length; i++) {
- Color32 color32 = pixels32[i];
- data[extraHeaderRooom + i * 4] = color32.r;
- data[extraHeaderRooom + i * 4 + 1] = color32.g;
- data[extraHeaderRooom + i * 4 + 2] = color32.b;
- data[extraHeaderRooom + i * 4 + 3] = color32.a;
- }
- return data;
- }
- private Color32[] ByteArrToColor32Arr(byte[] data, int startIndex) {
- Color32[] result = new Color32[(data.Length - startIndex) / 4];
- for (var i = 0; i < result.Length; i++) {
- byte r = data[startIndex + i * 4];
- byte g = data[startIndex + i * 4 + 1];
- byte b = data[startIndex + i * 4 + 2];
- byte a = data[startIndex + i * 4 + 3];
- result[i] = new Color32(r, g, b, a);
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement