0

I have built a game in Unity3D where the player can place models, rotate them and scale them.

Now what I want is to add an option to save the current state.

What I need is to save for every model is simply the scale, position, rotation and the model's mesh.

I know how to save all the properties except for the Mesh. I planned to save the data in JSON file but it can't save the Mesh nor the GameObject.

I have reference to all the instanced models so there is no need to find them.

Edit: To sum up, I am looking for a way to save the selected models so I would know which models to load when the user wants to load the file. But I can't serialize the GameObject nor the mesh, and therefore using JSON or binary file doesn't work. All I have been able to save so far is the position, rotation and scale of each models.

How can I save the selected models¿

SagiZiv
  • 932
  • 1
  • 16
  • 38
  • You could maybe simply use the [Scene OBJ Exporter](https://assetstore.unity.com/packages/tools/utilities/scene-obj-exporter-22250) combined with the [Runtime OBJ Importer](https://assetstore.unity.com/packages/tools/modeling/runtime-obj-importer-49547). In general: Questions asking for a tool or library are off-topic for StackOverflow. Rather make your own research, try implement something and if you then have a specific issue during the implementation we are glad to help (see [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)) – derHugo Sep 02 '19 at 16:18
  • What's your question? – TylerH Sep 02 '19 at 16:41
  • @derHugo Sorry about that, I am not next to my computer so I can't add the code I have tried. basically what I tried was to save the entire GameObject to binary file but it didn't work so I tried to save to JSON file that failed again for the same reason (GameObject is not serializable) – SagiZiv Sep 02 '19 at 16:56
  • @TylerH I am looking for a way to save the scene that the user created at runtime – SagiZiv Sep 02 '19 at 16:57
  • Possible duplicate of [What is the best way to save game state?](https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state) – Draco18s no longer trusts SE Sep 02 '19 at 18:01
  • @Draco18s Thanks for the link, I know how to use JSON, what I am having difficulty with, is saving UnityObjects, e.g. GameObject or Transform. – SagiZiv Sep 02 '19 at 18:21
  • @SagiZiv You can't save `GameObject`s or `Transforms`: you have to save all of the information necessary to *recreate* it: position, rotation, etc. – Draco18s no longer trusts SE Sep 02 '19 at 18:26

1 Answers1

1

The mesh is not generated on runtime right? He has a collection of models that you have as prefabs that he can place. So just save as a string or enum which model each position has, then to load the saved scene you instantiate the correct prefab of the model according to the save at the saved position and rotation.

public class PrefabHolder : MonoBehaviour //assign all necessary prefabs here, also used for logging on mobileDebugText
    {
     public static PrefabHolder instance;

 public GameObject prefabXYZ;

    private void Awake()
    {
        instance = this;
    }

Acess any model you want from any script with Prefabholder.instance.prefabXYZ

  • So what you suggest is to save the file and then load it using Resources.Load¿ – SagiZiv Sep 02 '19 at 16:58
  • I meant save the model's name – SagiZiv Sep 02 '19 at 17:08
  • You dont need to use Resources for prefabs right? Just have one Script to assign all the models that you have as prefabs so you can always access and instantiate them whenever, wherever and in whatever rotation you want. I edited my Comment to include an example Prefabholder Script that i use. – Kjell Schwaricke Sep 03 '19 at 07:55
  • And yes exactly, saving the models name would be enough, so then the mesh/model for recreating the save state can just be found out with a switch case for each name, with the according model from the Prefabholder as return value – Kjell Schwaricke Sep 03 '19 at 08:23
  • 1
    Got it, this is brilliant. Thank you so much – SagiZiv Sep 03 '19 at 11:29