Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion Saving/Loading Materials & Objects at Runtime

Discussion in 'Editor & General Support' started by angeldevelopment, Jan 19, 2023.

  1. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    236
    Hello I have a Unity Game with a store where players can customize their skins (material) and objects such as hats, shoes, etc...

    I had tried using JSON serialization to save these objects/ materials... this works within the editor, but when I deploy to iOS and test on a device, the material/objects do not load despite the application retrieving the correct JSON string. What is even weirder however, is that if I navigate to the store scene where all of these objects and materials are contained, then the saved skin/object load properly (despite the JSON string never changing).

    In Unity when I save the material or object as a JSON string it saves as skin: (InstanceID: 34567)
    On iOS it saves as skin: (m_FileID: 3456, m_PathID: 0) , where path id is always 0.

    Rather than look for a fix I think I am going to change how I save the skin/objects. I am wondering what is the recommended way of doing this. It seems that JSON is suited better to primitives, not objects.

    Another alternative is Resource.Load, I do not believe this will suit my needs because the Resource folder can not be modified at runtime. Perhaps I could move all of the store objects/skins into the resource folder ahead of time? This is a lot of data however.

    If anyone can point me in the correct direction I would appreciate it!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,521
    There's a lot of this question being asked lately. I'll just link what I posted five minutes ago:

    https://forum.unity.com/threads/saving-tilemap.1389024/#post-8744931

    In your case you would need to get the pixels of any customized textures written. This could mean converting them to JPG or PNG and writing them, or else getting all the pixels into an array and adding it to your data.
     
  3. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    236
    I ended up creating a gameobject which has a script that has a serialized material, texture, and gameobject array. These contain all of the possible materials, textures, and objects that could possibly be bought in the store (which are already contained in my project anyway).

    Then I save the names of the equipped materials in a JSON file. Then when I need to load the character (hats, shoes, skin) I just retrieve the name of the object/material/texture from the gameobject I mentioned before.

    The only issue with this is that I have this object which persists through my scenes, which contains all of this serialized fields. I have called DontDestroyOnLoad on it. It does not effect performance on iOS devices tho. I have considered instead putting all of these materials, textures, and objects into the resource folder. If I run into issues I will probably try that.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,521
    NICE! Well done Angel.

    It does that... so yeah, it might be a bit heavy on memory, but if you can handle it, USE the memory!

    It also means there will be less or no load times or load "lags" for you!

    This would work equally well, since you already have it as names. Just be sure when you load:

    Always use
    Resources.Load<T>()
    , never use
    Resources.Load() as T


    https://forum.unity.com/threads/resources-load-not-working.1137463/#post-7306567

    And the same goes for
    Resources.LoadAll<T>()


    Generally you want to avoid writing code that is simply waiting to mysteriously fail simply by the future appearance of an identically-named file of a different type.
     
  5. angeldevelopment

    angeldevelopment

    Joined:
    Sep 28, 2022
    Posts:
    236
    Thank you for your input....It would be nice to have someone I can talk to about thiss stuff if you would want to exchange info ? angeldevsoftware@gmail.com