题外话
一个项目需要用到u3d做个三维的展示,数据库是sqlserver,刚开始是直接写在u3d里面,等做好的时候,发布web player,开始报错,找了许久最后得知是不可以在u3d下直接对sql操作。然后开始找另外方法,
利用json互通数据。Unity3d的http通信很简单,也非常好用。
.net下新建
GetList.ashx一般处理程序
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
JsonData data = new JsonData();//利用LitJson.dll 格式化json
data["name"] = "十七";
data["age"] = "23";
string json = JsonMapper.ToJson(data);
}
U3d下
public UILabel myLable;
public UILabel myLable1;
void Start()
{
StartCoroutine(GetList());
}
IEnumerator GetList()
{
WWWForm form = new WWWForm();
WWW w = new WWW("../Json/GetList.ashx", form);
yield return w;
if (w.error != null) { Debug.LogError(w.error); }
// Debug.Log(w.text);
myLable.text = w.text;
JsonData jd = JsonMapper.ToObject(w.text);//利用LitJson 解析发来的json
myLable1.text ="name="+jd["name"] + " age=" + jd["age"];
}
在U3d下发布webPlayer 放到.aspx页面里,运行如下
以上是C#向u3d发送数据。而u3d里向C#下传送数据分get和post两种。
C#下(post方式)
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string name = context.Request.Form["name"];
string age = context.Request.Form["age"];
JsonData data = new JsonData();//利用LitJson.dll 格式化json
data["name"] = "十七";
data["age"] = "23";
string json = JsonMapper.ToJson(data);
context.Response.Write(json);
FileStream fs = new FileStream("d:\\log.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(name + age);
sw.Close();//可以知道 已经获取到由U3d传来的值
}
u3d下
public UILabel myLable;
public UILabel myLable1;
void Start()
{
StartCoroutine(GetList());
}
IEnumerator GetList()
{
WWWForm form = new WWWForm();
form.AddField("name", "十七");
form.AddField("age", "23");//添加两个表单值
WWW w = new WWW("../Json/GetList.ashx", form);
yield return w;
if (w.error != null) { Debug.LogError(w.error); }
// Debug.Log(w.text);
myLable.text = w.text;
JsonData jd = JsonMapper.ToObject(w.text);//利用LitJson 解析发来的json
myLable1.text ="name="+jd["name"] + " age=" + jd["age"];
}
发布webplayer 嵌入web页面中 运行页面 打开d盘
Get方式只需要在u3d下把 WWW w = new WWW("../Json/GetList.ashx", form); 修改为WWW w = new WWW("../Json/GetList.ashx?mark=mark");
C#下修改为 string mark = context.Request.QueryString["mark"];
当然get方式和post方式可以混合使用,只不过是在接受数据的时候post方式使用form收取,而get使用QueryString接收。
u3d与C#列表数据传输
C#下
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string mark = context.Request.QueryString["mark"];
string id = context.Request.Form["id"];
JsonData data = new JsonData();
listCeDianDate listcediandata = new listCeDianDate();
DataTable dt = SqlHelp.GetCeDianDate();
List<CeDianDateClass> listCDD = new List<CeDianDateClass>();
for (int i = 0; i < dt.Rows.Count; i++)
{
CeDianDateClass CeDianDate = new CeDianDateClass();
CeDianDate.ID = dt.Rows[i]["ID"].ToString();
CeDianDate.ZuoBiaoX = dt.Rows[i]["ZuoBiaoX"].ToString();
CeDianDate.ZuoBiaoY = dt.Rows[i]["ZuoBiaoY"].ToString();
CeDianDate.ZuoBiaoZ = dt.Rows[i]["ZuoBiaoZ"].ToString();
CeDianDate.CeDianType = dt.Rows[i]["CeDianType"].ToString();
CeDianDate.CeDianMark = dt.Rows[i]["CeDianMark"].ToString();
listCDD.Add(CeDianDate);
}
listcediandata.cediandata_list = listCDD;
string json = JsonMapper.ToJson(listcediandata);
context.Response.Write(json);
}
类定义
public class CeDianDateClass
{
public string ID;
public string ZuoBiaoX;
public string ZuoBiaoY;
public string ZuoBiaoZ;
public string CeDianType;
public string CeDianMark;
}
public class listCeDianDate
{
public List<CeDianDateClass> cediandata_list;
}
U3d下
public UILabel myLable;
public UILabel myLable1;
void Start()
{
StartCoroutine(GetList());
}
IEnumerator GetList()
{
WWWForm form = new WWWForm();
form.AddField("id", "123");
WWW w = new WWW("../Json/GetList.ashx?mark=nimade", form);
yield return w;
if (w.error != null) { Debug.LogError(w.error); }
listCeDianDate list = JsonMapper.ToObject<listCeDianDate>(w.text);
for (int i = 0; i < list.cediandata_list.Count; i++)
{
myLable1.text += list.cediandata_list[i].ID+",";
}
}
u3d下类定义
public class CeDianDateClass
{
public string ID;
public string ZuoBiaoX;
public string ZuoBiaoY;
public string ZuoBiaoZ;
public string CeDianType;
public string CeDianMark;
}
public class listCeDianDate
{
public List<CeDianDateClass> cediandata_list;
}
发布运行如下
json如下:
LitJson这个dll 官网上可以下载。