Search Unity

Scriptable Object to save at runtime

Discussion in 'Scripting' started by MrZeker, Oct 9, 2019.

  1. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    Hello, I'm making a customization for my game. Characters are stored in scriptable objects, and I'm using a simple binary serialization for saving data. I thought that combining those two I could make the character customization,but the serialize doesn't take into account meshes and materials.
    I have read that I might be able to achieve something like that with json, however I don't know how to use it.
    Is there any better way or simpler way to do it?
    I need to store the information on which mesh goes where and what material for each mesh,and a bunch of ints.
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Do the mesh/materials change during runtime?
     
  3. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    yes, at least once, and i need to keep those changes from play session to play session.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You don't need to save the material or mesh. What I would do is have a scriptable object which contains an array of meshes and materials which can be used as a resources object. Simply save the int index of the mesh/material which is in use. You can then restore that on load and use it to select which to use.
     
  5. MrZeker

    MrZeker

    Joined:
    Nov 23, 2018
    Posts:
    227
    the problem is that i have over 200 materials. it would take me a lot of work to make that work

    edit: the way im working with the character creator is that every Item has its own texture lists 4-5 textures per mesh aprox. if i just use one array for all textures things are going to be very ugly i guess.
     
    Last edited: Oct 19, 2019
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    How would you structure the data representation of your character creator? Off the top of my head, I'd probably assign an id/index to every object, and then save those. The structure could look something like:

    Code (CSharp):
    1. [Serializable]
    2. struct CharacterCreatorData {
    3.   int headId; // base mesh
    4.   int headSubId1;
    5.   int headSubId2;
    6.   int headSubId3; // a few fields for whatever secondary attributes you want to apply to the base head (textures, whatever)
    7.   Color headColor1;
    8.   Color headColor2;
    9.   Color headColor3; // maybe some colors?
    10.  
    11.   // rinse and repeat for other body parts
    12. }
    Your "sub ids" can then refer to the specific textures in each Item list. No need to put all textures in one array.

    Then all you have to do is create a method that will read this struct and assign everything correctly.