Skip to content

Instantly share code, notes, and snippets.

@MattRix
Created March 16, 2021 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattRix/6541f0eab903c5a9928ce0befcee7648 to your computer and use it in GitHub Desktop.
Save MattRix/6541f0eab903c5a9928ce0befcee7648 to your computer and use it in GitHub Desktop.
ScriptedImporter custom texture file format example
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);
}
}
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