Advertisement
Ajes

Untitled

Oct 31st, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.89 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Random = UnityEngine.Random;
  6.  
  7. public class ThumbnailTest : MonoBehaviour {
  8.     private Texture2D toShow = null;
  9.     private bool run1 = false;
  10.     private bool run2 = false;
  11.     void OnGUI() {
  12.         if (GUI.Button(new Rect(Screen.width / 2 - 100, 0, 200, 100), "test 1")) {
  13.             run1 = true;
  14.         }
  15.         if (GUI.Button(new Rect(Screen.width / 2 - 100, Screen.height * 0.5f, 200, 100), "test 2")) {
  16.             run2 = true;
  17.         }
  18.         if (toShow != null) {
  19.             GUI.DrawTexture(new Rect(0, 0, 200, 200), toShow);
  20.         }
  21.  
  22.     }
  23.  
  24.     void Start() {
  25.         Debug.Log("lel");
  26.     }
  27.  
  28.     void Update() {
  29.  
  30.         if (run1 == true) {
  31.             run1 = false;
  32.             Debug.Log("1");
  33.             Texture2D tex = new Texture2D(160, 160);
  34.             for (int x = 0; x < 160; x++) {
  35.                 for (int y = 0; y < 160; y++) {
  36.                     tex.SetPixel(x, y, Random.ColorHSV());
  37.                 }
  38.             }
  39.             tex.Apply();
  40.             //toShow = tex;
  41.             byte[] png = tex.EncodeToPNG();
  42.             Texture2D texNew = new Texture2D(1, 1);
  43.             texNew.LoadImage(png);
  44.             texNew.Apply();
  45.             toShow = texNew;
  46.         }
  47.  
  48.  
  49.         if (run2 == true) {
  50.             run2 = false;
  51.             Debug.Log("2");
  52.             Texture2D tex = new Texture2D(160, 160);
  53.             for (int x = 0; x < 160; x++) {
  54.                 for (int y = 0; y < 160; y++) {
  55.                     tex.SetPixel(x, y, Random.ColorHSV());
  56.                 }
  57.             }
  58.             tex.Apply();
  59.             byte[] dataAjes = Texture2DToByteArray(tex);
  60.             Texture2D texNew = ByteArrayToTexture2D(dataAjes);
  61.             toShow = texNew;
  62.         }
  63.     }
  64.  
  65.     private byte[] Texture2DToByteArray(Texture2D tex) {
  66.         Color32[] pixels32 = tex.GetPixels32();
  67.         int texWidth = tex.width;
  68.         int texHeight = tex.height;
  69.         byte[] data = Color32ArrToByteArr(pixels32, 8);
  70.         byte[] widthBytes = BitConverter.GetBytes(texWidth);
  71.         byte[] heightBytes = BitConverter.GetBytes(texHeight);
  72.         data[0] = widthBytes[0];
  73.         data[1] = widthBytes[1];
  74.         data[2] = widthBytes[2];
  75.         data[3] = widthBytes[3];
  76.         data[4] = heightBytes[0];
  77.         data[5] = heightBytes[1];
  78.         data[6] = heightBytes[2];
  79.         data[7] = heightBytes[3];
  80.         return data;
  81.     }
  82.     private Texture2D ByteArrayToTexture2D(byte[] data) {
  83.         Color32[] colors32 = ByteArrToColor32Arr(data, 8);
  84.         int width = BitConverter.ToInt32(data, 0);
  85.         int height = BitConverter.ToInt32(data, 4);
  86.         Texture2D texNew = new Texture2D(width, height, TextureFormat.RGBA32, false);
  87.         texNew.SetPixels32(colors32);
  88.         texNew.Apply();
  89.         return texNew;
  90.     }
  91.     private byte[] Color32ArrToByteArr(Color32[] pixels32, int extraHeaderRooom) {
  92.         byte[] data = new byte[pixels32.Length * 4 + extraHeaderRooom];
  93.         for (var i = 0; i < pixels32.Length; i++) {
  94.             Color32 color32 = pixels32[i];
  95.             data[extraHeaderRooom + i * 4] = color32.r;
  96.             data[extraHeaderRooom + i * 4 + 1] = color32.g;
  97.             data[extraHeaderRooom + i * 4 + 2] = color32.b;
  98.             data[extraHeaderRooom + i * 4 + 3] = color32.a;
  99.         }
  100.         return data;
  101.     }
  102.     private Color32[] ByteArrToColor32Arr(byte[] data, int startIndex) {
  103.         Color32[] result = new Color32[(data.Length - startIndex) / 4];
  104.         for (var i = 0; i < result.Length; i++) {
  105.             byte r = data[startIndex + i * 4];
  106.             byte g = data[startIndex + i * 4 + 1];
  107.             byte b = data[startIndex + i * 4 + 2];
  108.             byte a = data[startIndex + i * 4 + 3];
  109.             result[i] = new Color32(r, g, b, a);
  110.         }
  111.         return result;
  112.     }
  113.  
  114.  
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement