服务器 winform unity debug 解决方案

服务器c# 自己写,前端用u3d,调试的时候服务器不够直观。于是想到做个小的unity端做debug用。

1,安装unityweb
2,编写和发布一个web端
里面写上接收代码,接收的方式和和sendmessage 类似
但是参数类型有限制,建议参数使用string。
3,服务器选上unity web com组件
4,使用unityweb 控件
填上资源地址 src

5 发送要显示的信息
参考资料:
C# WinForm程序中使用Unity3D控件 
http://neil.wyh.blog.163.com/blog/static/3161151201361480843/
当前线程不在单线程单元中,因此无法实例化 ActiveX 控件解决办法
http://blog.csdn.net/xuehuic/article/details/6426284
C#中char[]与string之间的转换
http://www.cnblogs.com/wintalen/archive/2010/12/20/1911599.html

类序列化到string

public static class Util
    {
        public static string SerializeToString(object data)
        {
            var bts = Serialize(data);
            string str = CharsToString(BytesToChars(bts));
            str = @str.Replace("\0", "|% (");
            return str;
        }
        public static object DescrializeFromString(string str)
        {
            str = @str.Replace("|% (","\0");
            var cs = StringToChars(str);
            var bts = CharsToBytes(cs);
            var obj = Deserialize(bts);
            return obj;
        }
        private static char[] BytesToChars(byte[] bts)
        {
            char[] tmp = new char[bts.Length];
            for (int i = 0; i < bts.Length; i++)
            {
                tmp[i] = (char)bts[i];
            }
            return tmp;
        }
        private static byte[] CharsToBytes(char[] cs)
        {
            byte[] bts = new byte[cs.Length];
            for (int i = 0; i < bts.Length; i++)
            {
                bts[i] = (byte)cs[i];
            }
            return bts;
        }
        private static char[] StringToChars(string str)
        {
            return str.ToCharArray();
        }
        private static string CharsToString(char[] cs)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (var item in cs)
            {
                sb.Append(item);
            }
            string s = sb.ToString();
            return s;
        }
        private static byte[] Serialize(object data)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream rems = new System.IO.MemoryStream();
            formatter.Serialize(rems, data);
            return rems.GetBuffer();
        }
        private static object Deserialize(byte[] data)
        {
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
            System.IO.MemoryStream rems = new System.IO.MemoryStream(data);
            data = null;
            return formatter.Deserialize(rems);
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值