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

What's the better way to save/load editor data

Discussion in 'Scripting' started by Mr_Cad, Jan 7, 2014.

  1. Mr_Cad

    Mr_Cad

    Joined:
    Dec 7, 2013
    Posts:
    27
    I've written a custom editor to allow myself to key in different parameters for monster, level state information.
    For example:
    Monster Editor: I can set monster type, HP, damage, and etc.
    Level Editor: I can set total monsters for a particular level, difficulty, possible drop items and etc.

    My problem is, I don't know how can I record all these data.
    Currently my idea is to put them in XML files and load them during game launch. By doing this, I have no problem to load through editor but the problem arise when I'm trying to test the load on mobile devices because the device couldn't find/load the XML files. I have put the XML files in Resources folder but somehow it just couldn't work.

    I'm looking for better/faster way to handle this save/load and make sure it works on mobile devices as well.
    Anyone who done this before, could you please share your ideas with me on how should I handle this.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    I think I'll just write a template and just copy paste it from now on. I think - lately - it has been among the most often asked question. (Made a quick blog post: http://lightstrikersoftware.com/index.php/blog )

    In the less painful, most straight forwards, no conversion at all;

    - Save your data in .asset file using http://docs.unity3d.com/Documentation/ScriptReference/AssetDatabase.CreateAsset.html

    - Put your data in the Resources folder and load it using http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

    The good thing? Unity handle all the serialization/deserialization on its own. It also uses the same rules as what the Inspector uses for what is serializable and what isn't.
     
    Last edited: Jan 7, 2014
  3. fpuig

    fpuig

    Joined:
    Dec 26, 2012
    Posts:
    18
    If you like the XML approach, you could just save the XML to a string instead of a file.

    Then save the string in a component inside a prefab and load that prefab when you need it.

    e.g. Create a component Note { public string Data; } and assign that to a prefab.

    You can place the prefab in your scene and the data will be available all the time,
    or in the resource folder and request the data when needed.

    Nonetheless, I'm not 100% sure but I remember seeing that XML files can be retrieved from the resource or Assets folder in mobile using WWW.
     
    Last edited: Jan 7, 2014
  4. Mr_Cad

    Mr_Cad

    Joined:
    Dec 7, 2013
    Posts:
    27
    After tested, it seems like the following codes work fine on both PC and Mobile

    Code (csharp):
    1. var serializer = new XmlSerializer(typeof(MyClassContainer));
    2. TextAsset textAsset = (TextAsset)Resources.Load("data", typeof(TextAsset)); // data is the data.xml
    3. MemoryStream assetStream = new MemoryStream(textAsset.bytes);
    4. MyClassContainer container = serializer.Deserialize(assetStream) as MyClassContainer;
    5. assetStream.Close();
     
  5. JohnnyA

    JohnnyA

    Joined:
    Apr 9, 2010
    Posts:
    5,041
    I use a similar approach all the time. One thing to be careful of is there are some cases where the AOT compiler misses some generic type. This will work on desktop/web but on iOS you will get an ExecutionEngineException which can be a little hard to trace if you don't know what you are looking for.

    The solution is to include a dummy reference to the missing type.
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    unity_VA6ICiDid5q0sQ likes this.