#Gameobjects serialization problem

1 messages ยท Page 1 of 1 (latest)

digital gulch
#

Hello, I need a little help. I'm having trouble loading JSON with serialization. I thought at first it was a problem with deserialization but after testing and checking I noticed that serialization stops at about the fifth object. I removed some data that JSON contains but serialization still stops at 5 object and I don't really know why. It's not a problem with a specific object because I've tried different ones. I would really appreciate some help if anyone has had a similar problem. I would appreciate any help.The hierarchy of the object (MainObject) I want to save looks like this:

iron shadow
#

How are u serializing it. It's likely you're just hitting the depth limit

digital gulch
#

Hard to say, can I show you code which is responisble for it?

#
    {
        SerializedGameObject serializedObj = new SerializedGameObject
        {
            name = obj.name,
            tag = obj.tag,
            layer = obj.layer,
            isStatic = obj.isStatic,
            parent = obj.transform.parent,
            localPosition = new SerializableVector3(obj.transform.localPosition.x, obj.transform.localPosition.y, obj.transform.localPosition.z),
            localRotation = new SerializableQuaternion(obj.transform.localRotation),
            localScale = new SerializableVector3(obj.transform.localScale.x, obj.transform.localScale.y, obj.transform.localScale.z),
            activeSelf = obj.activeSelf
        };

        foreach (Component component in obj.GetComponents<Component>())
        {
            if (component is Transform) continue;

            SerializedComponent serializedComponent = new SerializedComponent
            {
                type = component.GetType().AssemblyQualifiedName
            };

            try
            {
                if (component is MeshRenderer)
                {
                    serializedObj.meshRenderers.Add(SerializeMeshRenderer(component as MeshRenderer));
                }
            }
            catch (ArgumentException)
            {
                Debug.LogWarning($"Could not serialize component of type {component.GetType().Name} on object {obj.name}");
            }
        }

        foreach (Transform child in obj.transform)
        {
            serializedObj.children.Add(Serialize(child.gameObject));
        }

        return serializedObj;
    }```
iron shadow
#

Did you write this

digital gulch
#

What you mean? THe code?

iron shadow
#

Yes

digital gulch
#

Yea, I did with some help

iron shadow
#

I'll be on pc in like 2 mins, hard to read on mobile

digital gulch
#

Ok, thank you blushie

iron shadow
digital gulch
#

JSON HAS LIKE 61K characters ๐Ÿ˜›

iron shadow
#

yea this is most likely some json limit then, because i see you're just looping over the objects

digital gulch
#

So I have prefabs and you can connect them to eachother So each prefab has connection point which next prefab can connect to

#

So if there is 7 prefabs there is already 7 layers :/

iron shadow
#

you may want to reference these differently, so there arent a possible infinite amount of connections

#

like give an ID to what prefab is connected instead

#

you really dont want to save entire gameobjects like this

#

like for example do you really need to save every single component

digital gulch
#

Hmm no idea tbh. I found out about this solution few weeks back and now I need to fight with it again. I would be more than happy to just serilize which prefab and what parent the prefab has instead of doing each component

#

If it is possible doing like that

iron shadow
#

yea i really wouldnt use this

digital gulch
#

But still I would need to save the materials and some of the variables from scripts that are inside of the prefab

iron shadow
#

your prefabs are already saved pretty much in the game files, unless the user has complete control over editing the materials then theres really no point in saving them

digital gulch
#

User can change the material/color of the prefabs model in the scene but the prefab in the assets is still the same

#

Sorry if I don't make sense it is hard for me to explain so of the things XD

#

It is confusing af

#

But there are predefined materials so there is no custom colors or anything

iron shadow
#

idk its hard for me to say what you can cut out, for example in my game i have a player prefab and the player can control its own material colors/metallic/smoothness variables, so i just save those only. Load the default prefab into the game, then apply the saved material

#

you likely dont need to be saving entire game objects, maybe just its position and an ID for what it connects to

digital gulch
#

Hard to say, I will try to do that. I hope it will not be too much pain to do something like that.

#

I am not fan of deserialization

#

But cannot do really anything else when I am using Unity WebGL and connecting it to MySQL

iron shadow
#

well you're saving to json already lol, doesnt matter what you save, you have to retrieve it somehow

digital gulch
#

It is retriving

iron shadow
#

then what i suggested is the same thing

digital gulch
#

It is just the serialization is not getting all of the objects in the hierarchy

iron shadow
#

webgl and mysql, you definitely arent gonna be saving a 61k line json file

#

you should just save the parts you need only

digital gulch
#

So in my case, Position,rotation, parent, material, prefab reference or smth, is that what you are saying instead of going all components?

iron shadow
#

maybe not even parent, but yea

#

like it really depends what these prefabs are, the example i gave was my actual player prefab which is like 50 transforms in total. I still only save the material because the prefab doesnt change

#

if i need to, i can save the location of the main transform but my game doesnt require it

digital gulch
#

I have 9 prefabs they look like this

#

When it come to hierarchy

#

Then Object 2 goes under SnapPoint and so on

#

model has renderer and materials

iron shadow
#

i mean i still dont know what this actually is, its more for u to decide what you actually need saved

#

does the transforms ever get changed, do the childs local transforms ever change, does scale/rotation ever change, its activeself?

digital gulch
#

Only Main parent Object1, object2 etc get the position and rotation changed the rest just stays in place by following the rotation,position of the parent

#

Scale is always the same

#

And the materials of model, and knobs are getting changed and that is all tbh

#

So I think I know what I need to save now ๐Ÿ˜›

#

I just hope accessing everything inside the prefabs and applying everything will not be pain to do

iron shadow
#

itll be more annoying in a sense, but saving entire game objects just isnt viable like this

digital gulch
#

Wait which is not viable? The thing that I have right now or the one that we are talking about ?

iron shadow
#

the thing u have right now

#

saving what u need then applying it is way less data saved

#

and in reality its not even gonna be a performance hit unless u are doing this to a ton of objects

#

but loading a massive json/sql is way more of a performance hit i think

digital gulch
#

Understood, So I will have to change a lot of stuff ๐Ÿ˜„

#

Hope it will go smooth

#

Thank you very much for help for now, I will try what I can do with the new understanding :D. But firstly need to go to sleep since it is getting a bit late ๐Ÿ˜›

#

Is it okay if I could ping you If I will have any question?

iron shadow
#

try just asking in the channels, if i see it ill answer