public void SaveEverything() { GameObject[] Blocks = GameObject.FindGameObjectsWithTag("Block"); GameObject[] Platforms = GameObject.FindGameObjectsWithTag("Platform"); BinaryFormatter bf = new BinaryFormatter(); FileStream MoneyFile = File.Create(Application.persistentDataPath + "/MoneyInfo.dat"); FileStream BlockFile = File.Create(Application.persistentDataPath + "/BlockInfo.dat"); FileStream PlatformFile = File.Create(Application.persistentDataPath + "/PlatformInfo.dat"); FileStream WorldFile = File.Create(Application.persistentDataPath + "/WorldInfo.dat"); SaveMoneyInfo HitThatMFLike = new SaveMoneyInfo(); HitThatMFLike.Money = MMRef.Money; HitThatMFLike.CostOfBoxes = MMRef.CostOfBoxes; HitThatMFLike.CostOfNewPlatform = MMRef.CostOfNewPlatform; HitThatMFLike.OwnedBoxes = MMRef.OwnedBoxes; SaveWorldInfo WorldInfo = new SaveWorldInfo(); WorldInfo.PosX = WorldBoundries.transform.position.x; WorldInfo.TransX = WorldBoundries.transform.lossyScale.x; WorldInfo.PlaySpaceWidth = WorldBoundries.transform.GetChild(2).GetComponent <ScreenEdgeWarp>().PlaySpaceWidth; bf.Serialize(WorldFile, WorldInfo); bf.Serialize(PlatformFile, new SavePlatforms(Platforms)); bf.Serialize(BlockFile, new SaveBlocks(Blocks)); bf.Serialize(MoneyFile, HitThatMFLike); WorldFile.Close(); PlatformFile.Close(); MoneyFile.Close(); BlockFile.Close(); }
// 46:35 of the video, Live Training 3 mar 2014 - data persistene public void LoadThatBitch() { BinaryFormatter bf = new BinaryFormatter(); if (File.Exists(Application.persistentDataPath + "/MoneyInfo.dat")) { FileStream file = File.Open(Application.persistentDataPath + "/MoneyInfo.dat", FileMode.Open); SaveMoneyInfo HTMFL = (SaveMoneyInfo)bf.Deserialize(file); file.Close(); MMRef.Money = HTMFL.Money; MMRef.CostOfBoxes = HTMFL.CostOfBoxes; MMRef.OwnedBoxes = HTMFL.OwnedBoxes; MMRef.CostOfNewPlatform = HTMFL.CostOfNewPlatform; Array.Clear(MMRef.MakingMoneyBoxes, 0, MMRef.MakingMoneyBoxes.Length); } if (File.Exists(Application.persistentDataPath + "/BlockInfo.dat")) { GameObject[] Blocks = GameObject.FindGameObjectsWithTag("Block"); for (int i = 0; i < Blocks.Length; i++) { Destroy(Blocks[i]); } FileStream BlockFile = File.Open(Application.persistentDataPath + "/BlockInfo.dat", FileMode.Open); SaveBlocks SavedBlocks = (SaveBlocks)bf.Deserialize(BlockFile); BlockFile.Close(); foreach (SaveBlock CBlock in SavedBlocks.blocks) { Vector3 BlockPos = new Vector3(CBlock.x, CBlock.y, CBlock.z); Quaternion quat = new Quaternion(CBlock.qx, CBlock.qy, CBlock.qz, CBlock.qw); Instantiate(BlockPrefabs[CBlock.level], BlockPos, quat); } } if (File.Exists(Application.persistentDataPath + "/WorldInfo.dat")) { FileStream WorldFile = File.Open(Application.persistentDataPath + "/BlockInfo.dat", FileMode.Open); SaveWorldInfo jeff = (SaveWorldInfo)bf.Deserialize(WorldFile); WorldFile.Close(); WorldBoundries.transform.position = new Vector3(jeff.PosX, 0, 0); WorldBoundries.transform.localScale = new Vector3(jeff.TransX, 1, 1); } }