Last active
April 15, 2021 22:22
-
-
Save partybusiness/9d32e50f65fd923f087d1c39420a4285 to your computer and use it in GitHub Desktop.
Text image format converter
This file contains 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
#if UNITY_EDITOR | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System; | |
using System.IO; | |
[ExecuteInEditMode] | |
public class ConvertImagesToText : MonoBehaviour { | |
/// <summary> | |
/// Texture that will be converted into text. It needs Read/Write Enabled or the script won't be able to read pixels | |
/// </summary> | |
public Texture2D sourceTexture; | |
/// <summary> | |
/// Text file that will be converted back into a PNG | |
/// </summary> | |
public TextAsset sourceText; | |
public bool convertNow = false; | |
void Update () { | |
if (convertNow) | |
{ | |
if (sourceTexture!=null) | |
{ | |
SaveImageAsText(); | |
} else if (sourceText!=null) | |
{ | |
SaveTextAsImage(); | |
} | |
convertNow = false; | |
} | |
} | |
private void SaveTextAsImage() | |
{ | |
//load height and width of image | |
//generate new image | |
//write pixel based on R G B values for each pixel | |
//save as PNG | |
var filePath = string.Format("{0}/{1}.png", Application.dataPath, sourceText.name); | |
var splitFile = new string[] { "\r\n", "\r", "\n" }; | |
var splitLine = new char[] { '|' }; | |
var lines = sourceText.text.Split(splitFile, StringSplitOptions.RemoveEmptyEntries); | |
var dimensions = lines[0].Split(','); | |
var temptexture = new Texture2D( int.Parse(dimensions[0]), int.Parse(dimensions[1]), TextureFormat.RGB24, false); | |
for (int y = 0; y < temptexture.height; y++) | |
{ | |
var currentLine = (lines[y + 1]).Split(splitLine); | |
for (int x = 0; x < temptexture.width; x++) | |
{ | |
var newColour = Color.white; | |
ColorUtility.TryParseHtmlString("#"+currentLine[x], out newColour); | |
temptexture.SetPixel(x, y, newColour); | |
} | |
} | |
var outputBytes = ImageConversion.EncodeToPNG(temptexture); | |
File.WriteAllBytes(filePath, outputBytes); | |
} | |
private void SaveImageAsText() | |
{ | |
//create new text file | |
//save height and width of image | |
//save R G B values for each pixel in image | |
//close text file | |
var filePath = string.Format("{0}/{1}.txt", Application.dataPath, sourceTexture.name); | |
Debug.Log(filePath); | |
StreamWriter writer = new StreamWriter(filePath, true); | |
writer.WriteLine(string.Format("{0},{1}, Text image format, converter here: https://gist.github.com/partybusiness/9d32e50f65fd923f087d1c39420a4285 ", sourceTexture.width, sourceTexture.height)); | |
for (int y = 0; y < sourceTexture.height; y++) | |
{ | |
var stringList = new List<string>(); | |
for (int x = 0; x < sourceTexture.width; x++) | |
{ | |
var col = sourceTexture.GetPixel(x, y); | |
stringList.Add(ColorUtility.ToHtmlStringRGB(col)); | |
} | |
writer.WriteLine(string.Join("|", stringList.ToArray())); | |
} | |
writer.Close(); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment