Skip to content

Instantly share code, notes, and snippets.

@wakeup5
Created August 23, 2019 06:58
Show Gist options
  • Save wakeup5/d2d4e7ff446de9b46dd587948df3d927 to your computer and use it in GitHub Desktop.
Save wakeup5/d2d4e7ff446de9b46dd587948df3d927 to your computer and use it in GitHub Desktop.
How to Serialize and Deserialize Sprite and AudioClip to Binary.
[System.Serializable]
public class AudioData
{
public string name;
public int channels;
public int samples;
public int frequency;
public float[] data;
public static AudioData FromAudioClip(AudioClip audioClip)
{
AudioData result = new AudioData();
result.name = audioClip.name;
result.channels = audioClip.channels;
result.samples = audioClip.samples;
result.frequency = audioClip.frequency;
result.data = new float[audioClip.samples * audioClip.channels];
audioClip.GetData(result.data, 0);
return result;
}
public static AudioClip ToAudioClip(AudioData audio)
{
AudioClip result = AudioClip.Create(audio.name, audio.samples, audio.channels, audio.frequency, false);
result.SetData(audio.data, 0);
return result;
}
}
public class Data
{
public string name;
public Sprite image;
public AudioClip audio;
}
public class SaveData
{
public string name;
public SpriteData image;
public AudioData audio;
public SaveData(Data data)
{
name = data.name;
image = SpriteData.FromSprite(data.image);
audio = AudioData.FromAudioClip(data.audio);
}
public Data ToData()
{
Data data = new Data();
data.name = name;
data.image = SpriteData.ToSprite(image);
data.audio = AudioData.ToAudioClip(audio);
return data;
}
}
public class SaveLoader : MonoBehaviour
{
public Data data;
private void Update()
{
if (Input.GetKeyDown(KeyCode.F2))
{
Save();
Clear();
}
if (Input.GetKeyDown(KeyCode.F3))
{
Load();
}
}
private void Save()
{
using (Stream stream = File.Open("Data.bin", FileMode.OpenOrCreate))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, new SaveData(data));
stream.Flush();
stream.Close();
}
}
private void Clear()
{
data = new Data();
}
private void Load()
{
using (Stream stream = File.Open("Data.bin", FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
SaveData sd = formatter.Deserialize(stream) as SaveData;
data = sd.ToData();
stream.Flush();
stream.Close();
}
}
}
[System.Serializable]
public class SpriteData
{
public string name;
public float xMin;
public float xMax;
public float yMin;
public float yMax;
public float pivotX;
public float pivotY;
public byte[] data;
public static SpriteData FromSprite(Sprite sprite)
{
SpriteData result = new SpriteData();
result.name = sprite.name;
result.xMin = sprite.rect.xMin;
result.xMax = sprite.rect.xMax;
result.yMin = sprite.rect.yMin;
result.yMax = sprite.rect.yMax;
result.pivotX = sprite.pivot.x;
result.pivotY = sprite.pivot.y;
result.data = sprite.texture.EncodeToPNG();
return result;
}
public static Sprite ToSprite(SpriteData data)
{
Rect rect = new Rect();
rect.xMin = data.xMin;
rect.xMax = data.xMax;
rect.yMin = data.yMin;
rect.yMax = data.yMax;
Vector2 pivot = new Vector2();
pivot.x = data.pivotX;
pivot.y = data.pivotY;
Texture2D texture = new Texture2D(2, 2);
texture.LoadImage(data.data);
Sprite result = Sprite.Create(texture, rect, pivot);
result.name = data.name;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment