public static SystemSimulationData LoadSavedStarSystem(string name)
    {
        string path = Application.persistentDataPath + "/savedSystems/" + name + ".data";

        if (File.Exists(path))
        {
            string result = File.ReadAllText(path);
            Debug.Log("loadResult: " + result);
            SystemSimulationData data = JsonUtility.FromJson <SystemSimulationData>(result);

            TrajectoryVelocity.startSlingshot   = false;
            TrajectoryVelocity.start            = new Vector3(0f, 0f, 0f);
            SimulationPauseControl.gameIsPaused = false;
            TrajectorySimulation.drawLine       = false;
            TrajectorySimulation.destroyLine    = false;
            TrajectorySimulation.freeze         = false;
            TrajectorySimulation.shoot          = false;
            ToggleGravityMode.nBodyGravity      = data.gravityState;
            var gToggle = Object.FindObjectOfType <ToggleGravityMode>();
            gToggle.ChangeToggleStart();

            return(data);
        }
        else
        {
            return(null);
        }
    }
    public static bool SaveStarSystem(bool addNew, string fileName)
    {
        SystemSimulationData simData = new SystemSimulationData(CelestialObject.Objects.Count, addNew);

        string path    = Application.persistentDataPath + fileName;
        string content = JsonUtility.ToJson(simData);

        return(SaveToFile(path, content));
    }
    public static bool SaveSpecificStarSystem(bool addNew, string fileName)
    {
        SystemSimulationData simData = new SystemSimulationData(CelestialObject.Objects.Count, addNew);
        string path = Application.persistentDataPath + "/savedSystems/";

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
        path += fileName;
        string content = JsonUtility.ToJson(simData);

        return(SaveToFile(path, content));
    }
 GameObject getPrefab(SystemSimulationData data, int i)
 {
     for (int j = 0; j < data.planetList.Length; j++)
     {
         if (data.planetList[j].id == data.physicsData[i].id)
         {
             return(Instantiate(rockPrefab));
         }
     }
     for (int j = 0; j < data.gasPlanetList.Length; j++)
     {
         if (data.gasPlanetList[j].id == data.physicsData[i].id)
         {
             return(Instantiate(gasPrefab));
         }
     }
     return(Instantiate(gasPrefab));
 }
    void Update()
    {
        if (save)
        {
            if (sceneIndex == 2)
            {
                //Save one planet to newPlanet.data
                SaveLoadStarSystem.SaveStarSystem(true, "/newPlanet.data");
                save = false;
            }
            else if (sceneIndex == 1)
            {
                //Save the entire system to system.data
                if (saveSpecific)
                {
                    SaveLoadStarSystem.SaveSpecificStarSystem(false, systemName + ".data");
                    saveSpecific = false;
                }
                else
                {
                    SaveLoadStarSystem.SaveStarSystem(false, "/system.data");
                    if (!firstGenSystemSaved)
                    {
                        firstGenSystemSaved = true;
                    }
                }
                save = false;
            }
        }
        if (load)
        {
            if (sceneIndex == 1)
            {
                //Load the entire system from system.data
                SystemSimulationData data;
                if (loadSpecific)
                {
                    data         = SaveLoadStarSystem.LoadSavedStarSystem(systemName);
                    loadSpecific = false;
                }
                else
                {
                    if (firstGenSystemSaved)
                    {
                        data = SaveLoadStarSystem.LoadStarSystem(false);
                    }
                    else
                    {
                        data = null;
                    }
                    load = false;
                }
                if (data != null)
                {
                    //CelestialObject.DestroyAll();

                    var planets = FindObjectsOfType <CelestialObject>();
                    foreach (CelestialObject co in planets)
                    {
                        if (!co.staticBody)
                        {
                            Destroy(co.gameObject);
                        }
                    }

                    int        rocky_i = 0;
                    int        gasy_i  = 0;
                    GameObject parent  = GameObject.Find("SceneObjects");
                    for (int i = 0; i < data.planetCount; i++)
                    {
                        GameObject obj = getPrefab(data, i);


                        MotherPlanet mp = obj.GetComponentInChildren <MotherPlanet>();
                        if (mp != null)
                        {
                            mp.GeneratePlanet();
                            mp.SetShape(data.planetList[rocky_i]);
                            mp.UpdateMesh();
                            rocky_i += 1;
                        }
                        //GasPlanetShaderMAterialPropertyBlock gp = co.GetComponentInChildren<GasPlanetShaderMAterialPropertyBlock>();
                        GasPlanetShaderMAterialPropertyBlock gp = obj.GetComponent <GasPlanetShaderMAterialPropertyBlock>();
                        if (gp != null)
                        {
                            gp.SetMaterial(data.gasPlanetList[gasy_i]);
                            gasy_i += 1;
                        }
                        //obj.GetComponent<SphereCollider>().enabled=false;

                        obj.GetComponent <CelestialObject>().enabled = true;

                        CelestialObject co = obj.GetComponent <CelestialObject>();
                        //print("position");
                        //print(data.physicsData[i].position);
                        //print("----------");
                        co.SetState(data.physicsData[i]);
                        obj.transform.SetParent(parent.transform);
                    }
                    load = false;
                }
                else
                {
                    load = false;
                    Debug.Log("failed to load entire system");
                }
            }
        }
        if (loadNewPlanet)
        {
            if (sceneIndex == 1)
            {
                loadNewPlanet = false;
                //Load one planet from newPlanet data and p
                SystemSimulationData data = SaveLoadStarSystem.LoadStarSystem(true);
                if (data != null)
                {
                    //CelestialObject.DestroyAll();
                    int rocky_i = 0;
                    int gasy_i  = 0;
                    for (int i = 0; i < data.planetCount; i++)
                    {
                        GameObject      obj = getPrefab(data, i);
                        CelestialObject co  = GetComponent <CelestialObject>();
                        if (co != null)
                        {
                            obj.AddComponent(typeof(CelestialObject));
                        }
                        obj.GetComponent <CelestialObject>().isShot = false;
                        //obj.GetComponent<IcoPlanet>().enabled = true;

                        MotherPlanet mp = obj.GetComponentInChildren <MotherPlanet>();
                        if (mp != null)
                        {
                            mp.GeneratePlanet();
                            mp.SetShape(data.planetList[rocky_i]);
                            mp.UpdateMesh();
                            mp.GenerateColors();
                            mp.GetComponent <IcoPlanet>().staticBody = false;
                            mp.GetComponent <IcoPlanet>().enabled    = true;

                            rocky_i += 1;
                        }
                        GasPlanetShaderMAterialPropertyBlock gp = obj.GetComponentInChildren <GasPlanetShaderMAterialPropertyBlock>();
                        if (gp != null)
                        {
                            gp.SetMaterial(data.gasPlanetList[gasy_i]);
                            gp.GetComponent <GasPlanetShaderMAterialPropertyBlock>().enabled = true;
                            gasy_i += 1;
                        }
                        string newPlanetName = PlayerPrefs.GetString("NewPlanetName", "Unknown Planet");
                        obj.GetComponent <CelestialObject>().SetName(newPlanetName);
                        PlayerPrefs.SetString("NewPlanetName", "Unknown Planet");
                        if (!obj.GetComponent <CelestialObject>().isAsteroid)
                        {
                            obj.GetComponent <CelestialObject>().SetMass();
                        }
                        GameObject            ARSessOrig = GameObject.Find("AR Session Origin");
                        ARPlacementTrajectory placement  = ARSessOrig.GetComponent <ARPlacementTrajectory>();
                        placement.setGOtoInstantiate(obj);
                    }
                }
            }
        }
        if (delete)
        {
            if (sceneIndex == 1)
            {
                SaveLoadStarSystem.DeleteStarSystem(delSystemName);
                delete = false;
            }
        }
    }