GameplayTags icon indicating copy to clipboard operation
GameplayTags copied to clipboard

Add JSON serialization support for GameplayTag and GameplayTagContainer

Open rurumi0318 opened this issue 1 month ago • 1 comments

Fix #16

Add GameplayTagJsonConverter and GameplayTagContainerJsonConverter. They use the same format as Unreal's convertion of FGameplayTag and FGameplayTagContainer

Formats

GameplayTag

{
    "tagName": "ParentTagA.Middle.LeafA"
}

GameplayTagContainer

{
    "gameplayTags": [
        {
            "tagName": "ParentTagA.Middle.LeafA"
        },
        {
            "tagName": "ParentTagB.Middle.LeafC"
        }
    ]
}

MyClass

[System.Serializable]
public class MyClass
{
    public GameplayTag tag;
    public GameplayTagContainer tagContainer;
}

MyClass to Json

{
    "tag":
    {
        "tagName": "ParentTagA.Middle.LeafB"
    },
    "tagContainer":
    {
        "gameplayTags": [
            {
                "tagName": "ParentTagA.Middle.LeafA"
            },
            {
                "tagName": "ParentTagB.Middle.LeafC"
            }
        ]
    }
}

Example usage

// serialize
MyClass myInstance = CreateInstance();
string JsonString = GameplayTagJsonSettings.Serialize(myInstance);

// deserialize
MyClass deserializedInstance = GameplayTagJsonSettings.Deserialize<MyClass>(jsonString);

Or the user can setup the serializer settings by themself

JsonSerializerSettings settings = new Newtonsoft.Json.JsonSerializerSettings
        {
            Converters = new Newtonsoft.Json.JsonConverter[]
            {
                new GameplayTagJsonConverter(),
                new GameplayTagContainerJsonConverter()
            }
        };

rurumi0318 avatar Nov 12 '25 17:11 rurumi0318

GameplayTag is a good tool used in Unreal, and I'm glade that someone can implement it in Unity.

However, the fix makes this pakcage has a dependency to Newtonsoft.Json. I only added the converters to my local project, but as Newtonsoft.Json is a built-in package of Unity, adding the dependency should be ok...?

rurumi0318 avatar Nov 12 '25 17:11 rurumi0318