// Linked from http://unity3dhacks.blogspot.com/
/// <summary>
/// Enables stream IO on PlayerPrefs for Unity3D. Cheap replacement for FileStream on web player builds.
/// </summary>
public class PlayerPrefsStream : System.IO.Stream
{
/// <summary>
/// Creates a stream in PlayerPrefs using key "~". See overload if you want to use different key.
/// </summary>
public PlayerPrefsStream()
: this("~")
{
}
/// <summary>
/// Creates a stream in PlayerPrefs using a custom key.
/// </summary>
/// <param name="key">The location where to store the stream.</param>
public PlayerPrefsStream(string key)
{
this.key = key;
Load();
}
/// <summary>
/// Reads all bytes from PlayerPrefs, using a key.
/// </summary>
/// <param name="key">The key in PlayerPrefs where the data is stored.</param>
/// <returns>Array containing all bytes that could be retrieved.</returns>
public static byte[] ReadAllBytes(string key)
{
return System.Convert.FromBase64String(UnityEngine.PlayerPrefs.GetString(key));
}
/// <summary>
/// Writes all bytes to PlayerPrefs, using a key.
/// </summary>
/// <param name="key">The key in PlayerPrefs where the data is stored.</param>
/// <param name="bytes">Array containing all bytes that should be written.</param>
public static void WriteAllBytes(string key, byte[] bytes)
{
UnityEngine.PlayerPrefs.SetString(key, System.Convert.ToBase64String(bytes));
}
#region System.IO.Stream implementation
/// <summary>
/// See System.IO.MemoryStream.CanRead for more information.
/// </summary>
public override bool CanRead
{
get { return stream.CanRead; }
}
/// <summary>
/// See System.IO.MemoryStream.CanSeek for more information.
/// </summary>
public override bool CanSeek
{
get { return stream.CanSeek; }
}
/// <summary>
/// See System.IO.MemoryStream.CanWrite for more information.
/// </summary>
public override bool CanWrite
{
get { return stream.CanWrite; }
}
/// <summary>
/// Writes the data to PlayerPrefs.
/// </summary>
public override void Flush()
{
stream.Flush();
Save();
}
/// <summary>
/// See System.IO.MemoryStream.Length for more information.
/// </summary>
public override long Length
{
get { return stream.Length; }
}
/// <summary>
/// See System.IO.MemoryStream.Position for more information.
/// </summary>
public override long Position
{
get
{
return stream.Position;
}
set
{
stream.Position = value;
}
}
/// <summary>
/// See System.IO.MemoryStream.Read for more information.
/// </summary>
public override int Read(byte[] buffer, int offset, int count)
{
return stream.Read(buffer, offset, count);
}
/// <summary>
/// See System.IO.MemoryStream.Seek for more information.
/// </summary>
public override long Seek(long offset, SeekOrigin origin)
{
return stream.Seek(offset, origin);
}
/// <summary>
/// See System.IO.MemoryStream.SetLength for more information.
/// </summary>
public override void SetLength(long value)
{
stream.SetLength(value);
}
/// <summary>
/// See System.IO.MemoryStream.Write for more information.
/// </summary>
public override void Write(byte[] buffer, int offset, int count)
{
stream.Write(buffer, offset, count);
}
#endregion
/// <summary>
/// Loads data from key.
/// </summary>
private void Load()
{
stream = new System.IO.MemoryStream();
if (UnityEngine.PlayerPrefs.HasKey(key))
{
byte[] bytes = ReadAllBytes(key);
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
}
}
/// <summary>
/// Saves data to key.
/// </summary>
private void Save()
{
byte[] bytes = stream.GetBuffer();
WriteAllBytes(key, bytes);
}
/// <summary>
/// Location in PlayerPrefs where data goes.
/// </summary>
private string key;
/// <summary>
/// Temporary stream to write data to.
/// </summary>
private MemoryStream stream;
}