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