/// <summary> /// Takes a <see cref="SaveData.TextureData"/> and converts it to a Unity Texure2D for use in game. /// </summary> /// <param name="serializedPlayerTexture2D"> <see cref="SaveData.TextureData"/> to convert. </param> /// <returns> A Unity Texture2D. </returns> public Texture2D SerializableToTexture2D(SaveData.TextureData serializedPlayerTexture2D) { Texture2D textureToLoad = new Texture2D(serializedPlayerTexture2D.width, serializedPlayerTexture2D.height); textureToLoad.LoadImage(serializedPlayerTexture2D.rawTextureData); return(textureToLoad); }
/// <summary> /// Encodes a Unity Texture2D to a PNG format and stores it to a <see cref="SaveData.TextureData"/> for saving. /// Ensure the image has Read/Write Enabled and is not compressed in the Unity Editor for this to work. /// </summary> /// <param name="playerTexture2D"> The Texture2D to convert. </param> /// <returns> Serializable <see cref="SaveData.TextureData"/> for saving. </returns> public SaveData.TextureData Texture2DToSerializable(Texture2D playerTexture2D) { SaveData.TextureData texturetoSave = new SaveData.TextureData(); texturetoSave.rawTextureData = playerTexture2D.EncodeToPNG(); texturetoSave.width = playerTexture2D.width; texturetoSave.height = playerTexture2D.height; return(texturetoSave); }
public void TextureSaveLoad() { Texture2D testTexture = (Texture2D)Resources.Load("Character1", typeof(Texture2D)); SaveData.TextureData savedTexture = PlayerData.instance.data.Texture2DToSerializable(testTexture); Texture2D restoredTexture = PlayerData.instance.data.SerializableToTexture2D(savedTexture); // Because of Unity subleties, equality of images done by first encoding to PNG. Assert.AreEqual(testTexture.EncodeToPNG(), restoredTexture.EncodeToPNG()); }