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

Is there a way to use "normal" object properties like Width and Height with prefab replacements #267

Open
markus1978 opened this issue Feb 28, 2024 · 2 comments

Comments

@markus1978
Copy link

Thanks for the great tool. Maybe it can even help me with this problem.

I know that we can use custom properties and assign them to public fields or setters on a script in a prefab replacement. But is it also possible somehow get the "normal" build-id tiled object properties like Width, Height, X, Y etc. transferred.

May particular use case is importing ellipses or rects. Here i want to assign the Width and Height to the transform scale and correct the transform position to the middle of the object. By default, SuperTiled2Unity does not seem to override the transform scale from the prefab and takes the top-left of the object (I guess this is what the X, Y in tiled refers to) and assigns it to the transform position.

@M0Z1NG0
Copy link

M0Z1NG0 commented Feb 29, 2024

Hey @markus1978. I was trying to do the same thing with rects and I wasn't able to find a built in way to do this. But I did find a way of hacking it in myself that might help you until the feature is added. There's a function in TmxAssetImporter.cs called DoPrefabReplacements(). This is where all the custom properties are applied to replaced prefabs. I added this code there on line 436 to call my own functions in my prefabs to access width and height of tiled rects. You can do the same thing with m_X and m_Y or any built-in SuperObject property. Do note though that this will add the custom property to every object.

image

@markus1978
Copy link
Author

@M0Z1NG0 Thanks alot for the suggestion. I opted to pull this into a custom importer. Since the replaced prefabs are inaccessible from the custom importer, I did the custom prefab replacement in the custom importer:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.AssetImporters;
using SuperTiled2Unity;
using SuperTiled2Unity.Editor;

[AutoCustomTmxImporter()]
public class MyTmxCustomImporter : CustomTmxImporter
{
    private GameObject cloudPrefab = AssetDatabase.LoadAssetAtPath<GameObject>("Assets/Prefabs/Cloud.prefab");

    public override void TmxAssetImported(TmxAssetImportedArgs args)
    {
        var map = args.ImportedSuperMap;

        foreach (var so in map.GetComponentsInChildren<SuperObject>())
        {
            if (so.m_Type == "Cloud")
            {
                var width = so.m_Width / 16;
                var height = so.m_Height / 16;
                var instance = PrefabUtility.InstantiatePrefab(cloudPrefab) as GameObject;
                instance.transform.SetParent(so.transform.parent);
                instance.transform.position = so.transform.position + cloudPrefab.transform.localPosition + new Vector3(width * 0.5f, -height * 0.5f, 0);
                instance.transform.rotation = so.transform.rotation;
                instance.transform.localScale = new Vector3(width, height, 1);
                instance.name = so.gameObject.name;
                UnityEngine.Object.DestroyImmediate(so.gameObject);
            }
        }
    }
}

This is basically the same lines that the normal importer does in DoPrefabReplacements(), but I added something to the transform.position to center the object and set transform.scale based on the Width and Height from tiled.

It would be nicer, if I could use the standard prefab replacement and customize this in the custom importer. But i really could not find a way to access the instantiated prefabs from the custom importer. In principle the instances would be accessible via args.AssetImporter.GetObject(targetId), but since the original SuperObject instances are removed before the custom importer is called, there is not way to get the targetId and also no easy way to access the tiled properties that were stored int he SuperObject instances.

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

2 participants