I am developing for Android.In my game data is in objects which aren't derived from MonoBehaviour.So I am using binary serializer and taking references from the gameobject.It works, but I have too many variables to save most of variables are in script attached to gameobject Gamemanager So I made a prefab of gameobject of Gamemanager and saved it using prefabutility and load it again It works but Can someone tells me whether this is right approach or not, please??
This is going to work as long as you run your project from the editor. But when you try to actually build it, you will notice that it doesn't compile. The reason for that is that the class PrefabUtility is part of the UnityEditor packet, which doesn't get added to real builds of your game.
I ran into the same trap in my last project. I was lucky that I only used PrefabUtility in my map editor which I was going to dummy out of the release build anyway.
You will have to find a different solution to serialize your gamestate.
One possible alternative is the class JSONUtility which allows you to convert objects to JSON and back. It doesn't work on complete game objects, but it works with anything which extends
ScriptableObject and with any plain C# class when it has the
[System.Serializable] attribute. Note that it will only serialize the
public variables and properties of your classes.
Use a ScriptableObject instead and mark the classes you want to serialize in it with [System.Serializable]. Mark each field you want to serialize into the .asset file with [SerializeField] or make it public.
When you've created the asset file, put it in the Resources folder and load it runtime with Resources.Load("assetPath"). Since it's an asset you should not have to Instantiate it once you load.
This handles serialization in the same way as a monobehaviour, but in an asset file on disk instead. It can handle arbitrarily much data up to a certain limit. If you have more than that, you should reorganize your data in multiple files instead.