加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2dX通过Java服务器向Unity传输数据四

发布时间:2020-12-14 16:57:58 所属栏目:百科 来源:网络整理
导读:Unity3D相关代码: using UnityEngine;using System;using System.Net;using System.Net.Sockets;using System.Collections;using System.Text;public class UnitySocket {public Socket mSocket=null; public UnitySocket() { } public void Connect(string

Unity3D相关代码:

using UnityEngine;
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Text;

public class UnitySocket  {

	public  Socket mSocket=null;

    public UnitySocket()
    {
        
    }

    public  void Connect(string IP,int localPort)
    {
        mSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);

        try
        {
            IPAddress ip = IPAddress.Parse(IP);
            IPEndPoint ipe = new IPEndPoint(ip,localPort);

            //---------------------m1-----------------------
            //mSocket.Connect(ipe);

            //---------------------m2------------------------
            mSocket.BeginConnect(ipe,new AsyncCallback(Connected),null);

        }
        catch(Exception e)
        {
            e.GetType();
        }
    }

    void Connected(IAsyncResult iar)
    {
        mSocket.EndConnect(iar);

    }

    bool ReceiveFlag = true;
    public  byte[] readData = new byte[1024];

   public void startReceive()
    {
        if (ReceiveFlag)
        {
            ReceiveFlag = false;
            mSocket.BeginReceive(readData,readData.Length,SocketFlags.None,new AsyncCallback(endReceive),mSocket);
        }
    }

   public void endReceive(IAsyncResult iar)
   {
       ReceiveFlag = true;
       Socket remote = (Socket)iar.AsyncState;
       int recv = remote.EndReceive(iar);
       if (recv > 0)
       {

       }
   }

   public void SendMessage(int rotation)
   {
       byte[] msg = BitConverter.GetBytes(rotation);
       mSocket.BeginSend(msg,msg.Length,new AsyncCallback(SendData),mSocket);
   }

   public void SendMessage(string data)
   {
       byte[] msg = Encoding.UTF8.GetBytes(data);
       mSocket.BeginSend(msg,mSocket);
   }

   void SendData(IAsyncResult iar)
   {
       Socket remote = (Socket)iar.AsyncState;
       int sent = remote.EndSend(iar);
   }

    public  void Send(int data)
    {
        byte[] longth = BitConverter.GetBytes(data);
        mSocket.Send(longth);
    }

    public  void Send(float data)
    {
        byte[] longth = BitConverter.GetBytes(data);
        mSocket.Send(longth);
    }

    public  void Send(string data)
    {
        byte[] longth = Encoding.UTF8.GetBytes(data);
        mSocket.Send(longth);
    }

    public  int ReceiveInt()
    {
        byte[] recvBytes = new byte[4];
        mSocket.Receive(recvBytes,4,0);
        int data=BitConverter.ToInt32(recvBytes,0);
        return data;
    }

    public double ReceiveDouble()
    {
        byte[] recvBytes=new byte[8];
        mSocket.Receive(recvBytes,0);
        double data = BitConverter.ToDouble(recvBytes,0);
        return data;
    }

    public string ReceiveString(int length)
    {
        byte[] recvBytes = new byte[length];
        mSocket.Receive(recvBytes,length,0);
        string data= Encoding.UTF8.GetString(recvBytes);
        return data;
    }



}

using UnityEngine;
using System.Collections;
using UnityNetwork;
using System;


public class BoxManager : NetworkManager
{
    NetTCPClient client = null;
    UnitySocket socket = null;
    int ra = 0;
    int flag = 0;
    public void Start()
    {
        //client = new NetTCPClient();
        //client.Connect("172.27.35.1",59422);
        socket = new UnitySocket();
        socket.Connect("172.27.35.1",59422);
        //BoxMove.Instance.yAngle = socket.ReceiveInt();
        //socket.Send(3);
        //socket.Send("sss");
        //int a = socket.ReceiveInt();
        //Debug.Log(a);
        //int b = socket.ReceiveInt();
        //int c = socket.ReceiveInt();
        
        //Debug.Log(b);
        //Debug.Log(c);
    }

    public void Send(NetBitStream stream)
    {
        client.Send(stream);
    }

    public override void Update()
    {
        base.Update();
        socket.SendMessage(5);
        socket.SendMessage("UNITY");
        socket.startReceive();
        ra = BitConverter.ToInt32(socket.readData,0);
        //if (socket.ReceiveString(8).StartsWith("Rotation")) ra = socket.ReceiveInt();
        BoxMove.Instance.yAngle = ra;

        //flag += 1;
        //if (flag > 90)
        //{
        //    flag = 0;
        //}
     
        Debug.Log(ra);

    }
}

using UnityEngine;
using System.Collections;
using UnityNetwork;

public class BoxMove : MonoBehaviour {

    public static BoxMove Instance = null;

    BoxManager _boxMgr;
    Vector3 s = new Vector3(1,0);
    public float yAngle=0.0f;
	// Use this for initialization
	void Start () {
        DontDestroyOnLoad(this);
        Instance = this;
        _boxMgr = new BoxManager();
        _boxMgr.Start();
	}
	
	// Update is called once per frame
	void Update () {
        _boxMgr.Update();
        //yAngle += 1.0f;
        transform.localRotation = Quaternion.AngleAxis(yAngle,s);
	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读