- Home /
WWWForm Question
This is the first time I've had to use a MySQL database to store outside information in, so if you have ever used the WWWForm class in unity I would really like to hear your advice. The code I'm writing is responsible for allowing a user to create a guild. The snippets that are responsible for storing the information on another server are located below. I'm interested particularly with what will happen with the Texture2D Emblem and the List variables. Thanks for your future feedback! (This is only the unity side code, no PHP)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GuildManager : MonoBehaviour {
public string guildName;
public string guildDescription;
public List<string> membersInGuild = new List<string> {""};
public Color emblemColor;
public Texture2D guildEmblem;
public List<string> guildInventoryItems = new List<string> {""};
public List<string> guildInventoryItemStats = new List<string> {""};
void OnGUI() {
if(GUI.Button(new Rect(200,350,100,30), "Complete")) {
if(guildName == "" || guildDescription == "" || guildEmblem == "") {
_errorMessage += "Please enter all the required fields \n";
}
else {
WWWForm form = new WWWForm();
form.AddField("Guild Name", guildName);
form.AddField("Guild Description", guildDescription);
form.AddField("Guild Emblem", guildEmblem.ToString());
form.AddField("Guild Emblem Color", emblemColor.ToString());
form.AddField("Guild Funds", 0);
form.AddField("Guild Members", membersInGuild.ToString());
form.AddField("Guild Inventory", guildInventoryItems.ToString());
form.AddField("Guild Inventory Stats", guildInventoryItemStats.ToString());
WWW w = new WWW("", form);
StartCoroutine(createGuild(w));
}
}
}
IEnumerator createGuild(WWW w) {
yield return w;
if(w.error == null) {
_errorMessage += w.text;
}
else {
_errorMessage += "ERROR: " + w.error + "\n";
}
}
}
Answer by Bunny83 · Jan 04, 2014 at 02:27 PM
Well, ToString is a function every objects has. For simple datatypes it usually converts the value into a string representation. More complex datatypes like classes / structs can't be "converted" in a general way into a string. That's why ToString usually just returns the classname of the class.
A class can override ToString to define how it should be converted to a string. Vector3 for example rounds it's components x,y and z to one decimal digit and concats the decimal-string-representation of the 3 values, seperates them with a comma and surrounds the whold string with brackets. This string representation is just for displaying the vector in a Debug.Log. It's shouldn't be used to recreate the Vector3 from the string since it's lacking information.
Texture2D is a more complex datatype and doesn't implement a string representation from which it could be recreated. ToString will just return the class name. The same is true for the List class.
Converting a complex object into another (sequencial) representation is called Serialization. There are many ways to serialize your data. There are ways so the resulting data is human readable, like JSON or XML or simply a binary serialization (usually most compact). As i said there's no general way to serialize a class. C# has a serialization concept but it won't work with Unity since you can't create most things with the "new" operator in Unity. So you have to implement your own way how to convert your data. In which way you "should" store the data in your database depends on your database design and layout. During the design process you have to decide what information should be "searchable" and what's the relation between different datasets. Also normalization should be applied in a way you have a balance between performance and "normality". There's also no general rule since it depends on the way you use the database.
A for a guild - member relationship you usually use a seperate relation table.
// pseudo SQL
create table guilds (id int primary key auto_increment , name text, ....);
create table users (id int primary key auto_increment , name text, ....);
create table guild_members (guild int, user int);
or a "guild" field in the user record. However since it's usually possible that a user doesn't belong to a guild a relation table is used.
Answer by HappyMoo · Jan 04, 2014 at 02:17 PM
Hi,
AddField needs strings, so if you put any other object in, the compiler will call obj.ToString() instead.
You can't easily send a Texture to the server. If that texture is selected from a static set of textures, just assign every texture a number and send that number instead.
If that's really a procedurally generated Texture and you need it on the server, you can do something like this:
form.AddBinaryData("emb", guildEmblem.EncodeToPNG());
For the other fields, just do something like this to see if the format you're sending is ok and if not, format the data, so your server can understand it:
Debug.Log(emblemColor.ToString());
I would also recommend to save on the data sent and shorten the field names. At least take the spaces out - I don't even know if that works.
Your answer
Follow this Question
Related Questions
What Method is better for Creating a Guild 1 Answer
WWW Form, MySQL, Azure Question 1 Answer
Access a MySQL database via C# ? 2 Answers
MySql Online Character About 0 Answers