Custom Importer - Seanba/Tiled2Unity GitHub Wiki

Simple Custom Importer, Reading the Tile Property AddComp and Adding an Component with the name given by this property.

[Tiled2Unity.CustomTiledImporter]
class CustomImporterAddComponent : Tiled2Unity.ICustomTiledImporter
{
    public static Type[] getTypeByName(string className) {
        List<Type> returnVal = new List<Type>();

        foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
            Type[] assemblyTypes = a.GetTypes();
            for (int j = 0; j < assemblyTypes.Length; j++) {
                if (assemblyTypes[j].Name == className) {
                    returnVal.Add(assemblyTypes[j]);
                }
            }
        }

        return returnVal.ToArray();
    }

    public void HandleCustomProperties(UnityEngine.GameObject gameObject,
        IDictionary<string, string> props)
    {
        // Simply add a component to our GameObject
        if (props.ContainsKey("AddComp")) {

            Type[] t = getTypeByName(props["AddComp"]);
            if(t.Length == 1) gameObject.AddComponent(t[0]);
            else Debug.LogWarning("Component \"" + props["AddComp"] + "\" could not be identified. Found " + t.Length + " expected 1.");
        }
    }


    public void CustomizePrefab(GameObject prefab)
    {
        // Do nothing
    }
}
⚠️ **GitHub.com Fallback** ⚠️