Seanba / SuperTiled2Unity

Imports Tiled files to Unity. Better than regular Tiled2Unity.
MIT License
642 stars 84 forks source link

CustomProperties creating duplicate objects and not applying properties #276

Open MichaelvanDiest opened 1 week ago

MichaelvanDiest commented 1 week ago

Hello, I'm currently attempting to use the Custom Properties feature of SuperTiled2Unity and running into the following error: Identifier uniqueness violation: 'Name:_MapPrfab/Headquarters/Grid/Objects/Object_7 (TRS), Type:GameObject'. Multiple Objects with the same name/type are generated by this Importer. There is no guarantee that subsequent imports of this asset will properly re-link to these targets.

This error only appears when I place an object in my Tiled map with a custom property. I've followed the steps on the "Custom Properties Support" doc page.

When I open the level prefab in Unity, I can see that the importer has duplicated the objects, one is a Super Object with the Super Custom Properties component. And the other being the prefab replacement. However the custom property is not set on the prefab replacement. Here is a screenshot of it: image

I do have the Object Types Xml set in the SuperTiled2Unity settings with the custom properties and everything seems to appear exactly as the "Custom Properties Support" documentation page. Here is a screenshot of my objecttypesxml with the custom properties shown: image

Below is the full error:

Stack Trace:
  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00083] in <dc753a1061284f8e971ee88ee4826eee>:0 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <dc753a1061284f8e971ee88ee4826eee>:0 
  at SuperTiled2Unity.Editor.GameObjectExtensions.BroadcastProperty (UnityEngine.GameObject go, SuperTiled2Unity.CustomProperty property, System.Collections.Generic.Dictionary`2[TKey,TValue] objectsById) [0x00141] in .\Packages\super-tiled2unity.v2.1.1\Editor\Extensions\GameObjectExtensions.cs:179 
  at SuperTiled2Unity.Editor.TmxAssetImporter.DoPrefabReplacements () [0x00186] in .\Packages\super-tiled2unity.v2.1.1\Editor\Importers\TmxAssetImporter.cs:438 
  at SuperTiled2Unity.Editor.TmxAssetImporter.InternalOnImportAsset () [0x000af] in .\Packages\super-tiled2unity.v2.1.1\Editor\Importers\TmxAssetImporter.cs:76 
  at SuperTiled2Unity.Editor.SuperImporter.OnImportAsset (UnityEditor.AssetImporters.AssetImportContext ctx) [0x00065] in .\Packages\super-tiled2unity.v2.1.1\Editor\Importers\SuperImporter.cs:71 
0x00007ff633d95cfd (Unity) StackWalker::GetCurrentCallstack
0x00007ff633d9adc9 (Unity) StackWalker::ShowCallstack
0x00007ff634d73ad1 (Unity) GetStacktrace
0x00007ff63542bc4e (Unity) DebugStringToFile
0x00007ff632d24e98 (Unity) DebugLogHandler_CUSTOM_Internal_Log
0x000001b775acf93a (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
0x000001b775adfe6b (Mono JIT Code) UnityEngine.DebugLogHandler:LogFormat (UnityEngine.LogType,UnityEngine.Object,string,object[])
0x000001b775adf6ac (Mono JIT Code) UnityEngine.Logger:LogFormat (UnityEngine.LogType,string,object[])
0x000001b775adf3fb (Mono JIT Code) UnityEngine.Debug:LogErrorFormat (string,object[])
0x000001b7759d924b (Mono JIT Code) SuperTiled2Unity.Editor.SuperImporter:OnImportAsset (UnityEditor.AssetImporters.AssetImportContext) (at ./Packages/super-tiled2unity.v2.1.1/Editor/Importers/SuperImporter.cs:92)
0x000001b7759d88e5 (Mono JIT Code) UnityEditor.AssetImporters.ScriptedImporter:GenerateAssetData (UnityEditor.AssetImporters.AssetImportContext)
0x000001b6951d551e (Mono JIT Code) (wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object (object,intptr,intptr,intptr)
0x00007ff908a44bfe (mono-2.0-bdwgc) mono_jit_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/mini/mini-runtime.c:3445)
0x00007ff90897d254 (mono-2.0-bdwgc) do_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3068)
0x00007ff90897d3cc (mono-2.0-bdwgc) mono_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3115)
0x00007ff633caf514 (Unity) scripting_method_invoke
0x00007ff633c8d274 (Unity) ScriptingInvocation::Invoke
0x00007ff63519d514 (Unity) ScriptedImporter::GenerateAssetData
0x00007ff63508c86b (Unity) ImportToObjects
0x00007ff63508b91c (Unity) ImportAsset
0x00007ff6350b07d8 (Unity) AssetImportWorker::Import
0x00007ff6350e18eb (Unity) AssetImportManager::ImportInProcess
0x00007ff6350decfa (Unity) AssetImportManager::Import
0x00007ff6350e21e1 (Unity) ImportOutOfDateAssets
0x00007ff6350ec48c (Unity) RefreshInternalV2
0x00007ff6350f3dcc (Unity) StopAssetImportingV2Internal
0x00007ff6350ed7cc (Unity) RefreshV2
0x00007ff635063140 (Unity) AssetDatabase::Refresh
0x00007ff6348e8c6b (Unity) Application::AutoRefresh
0x00007ff634d9c0f3 (Unity) ContainerWindow::ContainerWndProc
0x00007ff97dc1ef5c (USER32) CallWindowProcW
0x00007ff97dc1e8cc (USER32) DispatchMessageW
0x00007ff97dc31633 (USER32) SendMessageTimeoutW
0x00007ff97e8313e4 (ntdll) KiUserCallbackDispatcher
0x00007ff97c461064 (win32u) NtUserPeekMessage
0x00007ff97dc1a333 (USER32) PeekMessageW
0x00007ff97dc1a293 (USER32) PeekMessageW
0x00007ff634d7931d (Unity) MainMessageLoop
0x00007ff634d7e650 (Unity) WinMain
0x00007ff63615e0ae (Unity) __scrt_common_main_seh
0x00007ff97d917344 (KERNEL32) BaseThreadInitThunk
0x00007ff97e7dcc91 (ntdll) RtlUserThreadStart
Seanba commented 1 week ago

Hi there, @MichaelvanDiest. Can I get a access to your project? That would be the easiest way for me to debug and figure out what has gone wrong here.

MichaelvanDiest commented 1 week ago

Hi @Seanba I've added you as a contributor to the repository.

You can open Scenes/Test and then drag in one of the Tiled maps in Assets/Tiled/Resources/Levels/City/Easy to get the error.

Seanba commented 1 week ago

Hi again, @MichaelvanDiest. The issue is that your DiestSprite.SetGravityScale method is throwing an exception because the RigidBody property is null. My guess is this because that property getter is only non-null after Awake is called. That Awake method isn't invoked until the game enters play mode though.

image

I modified your property from this ...

public Rigidbody2D RigidBody { get { return cRigidBody; } }

to this ...

public Rigidbody2D RigidBody
{
    get
    {
        if (cRigidBody == null)
        {
            cRigidBody = gameObject.GetComponent<Rigidbody2D>();
        }
        return cRigidBody;
    }
}

and then I was able to reimport your TMX file without any warnings or errors.

I'll try to add some better error reporting to ST2U so that issues like this are easier to spot next time. I'll keep this bug open until that task is completed.

Let me know if you have further questions.

MichaelvanDiest commented 1 week ago

Amazing @Seanba , thank you so much.

I wasn't aware that SuperTiled2Unity runs the function at editor/import time, I thought only during runtime.

Seanba commented 1 week ago

You're welcome. The next version of ST2U provides more information on this sort of thing:

image

SuperTiled2Unity version: 2.1.1, Unity version: 2022.3.20f1
Error invoking 'BarrelController.SetGravityScale(1)' method. Exception = 'Object reference not set to an instance of an object'
Error invoking 'BoxController.SetGravityScale(1)' method. Exception = 'Object reference not set to an instance of an object'
Error invoking 'BoxController.SetGravityScale(1)' method. Exception = 'Object reference not set to an instance of an object'
Error invoking 'BoxController.SetGravityScale(1)' method. Exception = 'Object reference not set to an instance of an object'
Error invoking 'BarrelController.SetGravityScale(1)' method. Exception = 'Object reference not set to an instance of an object'

That should help indicate the problem lies in the code being invoked and not necessarily in the ST2U plugin itself. (Not to say bugs haven't been found in ST2U, mind you.)

MichaelvanDiest commented 1 week ago

That looks great! The original error message made me think I set up the custom properties incorrectly, so I didn't even think to look at the SetGravityScale function. Though in hindsight I totally should have.