Affected version: 1.0.6.0
How to reproduce
- Have two maps that use the same tsx tileset, with the same textures. Say, Map1 and Map2.
- Export Map1 and let Unity import it. Verify that it imported correctly.
- Now export Map2 and let Unity import it.
- Map1 has now lost all of its material references.
Fix
During the xml import phase, first we check if an asset with the given name was already created. If it was created, we replace it instead of re-creating it from scratch. This preserves all references to the asset (preserves the meta files).
If anyone ends up having to deal with this, add this helper function to the HelperExtensions {} class on the ImportUtils.cs file:
public static T CreateOrReplaceAsset<T>(T asset, string path) where T : UnityEngine.Object {
var existingAsset = AssetDatabase.LoadAssetAtPath<T>(path);
if (existingAsset == null) {
AssetDatabase.CreateAsset(asset, path);
existingAsset = asset;
} else {
EditorUtility.CopySerialized(asset, existingAsset);
}
return existingAsset;
}
Now, in the ImportTiled2Unity.Xml.cs file, replace AssetDatabase.Create calls with HelperExtensions.CreateOrReplaceAsset
Drawbacks
Suppose you're actually using two maps that have different textures with the same name. For example, in each map we have a background tileset, with a bg1.png tile (or tilesheet).
Using this fix will make the both maps display the bg1.png of the last imported map. This is undesirable, as the images are most likely different.
Note that, without this fix, the first map to be imported would display nothing for bg1.png, as it would have lost the reference altogether.
An alternative fix would be having each imported map have its own material and textures, completely independent of each other, possibly with a suffix in the materials and textures names. Of course, doing this could end up bloating the game since we could end up with lots of duplication.
In my case, the best alternative was to use the presented fix and be careful not to name different things with the same name, even across maps.