public static void SaveToFile(string filename, FoliageData data) { string path = Path.Combine(Application.streamingAssetsPath, filename); Debug.Log("Saving runtime grass to file: " + path); // Write the grass runtime to the file if (File.Exists(path)) { File.Delete(path); } using (BinaryWriter writer = new BinaryWriter(new BufferedStream(File.Open(path, FileMode.OpenOrCreate)))) { writer.Write(FoliageGlobals.DISK_IDENTIFIER); writer.Write(FoliageGlobals.DISK_VERSION); // Remove any empty data so that we don't have any empty cells that we're going to have to test for data.RemoveEmptyData(); WriteFoliageData(writer, data); } // Just print out some information #if UNITY_EDITOR { int instances = data.GetInstanceCount(); Debug.Log(string.Format("Written foliage data with [{0} Cells] and [{1} Instances].", data.m_FoliageData.Count, instances)); } #endif }
/** * Loads the edited grass that was added at edit time. It can even be an empty file, * since we can add all the grass data that we need at runtime. * * @param filename * Filename where we should load the data from * @param loadRuntimeDataOnly * To be used at runtime only. If this is 'true' it will not load the whole hierarchy * but only the runtime use intended data. Set this to false when loading from the * 'FoliagePainter' that is intended for edit time use and 'false' when loading from * the 'FoliageRenderer' that is intended for runtime use */ public static FoliageData LoadFromFileEditTime(string filename) { string path = Path.Combine(Application.streamingAssetsPath, filename); Debug.Log("Loading runtime foliage from file: " + path); FoliageData data = new FoliageData(); // Ensure the file exists if (File.Exists(path)) { // Read the runtime grass from a file // using (BinaryReader reader = new BinaryReader(new MemoryStream(File.ReadAllBytes(path)))) using (BinaryReader reader = new BinaryReader(new BufferedStream(File.OpenRead(path)))) { ulong ID = reader.ReadUInt64(); int version = reader.ReadInt32(); if (ID == FoliageGlobals.DISK_IDENTIFIER) { Debug.Log(string.Format("Reading file with identifier: {0} and version: {1}", ID, version)); ReadFoliageData(reader, data); } else { Debug.LogError("Foliage data has been tampered with! Delete it!"); } } } else { Debug.LogWarning("Warning, no grass file data exists! Save the grass!"); } #if UNITY_EDITOR { int instances = data.GetInstanceCount(); Debug.Log(string.Format("Read foliage data with [{0} Cells] and [{1} Instances].", data.m_FoliageData.Count, instances)); } #endif return(data); }