public static GameObject LoadVoxelFileAsPrefab(VoxFileData voxel, string name, string path = "Assets/", int lodLevel = 0) { Debug.Assert(!String.IsNullOrEmpty(name)); GameObject gameObject = null; try { gameObject = LoadVoxelFileAsGameObject(name, voxel, lodLevel); var prefabPath = path + name + ".prefab"; var prefab = PrefabUtility.CreateEmptyPrefab(prefabPath); var prefabTextures = new Dictionary <string, int>(); for (int i = 0; i < gameObject.transform.childCount; i++) { var subObject = gameObject.transform.GetChild(i); var meshFilter = subObject.GetComponent <MeshFilter>(); if (meshFilter != null) { AssetDatabase.AddObjectToAsset(meshFilter.sharedMesh, prefabPath); } var renderer = subObject.GetComponent <MeshRenderer>(); if (renderer != null) { if (renderer.sharedMaterial != null) { AssetDatabase.AddObjectToAsset(renderer.sharedMaterial, prefabPath); var textureName = renderer.sharedMaterial.mainTexture.name; if (!prefabTextures.ContainsKey(textureName)) { prefabTextures.Add(textureName, 1); AssetDatabase.AddObjectToAsset(renderer.sharedMaterial.mainTexture, prefabPath); } } } } return(PrefabUtility.ReplacePrefab(gameObject, prefab, ReplacePrefabOptions.ReplaceNameBased)); } finally { GameObject.DestroyImmediate(gameObject); } }
public static VoxFileData Load(string path) { using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { if (stream == null) { throw new System.Exception("Failed to open file for FileStream."); } using (var reader = new BinaryReader(stream)) { VoxFileData voxel = new VoxFileData(); voxel.hdr.header = reader.ReadBytes(4); voxel.hdr.version = reader.ReadInt32(); if (voxel.hdr.header[0] != 'V' || voxel.hdr.header[1] != 'O' || voxel.hdr.header[2] != 'X' || voxel.hdr.header[3] != ' ') { throw new System.Exception("Bad Token: token is not VOX."); } if (voxel.hdr.version != 150) { throw new System.Exception("The version of file isn't 150 that version of vox, tihs version of file is " + voxel.hdr.version + "."); } voxel.main.name = reader.ReadBytes(4); voxel.main.chunkContent = reader.ReadInt32(); voxel.main.chunkNums = reader.ReadInt32(); if (voxel.main.name[0] != 'M' || voxel.main.name[1] != 'A' || voxel.main.name[2] != 'I' || voxel.main.name[3] != 'N') { throw new System.Exception("Bad Token: token is not MAIN."); } if (voxel.main.chunkContent != 0) { throw new System.Exception("Bad Token: chunk content is " + voxel.main.chunkContent + ", it should be 0."); } if (reader.PeekChar() == 'P') { voxel.pack.name = reader.ReadBytes(4); if (voxel.pack.name[0] != 'P' || voxel.pack.name[1] != 'A' || voxel.pack.name[2] != 'C' || voxel.pack.name[3] != 'K') { throw new System.Exception("Bad Token: token is not PACK"); } voxel.pack.chunkContent = reader.ReadInt32(); voxel.pack.chunkNums = reader.ReadInt32(); voxel.pack.modelNums = reader.ReadInt32(); if (voxel.pack.modelNums == 0) { throw new System.Exception("Bad Token: model nums must be greater than zero."); } } else { voxel.pack.chunkContent = 0; voxel.pack.chunkNums = 0; voxel.pack.modelNums = 1; } voxel.chunkChild = new VoxFileChunkChild[voxel.pack.modelNums]; for (int i = 0; i < voxel.pack.modelNums; i++) { var chunk = new VoxFileChunkChild(); chunk.size.name = reader.ReadBytes(4); chunk.size.chunkContent = reader.ReadInt32(); chunk.size.chunkNums = reader.ReadInt32(); chunk.size.x = reader.ReadInt32(); chunk.size.y = reader.ReadInt32(); chunk.size.z = reader.ReadInt32(); if (chunk.size.name[0] != 'S' || chunk.size.name[1] != 'I' || chunk.size.name[2] != 'Z' || chunk.size.name[3] != 'E') { throw new System.Exception("Bad Token: token is not SIZE"); } if (chunk.size.chunkContent != 12) { throw new System.Exception("Bad Token: chunk content is " + chunk.size.chunkContent + ", it should be 12."); } chunk.xyzi.name = reader.ReadBytes(4); if (chunk.xyzi.name[0] != 'X' || chunk.xyzi.name[1] != 'Y' || chunk.xyzi.name[2] != 'Z' || chunk.xyzi.name[3] != 'I') { throw new System.Exception("Bad Token: token is not XYZI"); } chunk.xyzi.chunkContent = reader.ReadInt32(); chunk.xyzi.chunkNums = reader.ReadInt32(); if (chunk.xyzi.chunkNums != 0) { throw new System.Exception("Bad Token: chunk nums is " + chunk.xyzi.chunkNums + ",i t should be 0."); } var voxelNums = reader.ReadInt32(); var voxels = new byte[voxelNums * 4]; if (reader.Read(voxels, 0, voxels.Length) != voxels.Length) { throw new System.Exception("Failed to read voxels"); } chunk.xyzi.voxels = new VoxData(voxels, chunk.size.x, chunk.size.y, chunk.size.z); voxel.chunkChild[i] = chunk; } if (reader.BaseStream.Position < reader.BaseStream.Length) { byte[] palette = reader.ReadBytes(4); if (palette[0] != 'R' || palette[1] != 'G' || palette[2] != 'B' || palette[3] != 'A') { throw new System.Exception("Bad Token: token is not RGBA"); } voxel.palette.chunkContent = reader.ReadInt32(); voxel.palette.chunkNums = reader.ReadInt32(); var bytePalette = new byte[voxel.palette.chunkContent]; reader.Read(bytePalette, 0, voxel.palette.chunkContent); voxel.palette.values = new uint[voxel.palette.chunkContent / 4]; for (int i = 4; i < bytePalette.Length; i += 4) { voxel.palette.values[i / 4] = BitConverter.ToUInt32(bytePalette, i - 4); } } else { voxel.palette.values = new uint[256]; _paletteDefault.CopyTo(voxel.palette.values, 0); } return(voxel); } } }