Hi all,
I need a help in retrieving and sending data from and to the server of the Web Game made using unity 3D. What i actually need is, when i click an object, the script attached to it have a hard coded ID for the object, which i have to send back to the server and then retrieve back the details from the server, Which is stored in there.
I am new to Unity Web development and so please Help me Out to solve this.
Hi,
One more thing i like to know is, How to call the web services using C# script in unity 3D.
If Any one hav any sample code pls direct it to me.
:o why no one is responding to my request ?? wat happened to all??
I guess nobody answers because its not that hard to do actually. Take a look at the WWW class, its all you need! See Unity - Scripting API: WWW for the information needed.
thanks for the replay but in search ther in the script reference i dont find anything relalted to Web services and data retrieval and so… thats y i posted a topic.
Atleast for basic coding and scripting i need some help , ther after i am confident i will make it up…
There’s an example on the Unify wiki explaining how to do server side highscores. This is not exactly what you need, but most of the principles are the same. Do you need to write the server code as well as the Unity code? If so, perhaps you can say which server platform you are using and exactly what you need the server to do.
Web Services are not directly Supported.
You will need to do all the serialization and deserialization yourself.
I do something very similar, but I ditch the soap wrapper and just return xml serialized objects. Since both my client and server are C#, I copy and paste the Transient objects so they are the same structure. I have an httpHandler on the server that does all the hard work for me.
I had to write the client side service methods as well, so I created a project item template to create these when I add a new service method. Still have to copy and paste it, so it isn’t entirely clean, but it does its job, and I really don’t have to make all that many web calls soooo.
I played around with WCF’s different protocols, but in then end felt there was too much meta crap being passed so stuck with my manual versiom.
If you had pro, I am pretty sure you could create the client proxies automatically using the XSD tool or visual studio Web References and reference them in unity from a dll.
Yes i agree to you.
So made a .Dll file of mu own and written all the functions required to call the web links and all there in the .DLL file.
Now the 2nd problem:: there is some access problem in using the System.Web.Services and its supported class, Not all the classes but some of them are having the problem.
Now pls tell me How this could be solved. Right now i am using Unity 3D pro 30 days trial, does this has anything to do with the following error.
No, it has nothing to do with Unity / Unity Pro but with the fact that System.Web is not available on the webplayer (see the error message you get)
So please me tell me how i could get the web Link or web service that is written by our own. Or is ther any way to get the detail of an Object, whoes TagID i am sending to the server from the client side. the details being stored on a remote machine.
Thanks in Advance…
pointing to Lukas posting from Feb 2nd
The WWW class is all you need. (its basically a HTTP request, either GET or POST)
Using the HTTP POST and GET request method Of WWW class, How could we get the return value of a particular link. For example if the particular function returns a string, how is it possible to get the string to Variable?? Is it simply
WWW m_www = new WWW(URL);
string temp_str = yield return m_www;
if not please let me know how to correct it and work it out.
Thanks In Advance
After the download has completed, you can get the data using the WWW class’s data field.
I have a similar problem, could retrieve xml data from web service, but how i can use serialization/deserialization with this data?
I’m tried this:
XmlTextReader textReader = new XmlTextReader(new StringReader(mywww.data));
XmlSerializer serializer = new XmlSerializer(typeof(MyObject[]));
temp = (MyObject[]) serializer.Deserialize(textReader);
But an exception in last line is returned.
An exemple, please.
Thanks for help, sorry for “english”.
I solved my problem, forgot some details.
In .Net:
MyObject class:
[Serializable] // Add this line
public class MyObject
{
private int myId;
public int MyId
{ get { return myId; } set { myId = value; } }
private string myName;
public string MyName
{ get { return myName; } set { myName= value; } }
// This constructor is needed on Serializable classes
public MyObject()
{ }
public MyObject(int id, string name)
{
myId = id;
myName= name;
}
}
Webservice class:
[WebService(Namespace = "http://tempuri.org/")] // Add this line (I had forgotten that)
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WS1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public List<MyObject> GeMyObjectList(string s)
{
List<MyObject> list = new List<MyObject>();
for(int i = 0; i <= 3; i++)
list.Add(new MyObject(i, "name" + i));
return list;
}
}
And Unity mono:
Request:
void InvokeWS()
{
string urlWS = "http://localhost:61445/WS1.asmx/GetMyObjectList";
WWWForm form = new WWWForm();
form.AddField("s",string.Empty);
WWW request = new WWW(urlWS, form); // yes, I need send some value for POST
StartCoroutine(SendMyRequest(request));
}
IEnumerator RequestAdd(WWW myWWW)
{
yield return myWWW;
// check for errors
if (myWWW.error == null)
{
XmlTextReader textReader = new XmlTextReader(new StringReader(mywww.data));
XmlSerializer serializer = new XmlSerializer(typeof(List<MyObject>), "http://tempuri.org/"); // remember, the string "http://tempuri.org" is in your webservice class too (I had forgotten that)
temp = (List<MyObject>) serializer.Deserialize(textReader);
// temp have MyObject list from Webservice now
}
else
{
Debug.Log("WWW Error: "+ mywww.error);
}
}
I left some comments in code, to try help. :shock:
I hope someone help with this. ![]()
One more time, sorry for my “english” ![]()