.net与u3d通信

题外话

          一个项目需要用到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 官网上可以下载。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值