Home Game Development savegame – Best technique to save recreation information in Unity

savegame – Best technique to save recreation information in Unity

0
savegame – Best technique to save recreation information in Unity

[ad_1]

Unfortunately Unity doesn’t have a build-in savegame system. So each recreation must develop an personal savegame system which is tailor-made to the structure and necessities of that particular recreation. That means there isn’t any “greatest” technique to save information, solely the way in which which is greatest for you.

When you could have little or no information to save lots of, then utilizing the PlayerPrefs class generally is a fast and simple technique to do it. In your specific case, this might be adequate. But it’s supposed for, effectively, preferences. A brief checklist of key-value pairs. It shouldn’t be designed to carry giant quantities of information, and even permitting multiple savegame rapidly turns into messy.

So you possible will not get round inventing your individual savegame file format which you learn and write with the usual C# lessons like StreamWriter / StreamReader (for text-based codecs) or FileStream (for binary-based format).

When you solely need to save the sport state between scene modifications, then you definately solely must persist the information which you already must deal with between scenes. That makes a variety of issues lots simpler, as a result of you have already got a very good overview of what information you could have.

But if you wish to have a system which permits to save lots of and reload the sport at any time, then it will get much more difficult. Such a system must:

  • Find out which gameObjects are at the moment within the recreation
  • Read any information about them which is price persisting
  • Compile all that information right into a file format
  • Save that file
  • Read that file and parse it
  • Recreate all of the gameObjects with their elements based mostly on the content material of the file
  • Set their variables in keeping with the content material of the file

As you may see from the complexity of a few of these factors, this isn’t one thing the place one answer matches each recreation. But a pair instruments that might come in useful for creating your savegame system might be:

  • Using the Unity serialization system and see how far it will get you (sadly it has some limitations)
  • Interfaces which mark a MonoBehaviour as “Saveable” and contains strategies which flip that element into a knowledge illustration and might set the state of that element from such a knowledge illustration. (Unfortunately that will not aid you with Unity normal elements).
  • Behaviours which deal with persisting / restoring all of the elements on a selected kind of gameObject they’re on, together with Unity-specific elements. But which may create numerous upkeep work in the long term.
  • Reflection to search out out what variables an object has and try to persist / restore it routinely
    • Attributes to mark variables which ought to / shouldn’t be saved by your reflection-based system

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here