- Home /
Save and share from Web Player
Hi I wonder what is the easiest and best way to save changes in web player and share with others. For example if I have a cube and I change the color of the cube. Playerprefs can save changes on my local machine, but I want to send the changes to others trough email or the automatic update on their machines when they do refresh page or similar.
Many car configurator generates a unique code that you can send to others who can open your configurator using your code and read up on the web.
Thanks in advance
Answer by Statement · Dec 24, 2012 at 12:35 AM
There are several ways you can do it. Either you can send the saved preferences as a Base 64 encoded string (suitable when you don't have too many bytes of info). Otherwise you could set up a server that stores preferences in a database and provides keys to look them up later.
Base64 encoding is just taking any binary blob of data and transforming it into a string containing safe and normal characters.
Example to encode/decode a color:
using UnityEngine;
using System.Collections;
public class ColorBase64 : MonoBehaviour
{
public Color color = Color.cyan;
void Start () {
Debug.Log ("Color to store as Base64: " + color);
string base64 = ColorToBase64 (color);
Debug.Log ("Color stored as Base64 string: " + base64);
Color color2 = Base64ToColor (base64);
Debug.Log ("Color loaded from Base64: " + color2);
}
string ColorToBase64 (Color color) {
return System.Convert.ToBase64String (ToBytes (color));
}
Color Base64ToColor (string base64) {
return ToColor (System.Convert.FromBase64String (base64));
}
static byte[] ToBytes (Color color) {
byte[] colorAsBytes = new byte[4];
colorAsBytes [0] = (byte)(color.a * 255);
colorAsBytes [1] = (byte)(color.r * 255);
colorAsBytes [2] = (byte)(color.g * 255);
colorAsBytes [3] = (byte)(color.b * 255);
return colorAsBytes;
}
static Color ToColor (byte[] bytes) {
float a = bytes [0];
float r = bytes [1];
float g = bytes [2];
float b = bytes [3];
return new Color (r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);
}
}
Example output:
Color to store as Base64: RGBA(0.443, 0.761, 0.548, 1.000)
Color stored as Base64 string: /3DCiw==
Color loaded from Base64: RGBA(0.439, 0.761, 0.545, 1.000)
If you want to talk with a web server to send and retrieve info:
Hope this helps you find a solution that works for you.
(And naturally there are many other ways to do it, but I think these are probably some of the easiest ways to go about)
Ok so it needs to communicate with the server and database via WWWForm and ex. PHP. Thank you for your answers
Yeah, well, not if you do the base64 approach. You'd generate a piece of text that the user can copy/paste.
Do you have maybe an example of using base64 approach in action, and what is the limit of bytes?
Ok I understand now how this works. So the next user can simply paste / 3DCiw == and get the same color as me by decoding it back to color. Thanks
Your answer
Follow this Question
Related Questions
Saving a text file to a server and accessing it again? 0 Answers
Save/Load game using XML on Browser/Web Player 2 Answers
Is it possible to add a specific prefab and custom user text in in a multiuser web player? 0 Answers
How to save data with the webplayer avaiable to all users? 1 Answer
Problems with saving/loading score with PlayerPrefs [C#] 1 Answer