Exemplo n.º 1
0
        private void InitializeScenes()
        {
            //get all initializers
            var allInitializers = new List <INIT001_Initialize>(ComponentExtensions.FindAllComponentsOfType <INIT001_Initialize>());

            //initialize all initializers
            foreach (var initializer in allInitializers)
            {
                if (initializer.initOn == INIT001_Initialize.InitOn.SceneStart)
                {
                    initializer.StartInitialization();
                }
            }
        }
Exemplo n.º 2
0
        public void LoadGameState()
        {
            Debug.Log("Start loading game @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay);

            // Clean up & prepare
            loadingPrepared = false;
            loadedBodyData  = new SaveFile_BodyData();
            initializers.Clear();

            // 0. Read file and convert Json
            string fileContent = System.IO.File.ReadAllText(saveGameFilePath); // read file from disk

            // 1. Deserialize
            SaveFileSerializer.Deserialize("Head", fileContent, encryptSaveFile, ref HeadData);
            SaveFileSerializer.Deserialize("Body", fileContent, encryptSaveFile, ref loadedBodyData);

            // 2. Create ScriptableObject Instances and load data into them
            foreach (var data in loadedBodyData.scriptableObjectDatas)
            {
                LoadScriptableObject(data);
            }

            Debug.Log("Loaded all scriptableObjects @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay);

            // 3. All Guids auf bestehenden Objekten initialisieren
            GuidManager.InitializeGuidComponents();
            Debug.Log("Initialized all GuidComponents @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay);

            // 4. Load Components
            List <SaveableComponent> components = ComponentExtensions.FindAllComponentsOfType <SaveableComponent>();

            foreach (var componentData in loadedBodyData.componentDatas)
            {
                var component = FindSaveableComponentByGuid(componentData.Id, components);
                if (component != null)
                {
                    component.RestoreData(componentData);
                }
            }

            // 5. Cleanup
            Resources.UnloadUnusedAssets();

            // 6. Loading complete
            Debug.Log("Loading complete @ frame #" + Time.frameCount + " / time: " + DateTime.Now.TimeOfDay);
        }
Exemplo n.º 3
0
        /*[Button, DisableInEditorMode]*/
        public void SaveGameState(string _saveGameFileName)
        {
            bodyDataToSave   = new SaveFile_BodyData();
            saveGameFileName = _saveGameFileName;

            // 1. Collect SaveableScriptableObjects
            ScriptableObjectsGuidMapSingleton.Instance.scriptableObjectsGuidMap.CollectAllRuntimeInstantiatedSOs(); //Set Guids for new SOs

            var saveableScriptableObjects = new List <ScriptableObject>();

            foreach (var so in scriptableObjectsToSave)
            {
                if (so == null)
                {
                    Debug.LogError($"There is a null entry in ScriptableObjectsToSave on the SaveLoadController {this.name}. Please fix.", this);
                    continue;
                }
                saveableScriptableObjects.AddRange(SaveableScriptableObjectsCollector.Collect(so));
            }

            // 2. Save ScriptableObjects
            List <SaveableScriptableObjectData> scriptableObjectsData = new List <SaveableScriptableObjectData>();

            scriptableObjectsData.AddRange(SaveLoadScriptableObjects.SaveScriptableObjects(saveableScriptableObjects));

            bodyDataToSave.scriptableObjectDatas.AddRange(scriptableObjectsData);

            // 3. Save Components
            if (saveComponentsOfActiveScenes)
            {
                saveableComponents = ComponentExtensions.FindAllComponentsOfType <SaveableComponent>();
                foreach (var saveableComponent in saveableComponents)
                {
                    bodyDataToSave.componentDatas.Add(saveableComponent.StoreData());
                }
            }

            // 4. Write file
            string fileContent = string.Empty;
            // 4.1 Write version data
            var currentVersion = new Version(Application.version);

            fileContent += SaveFileSerializer.Serialize("Version", typeof(string), currentVersion.ToString(), encryptSaveFile);

            // 4.2 write head data
            HeadData     = new SaveFile_HeadData(DateTime.Now, ScreenshotTaker.TakeScreenshot(UnityEngine.Camera.main, Screen.width / 4, Screen.height / 4));
            fileContent += SaveFileSerializer.Serialize("Head", typeof(SaveFile_HeadData), HeadData, encryptSaveFile);

            // 4.3 write body data
            fileContent += SaveFileSerializer.Serialize("Body", typeof(SaveFile_BodyData), bodyDataToSave, encryptSaveFile);

            // 4.4 write to disk
            System.IO.Directory.CreateDirectory(saveGameDirectoryPath); // Create save game directory path if it doesn't already exists
            System.IO.File.WriteAllText(saveGameFilePath, fileContent); // Write file to disk

            // 5. Cleanup
            Resources.UnloadUnusedAssets();

            // 6. Finished saving
            Debug.Log("Game saved!");
        }