赞
踩
在雷达互动系统中,由于硬件问题,程序容易误触,为了解决这个问题,通过UDP传送协议控制程序部分功能。例如:中控(平板)发送1,在程序中视频1播放,中控(平板)发送2,程序中视频2播放。
链接:https://pan.baidu.com/s/1Vvs6HfmFrPEUn1gKVULymg?pwd=jl98
提取码:jl98
上方框中
第一个先选UDP
第二个框中输入本机主机地址,就是电脑的ip地址(按win+R调出一个窗口,输入CMD,然后在弹出的框中输入ipconfig,出现的内容中,ipv4地址就是本机的ip地址)
第三个框中就是端口号,端口号是4位,可以输入未被占用的任意四位,例如:8800,8080。
设置完成后,通过点击开启按钮就可以打开端口(图片是打开后的状态,只有打开后才会显示下方框中的内容)
下方框中
就是需要控制的主机的ip和端口号,由于是一台电脑,所以主机的ip是一样的,“:”后面是端口号,端口号是在程序中显示的未被占用的端口号,本案例中是1180。
通过发送不同的数据,在程序中把需要接收的数据写进去,然后再写相对应的方法即可。
由于我们是接收方,所以上方框中的内容不需要知道,开启后,将电脑的ip号和端口号传给发送方即可。
ServerControl
- using System.Collections.Generic;
- using UnityEngine.UI;
- using UnityEngine;
- using UnityEngine.Video;
-
- /// <summary>
- /// 服务接收生成
- /// </summary>
- public class ServerControl : MonoBehaviour
- {
- //public Transform prefab;
- public InputField field;
- public Button btn;
- public GameObject part1Root;
- public GameObject part2Root;
- void Start()
- {
- if (field.text != string.Empty)
- {
- UdpManager.InitializeUdpClient(int.Parse(field.text));
- }
- part1Root.SetActive(true);
- part2Root.SetActive(false);
- }
- void Update()
- {
- #region 1.0
- //if (UdpManager.m_receivedMessage != null)
- //{
- // string[] msgArr = UdpManager.m_receivedMessage.Split('_');
- // Debug.Log(msgArr[0]);
- // if (msgArr.Length > 0)
- // {
- // //创建预设体
- // Transform obj = Instantiate(PrefabList[int.Parse(msgArr[0])], parent);
- // obj.gameObject.SetActive(true);
- // Debug.Log("创建");
- // }
- // UdpManager.m_receivedMessage = null;
- //}
- #endregion
-
- #region 2.0
- if (UdpManager.m_receivedMessage != null)
- {
- string[] array = UdpManager.m_receivedMessage.Split(',');
- Debug.Log(UdpManager.m_receivedMessage);
- if (UdpManager.m_receivedMessage=="1")
- {
- part1Root.SetActive(true);
- part2Root.SetActive(false);
- }
-
- if (UdpManager.m_receivedMessage == "2")
- {
- part1Root.SetActive(false);
- part2Root.SetActive(true);
- }
- UdpManager.m_receivedMessage = null;
- //if (UdpManager.m_receivedMessage != null)
- //{
- // //创建预设体
- // transform.eulerAngles = new Vector3(float.Parse(array[0]), float.Parse(array[1]), float.Parse(array[2]));
- //}
- //UdpManager.m_receivedMessage = null;
- }
- #endregion
-
-
-
- }
-
- private void OnDestroy()
- {
- UdpManager.Close();
- }
- }
其中端口号也可以设置外部加载,本系统中设置的是在程序中修改
UdpManager
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using UnityEngine;
- using UnityEngine.Video;
-
- public class UdpManager
- {
- public static string m_receivedMessage;
- static IPEndPoint m_IPEndPoint;
- static UdpClient m_udpClient;
-
-
- public static void InitializeUdpClient(int Prot)
- {
- m_IPEndPoint = new IPEndPoint(IPAddress.Any, Prot);
- m_udpClient = new UdpClient(m_IPEndPoint);
- UdpModel s = new UdpModel(m_udpClient, m_IPEndPoint);
- m_udpClient.BeginReceive(EndReceive, s);
- Debug.Log("服务器启动");
-
- }
-
- //结束得到的信息
- private static void EndReceive(IAsyncResult ar)
- {
- try
- {
- UdpModel m_UdpModel = ar.AsyncState as UdpModel;
- if (m_UdpModel != null)
- {
- UdpClient udpClient = m_UdpModel.m_udpclient;
- IPEndPoint ip = m_UdpModel.m_ip;
- Byte[] receiveBytes = udpClient.EndReceive(ar, ref ip);
- string msg = Encoding.UTF8.GetString(receiveBytes);
- m_receivedMessage = msg;
- udpClient.BeginReceive(EndReceive, m_UdpModel); //开始获取
-
- }
- }
- catch (Exception ex)
- {
- //处理异常
- }
- }
-
- //udp模型
- private class UdpModel
- {
- public UdpClient m_udpclient = null;
- public IPEndPoint m_ip;
- public UdpModel(UdpClient udpclient, IPEndPoint ip)
- {
- this.m_udpclient = udpclient;
- this.m_ip = ip;
- }
- }
-
- //关闭
- public static void Close()
- {
- if (m_udpClient != null)
- {
- m_udpClient.Close();
- m_udpClient = null;
- }
- }
-
- /// <summary>
- /// 发送数据
- /// </summary>
- /// <param name="obj"></param>
- public static void SendMessage(string message)
- {
- UdpClient myUdpClient = new UdpClient();
- IPEndPoint endpoint;
- //当前服务器ip和端口号
- myUdpClient = new UdpClient(new IPEndPoint(IPAddress.Any, 8800));
-
- //要发送给的地址和端口号,255.255.255.255表示在这个局域网的所有ip
- endpoint = new IPEndPoint(IPAddress.Parse("192.168.31.174"), 1180);
-
- byte[] bytes = Encoding.UTF8.GetBytes(message);
- try
- {
- myUdpClient.Send(bytes, bytes.Length, endpoint);
- myUdpClient.Close();
- }
- catch (Exception err)
- {
- Console.Write(err.Message, "发送失败");
- }
- finally
- {
- myUdpClient.Close();
- }
- }
- }
脚本挂载在摄像机上,开启程序,设置网络调试助手下方的ip和端口号,发送数据1时,播放视频1,发送数据2时,播放视频2。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。