void OnSaveFile_ForLocal() { // get all the objects Transform blocks_grid = this.transform.GetChild(0); // extract the grid name from the first block's name up to the first "_" character string name = blocks_grid.GetChild(0).name; name = name.Substring(0, name.IndexOf("_")); // detect how many blocks wide and high the grid is // todo: 1000 should be replaced by blocks_grid.ChildCount to support arbitrary grid sizes // todo: but there are 2x the expected children in the grids somehow, so it's failing int width = blocksList.width; int height = blocksList.height; // create a 2D grid to place the GameObjects in GameObject[,] blocksGameObjectList2D = new GameObject[width, height]; for (int nIndex = 0; nIndex < width; nIndex++) { for (int mIndex = 0; mIndex < height; mIndex++) { blocksGameObjectList2D[nIndex, mIndex] = blocks_grid.GetChild(nIndex * height + mIndex).gameObject; } } blocksList = new BlockDataList(name, blocksGameObjectList2D); // Serialze into JSON format string blockListText = JSONSerializeBlockDataList(blocksList); // Save JSON string as file to Documents folder SaveBlockDataListToRoamingFolder(blockListText, name); }
private BlockDataList JSONDeserializeBlockDataList(string JSONdata) { DataContractJsonSerializer jsonSer = new DataContractJsonSerializer(typeof(BlockDataList)); Debug.Log(JSONdata); MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(JSONdata)); BlockDataList returnBlockDataList = (BlockDataList)jsonSer.ReadObject(stream); return(returnBlockDataList); }
void createBlocksFromFile(string fileName, bool SpawnSharedObjects) { // load file from documents string blocksJSON = LoadBlocksFromRoamingFolder(fileName).Result; // if no file was found or failed, load from app resource file URI if (blocksJSON == null) { blocksJSON = readBlocksJSONFromAppResource(fileName).Result; } // convert JSON string to blocks data object blocksList = JSONDeserializeBlockDataList(blocksJSON); // create the block game objects createBlocksFromDataList(blocksList, SpawnSharedObjects); }
private void createBlocksFromDataList(BlockDataList blocksDataList, bool SpawnSharedObjects) { float nOffset = blocksDataList.width / 2; // sets the center of grid the grid to 0 float mOffset = 0; // sets bottom of grid to 0 (to center grid at 0 instead, use "mRows / 2"); Quaternion BlockRotation; float blockSpacing = Globals.BlockSpacing; // set blocks up for each column of the grid for (int nIndex = 0; nIndex < blocksDataList.width; nIndex++) { // set blocks up for each row of the grid for (int mIndex = 0; mIndex < blocksDataList.height; mIndex++) { // get rotation from input block grid if it exists BlockRotation = Quaternion.Euler(blocksDataList.BlockData2DArray[nIndex, mIndex].Rotation); if (SpawnSharedObjects) { // instantiate a shared cube in the right spot on the grid this.spawnManager.Spawn( new SyncSpawnedObject(), new Vector3( (nIndex - nOffset) * Globals.BlockSpacing, 1 * blockSpacing, (mIndex - mOffset + 1) * Globals.BlockSpacing), BlockRotation, this.transform.GetChild(0).gameObject, blocksDataList.Name, false); } else { // instantiate a local cube in the right spot on the grid GameObject NewBlock = Instantiate(block_prefab, this.transform.GetChild(0), false); NewBlock.transform.localRotation = BlockRotation; NewBlock.transform.localPosition = new Vector3( (nIndex - nOffset) * Globals.BlockSpacing, 1 * Globals.BlockSpacing, (mIndex - mOffset + 1) * Globals.BlockSpacing); NewBlock.name = blocksDataList.Name + "_" + mIndex.ToString() + "-" + nIndex.ToString(); } } } }
// Handler to load sample files void OnLoadFiles_ForSlideShow(string[] fileNames) { blocksJSONList = new string[fileNames.Length]; for (int i = 0; i < fileNames.Length; i++) { // read blocks JSON string from file blocksJSONList[i] = readBlocksJSONFromAppResource(fileNames[i]).Result; } // convert JSON string to blocks data object blocksList = JSONDeserializeBlockDataList(blocksJSONList[blocks_SideShow_CurrentListItem]); // create the block game objects using the first file createBlocksFromDataList(blocksList, false); // scale up a bit this.transform.localScale = this.transform.localScale * scaleFactor; }
void OnLoadNextBlocks_ForSlideShow() { // rotate to next item in slideshow, if at end, set it back to first item if (blocks_SideShow_CurrentListItem < (blocksJSONList.Length - 1)) { blocks_SideShow_CurrentListItem += 1; } else { blocks_SideShow_CurrentListItem = 0; } // load the .blocks data into blocks datastructure blocksList = JSONDeserializeBlockDataList(blocksJSONList[blocks_SideShow_CurrentListItem]); // rotate all the SlideShow's blocks to the new orientation for (int i = 0; i < blocksList.BlockDataArray.Length; i++) { this.transform.GetChild(0).GetChild(i).gameObject.SendMessage("OnRotateAbsolute", Quaternion.Euler(blocksList.BlockDataArray[i].Rotation)); } }