hi
I am beginer with unity 3d.please tell me how to call web services in unity 3d.
and guidance related to API calls.
hi
I am beginer with unity 3d.please tell me how to call web services in unity 3d.
and guidance related to API calls.
Hi and welcome,
I would recommend using the WWW class with wwwforms in C#. It works really well and is fast to use, you just need to have to web servies already up to call them.
I’ve done for doing this below. Let’s go : ==>
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class btnGetData : MonoBehaviour {
void Start()
{
gameObject.GetComponent<Button>().onClick.AddListener(TaskOnClick);
}
IEnumerator WaitForWWW(WWW www)
{
yield return www;
string txt = "";
if (string.IsNullOrEmpty(www.error))
txt = www.text; //text of success
else
txt = www.error; //error
GameObject.Find("Txtdemo").GetComponent<Text>().text = "++++++
" + txt;
}
void TaskOnClick()
{
try
{
GameObject.Find(“Txtdemo”).GetComponent().text = “starting…”;
string ourPostData = “{"plan":"TESTA02"”;
Dictionary<string,string> headers = new Dictionary<string, string>();
headers.Add(“Content-Type”, “application/json”);
//byte b = System.Text.Encoding.UTF8.GetBytes();
byte pData = System.Text.Encoding.ASCII.GetBytes(ourPostData.ToCharArray());
///POST by IIS hosting…
WWW api = new WWW(“http://192.168.1.120/si_aoi/api/total”, pData, headers);
///GET by IIS hosting…
///WWW api = new WWW(“http://192.168.1.120/si_aoi/api/total?dynamix={"plan":"TESTA02"”);
StartCoroutine(WaitForWWW(api));
}
catch (UnityException ex) { Debug.Log(ex.Message); }
}
}