10-23-2018, 03:49 PM
Boom, got it working!
It took a little experimenting, but it wasn't nearly as difficult as I feared. This generates a dynamically-created Texture2D object, and then uses a byte array along with Tilengine to render content to that Texture2D object. Texture2D is a standard Unity class for dynamically generating and editing pixel textures on-the-fly, and can be applied to pretty much any standard Unity shader or material that accepts texture input. (most of them) There are several different ways to use this to display the rendered content. The method I used in the example is to link to an objects mesh rendering context, and assign the Texture2D as its mainTexture reference.
Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Tilengine;
public class TileHandler : MonoBehaviour {
public MeshRenderer trialRenderer;
private Texture2D trialTexture;
public static Engine trialEngine;
private byte[] trialBuffer;
[Range(1, 1920)]
public int trialWidth;
[Range(1, 1920)]
public int trialHeight;
[Range(1, 256)]
public int trialLayersNumber;
[Range(0, 256)]
public int trialSpritesNumber;
[Range(0, 256)]
public int trialAnimationsNumber;
// Use this for initialization
void Start () {
if (trialWidth <= 0) { trialWidth = 1; }
if (trialHeight <= 0) { trialHeight = 1; }
if (trialLayersNumber <= 0) { trialLayersNumber = 1; }
trialEngine = Engine.Init(trialWidth, trialHeight, trialLayersNumber, trialSpritesNumber, trialAnimationsNumber);
trialEngine.LoadPath = "Assets/TrialMap";
trialTexture = new Texture2D(trialWidth, trialHeight, TextureFormat.BGRA32, false, false);
trialTexture.anisoLevel = 0;
trialTexture.filterMode = FilterMode.Point;
trialBuffer = trialTexture.GetRawTextureData();
trialEngine.SetRenderTarget(trialBuffer, 4 * trialWidth);
Tilengine.Color trialBack = new Tilengine.Color(0x1B, 0x00, 0x8B);
trialEngine.SetBackgroundColor(trialBack);
if (trialRenderer != null) {
trialRenderer.material.mainTexture = trialTexture;
}
}
// Update is called once per frame
void Update () {
trialEngine.BeginFrame(Time.frameCount);
for (int i = 0; i < trialHeight; i++) {
trialEngine.DrawNextScanline();
}
trialTexture.LoadRawTextureData(trialBuffer);
trialTexture.Apply();
}
}
It took a little experimenting, but it wasn't nearly as difficult as I feared. This generates a dynamically-created Texture2D object, and then uses a byte array along with Tilengine to render content to that Texture2D object. Texture2D is a standard Unity class for dynamically generating and editing pixel textures on-the-fly, and can be applied to pretty much any standard Unity shader or material that accepts texture input. (most of them) There are several different ways to use this to display the rendered content. The method I used in the example is to link to an objects mesh rendering context, and assign the Texture2D as its mainTexture reference.