본문으로 바로가기

Texture2D.Encode

category Technical Report/Unity Scripts 2017. 8. 17. 14:59
반응형

https://docs.unity3d.com/kr/current/ScriptReference/Texture2D.html


Texture2D.EncodeToPNG >> byte[]


Texture2D img = new Texture2D(8, 8);
for(int y=0; y<8; ++y)
{
for(int x=0; x<8; ++x)
{
img.SetPixel(x, y, Color.black);
}
}
img.Apply();
byte[] by = img.EncodeToPNG();


System.IO.FileStream _fs = new System.IO.FileStream("Img.png", System.IO.FileMode.Create, System.IO.FileAccess.Write);
  System.IO.BinaryWriter _bw = new System.IO.BinaryWriter(_fs);

_bw.Write(by);

_bw.Close();
_fs.Close();



해당 텍스쳐는 import 설정에서 Is Readable가 설정되어 있어야 합니다.



// Create a new texture and assign it to the renderer's material
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
   

void Start() {
      

  Texture2D texture = new Texture2D(128, 128);
        GetComponent<Renderer>().material.mainTexture = texture;

        for (int y = 0; y < texture.height; y++) {
            for (int x = 0; x < texture.width; x++) {
                Color color = ((x & y) != 0 ? Color.white : Color.gray);
                texture.SetPixel(x, y, color);
            }
        }
        texture.Apply();
    }
}


반응형

'Technical Report > Unity Scripts' 카테고리의 다른 글

Unity Custom tube, shere Light  (0) 2018.12.04
Custom Inspectors and Scriptable Objects for UDIM materials  (0) 2018.02.10
Unity FPSCounter  (0) 2017.07.26
Unity Attribute API  (0) 2017.07.26
Unity Shader Property API  (0) 2017.07.25