Using the API
The other tutorials have shown you how to set up a tiles and a generator, without any coding. But now you need to hook up the generation to the rest of your level.
To do so, you need to call Generate or StartGenerate. Generate will run synchoronously, and return details about the generation. It's easier to use, but can cause noticable stutter if you are doing a big generation. StartGenerate behaves exactly the same, but can be used from a Unity coroutine.
Because co-routines cannot return information, you can instead supply various callbacks using TesseraGenerateOptions. Most commonly, you'll want to set onCreate to replace the behaviour for instantiating new tiles. The default behaviour instantiates them all at once, which can cause stutter.
using UnityEngine;
using Tessera;
using System.Collections;
public class MyBehaviour : MonoBehaviour
{
private TesseraGenerator generator;
void Start()
{
generator = GetComponent<TesseraGenerator>();
StartCoroutine(MyCoro());
}
IEnumerator MyCoro()
{
var options = new TesseraGenerateOptions { onCreate = MyCreate };
yield return generator.StartGenerate(options);
// Any following code will be run after the generation
}
void MyCreate(TesseraTileInstance instance)
{
Debug.Log("Creating " + instance.Tile.gameObject.name);
// Do the default behaviour
TesseraGenerator.Instantiate(instance, generator.transform);
}
}
Here's an example of overriding onComplete, to so we can create the tiles one at a time rather than all at once:
using UnityEngine;
using Tessera;
using System.Collections;
using System.Collections.Generic;
public class MyBehaviour : MonoBehaviour
{
private TesseraGenerator generator;
void Start()
{
generator = GetComponent<TesseraGenerator>();
StartCoroutine(MyCoro());
}
IEnumerator MyCoro()
{
IList<TesseraTileInstance> instances = null;
var options = new TesseraGenerateOptions
{
onComplete = completion =>
{
if(completion.success)
{
instances = completion.tileInstances;
}
}
};
yield return generator.StartGenerate(options);
if(instances != null)
{
foreach(var instance in instances)
{
TesseraGenerator.Instantiate(instance, generator.transform);
// Wait for next frame.
yield return null;
}
}
}
}