Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing with shared assets makes all maps lose material references #42

Closed
SugoiDev opened this issue Jun 30, 2016 · 1 comment
Closed

Comments

@SugoiDev
Copy link

SugoiDev commented Jun 30, 2016

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.

@SugoiDev
Copy link
Author

SugoiDev commented Jun 30, 2016

I noticed you fixed this on 1.0.7.2 so I'm closing this issue.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant