效果展示(尚未完善) using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Threading; using System.Net; using System.Net.S ...
效果展示(尚未完善)
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Threading; using System.Net; using System.Net.Sockets; using System.Text; using Common; using UIWidgetsSamples; using System; /// <summary> /// 服務端 /// </summary> public class ChatUDPServerTest : MonoBehaviour { public string serverIP; //IP地址 public int serverPort; //埠 //1.創建Scoket對象 IP Port private Thread thread; private UdpClient udpSeivic; public void Start() { chatView = transform.FindChildByName("ChatView"). GetComponent<ChatView>(); //給埠和IP //構建終結點 IP和一個埠 IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(serverIP), serverPort); udpSeivic = new UdpClient(localEP); thread = new Thread(ReceiveMessage); thread.Start(); } /// <summary> /// 接收消息 /// </summary> private void ReceiveMessage() { while (true) { IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0); //創建任意終結點 //ref byte[] date = udpSeivic.Receive(ref remote); //Receive接收消息 如果沒有收到消息 線程阻塞 放線上程中 string msg = Encoding.UTF8.GetString(date); //獲取的客戶都安信息 Debug.Log(remote.Address + "===" + remote.Port); //如果接收客戶端的消息,會把任意終結點修改為客戶端的終結點 ThreadCrossHelper.Instance.ExecuteOnMainThread(() => { ShowMessage(msg); }); } } private ChatView chatView; /// <summary> /// 顯示消息 /// </summary> /// <param name="msg"></param> public void ShowMessage(string msg) { chatView.DataSource.Add(new ChatLine() { UserName = "AnnnS", Message = msg, Time = DateTime.Now, Type = ChatLineType.User, }); } private void OnApplicationQuit() { udpSeivic.Close(); thread.Abort(); } }ChatUDPServerTest
腳本引用的工具箱
- MonoSingleton (泛型單例)
- ThreadCrossHelper (為子線程提供,可以在主線程中執行的方法)
- TransformHelper(根據名稱查找後代元素)
using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Common { /// <summary> /// /// </summary> public class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T> { //public static T Instance //{ // get; // private set; //} //private void Awake() //{ // Instance = this as T; //} //按需載入 private static T instance; public static T Instance { get { if (instance == null) { //在場景中查找對象 instance = FindObjectOfType<T>(); if (instance == null) { //創建游戲對象 附加 腳本對象 new GameObject("Singleton of " + typeof(T)).AddComponent<T>();//立即執行Awake } else { instance.Initialized(); } } return instance; } } protected virtual void Initialized() { } [Tooltip("是否需要跨場景不銷毀")] public bool isDontDestroy = true; //如果管理類自行附加到物體中 //在Awake中為instance賦值 protected void Awake() { if (isDontDestroy) { DontDestroyOnLoad(gameObject); } if (instance == null) { instance = this as T; instance.Initialized(); } } } }MonoSingleton
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Common { /// <summary> /// /// </summary> public class ThreadCrossHelper : MonoSingleton<ThreadCrossHelper> { /// <summary> /// 延遲項 /// </summary> class DelayedItem { public Action CurrentAction { get; set; } public DateTime Time { get; set; } } private List<DelayedItem> actionList; //private List<Action> actionList; //private List<float> timeList; protected override void Initialized() { base.Initialized(); actionList = new List<DelayedItem>(); } private void Update() { for (int i = actionList.Count - 1; i >= 0; i--) { //到時間 if (actionList[i].Time <= DateTime.Now) { lock (actionList) { actionList[i].CurrentAction();//執行 actionList.RemoveAt(i);//從列表中移除 } } } } /// <summary> /// 為子線程提供,可以在主線程中執行的方法 /// </summary> /// <param name="action"></param> /// <param name="dealy"></param> public void ExecuteOnMainThread(Action action, float dealy = 0) { DelayedItem item = new DelayedItem() { CurrentAction = action, //Time = Time.time + dealy Time = DateTime.Now.AddSeconds(dealy) }; lock (actionList) { actionList.Add(item); } } } }ThreadCrossHelper
using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Common { /// <summary> /// 變換組件助手類 /// </summary> public static class TransformHelper { /// <summary> /// 未知層級,根據名稱查找後代元素 /// </summary> /// <param name="currentTF"></param> /// <param name="childName"></param> /// <returns></returns> public static Transform FindChildByName(this Transform currentTF, string childName) { Transform childTF = currentTF.Find(childName); if (childTF != null) return childTF; //將問題推遲給子物體 for (int i = 0; i < currentTF.childCount; i++) { //在方法體內部,又遇到了相同的問題,所以需要調用自身。 childTF = FindChildByName(currentTF.GetChild(i), childName); if (childTF != null) return childTF; } return null; } } }TransformHelper