Created
March 16, 2021 19:35
-
-
Save MattRix/6541f0eab903c5a9928ce0befcee7648 to your computer and use it in GitHub Desktop.
ScriptedImporter custom texture file format example
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor.AssetImporters; | |
using System; | |
using System.IO; | |
//ScriptedImporter docs: https://docs.unity3d.com/Manual/ScriptedImporters.html | |
[ScriptedImporter(1, "pix")] | |
public class PixImporter : ScriptedImporter | |
{ | |
public Color underscoreColor = Color.clear; | |
public override void OnImportAsset(AssetImportContext ctx) | |
{ | |
var text = File.ReadAllText(ctx.assetPath); | |
var lines = text.Split(new []{"\n"}, StringSplitOptions.RemoveEmptyEntries); | |
for(int i = 0; i<lines.Length; i++) lines[i] = lines[i].Trim(); //trim extra whitespace like /r etc. | |
var tex = new Texture2D(lines[0].Length, lines.Length, TextureFormat.ARGB32,false); | |
tex.filterMode = FilterMode.Point; | |
for(int y = 0; y<tex.height; y++) | |
{ | |
for(int x = 0; x<tex.width; x++) | |
{ | |
int cha = lines[y][x]; | |
var color = underscoreColor; | |
if(cha == 'X') color = Color.black; | |
if(cha == 'W') color = Color.white; | |
tex.SetPixel(x, tex.height-1-y, color); //invert y because texture coords are bottom to top instead of top to bottom like a text file | |
} | |
} | |
tex.Apply(); | |
ctx.AddObjectToAsset("pix", tex); | |
ctx.SetMainObject(tex); | |
} | |
} |
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
WWWWWWWW | |
W__XX__W | |
W__XX__W | |
WXXXXXXW | |
WXXXXXXW | |
W__XX__W | |
W__XX__W | |
WWWWWWWW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment