Helper to serialize Vector3 using BinaryFormatter
상속: ISerializationSurrogate
예제 #1
0
		/// <summary>
		/// Load the preset file at the specified path into the specified asset and cook it.
		/// </summary>
		/// <param name="asset">Asset to load preset into</param>
		/// <param name="filePath">Full path to file containing preset. File must have been written out by SaveAssetPresetToFile.</param>
		public static void LoadPresetFileIntoAssetAndCook(HEU_HoudiniAsset asset, string filePath)
		{
			try
			{
				HEU_AssetPreset assetPreset = null;

				using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
				{
					BinaryFormatter formatter = new BinaryFormatter();

					HEU_Vector3SerializationSurrogate vector3S = new HEU_Vector3SerializationSurrogate();
					HEU_Vector2SerializationSurrogate vector2S = new HEU_Vector2SerializationSurrogate();

					SurrogateSelector surrogateSelector = new SurrogateSelector();
					surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3S);
					surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2S);
					formatter.SurrogateSelector = surrogateSelector;

					assetPreset = (HEU_AssetPreset)formatter.Deserialize(fs);
				}

				if(assetPreset != null)
				{
					if (PRESET_IDENTIFIER.SequenceEqual(assetPreset._identifier))
					{
						asset.LoadAssetPresetAndCook(assetPreset);
					}
					else
					{
						Debug.LogErrorFormat("Unable to load preset. Specified file is not a saved HDA preset: {0}", filePath);
					}
				}
				else
				{
					Debug.LogErrorFormat("Failed to load preset file {0}.", filePath);
				}
			}
			catch (System.Exception ex)
			{
				Debug.LogErrorFormat("Failed to load preset due to exception: " + ex.ToString());
			}
		}
예제 #2
0
		/// <summary>
		/// Save the specified asset's preset data to file at specified path.
		/// </summary>
		/// <param name="asset">The asset's preset data will be saved</param>
		/// <param name="filePath">The file to save to</param>
		public static void SaveAssetPresetToFile(HEU_HoudiniAsset asset, string filePath)
		{
			// This should return an object filled with preset data, and which we can serialize directly
			HEU_AssetPreset assetPreset = asset.GetAssetPreset();

			if (assetPreset != null)
			{
				try
				{
					int len = PRESET_IDENTIFIER.Length;

					assetPreset._identifier = PRESET_IDENTIFIER;
					assetPreset._version = PRESET_VERSION;

					using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write))
					{
						IFormatter formatter = new BinaryFormatter();

						HEU_Vector3SerializationSurrogate vector3S = new HEU_Vector3SerializationSurrogate();
						HEU_Vector2SerializationSurrogate vector2S = new HEU_Vector2SerializationSurrogate();

						SurrogateSelector surrogateSelector = new SurrogateSelector();
						surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3S);
						surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2S);
						formatter.SurrogateSelector = surrogateSelector;

						formatter.Serialize(fs, assetPreset);
					}
				}
				catch (System.Exception ex)
				{
					Debug.LogErrorFormat("Failed to save preset due to exception: " + ex.ToString());
				}
			}
			else
			{
				Debug.LogErrorFormat("Failed to save preset due to unable to retrieve the preset buffer!");
			}
		}