求问unity3d怎样发送unity 广播消息和接收消息

匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。Unity3D(11)
Unity3D引擎交流QQ群:【】
之前发送消息或者调用消息只是SendMessage或者创建一个public的GameObject,然后在程序中调用这个对象的public方法。由于没有仔细看GameObject的方法,今天才发现如果想给父级对象发送消息,或者调用父级对象的方法,我们可以使用SendMessageUpwards(“ApplyDamage”, “libufan”);这样,只要父类的函数里面有ApplyDamage方法的就一定会有反应。
那么如果给自己的子类传消息呢?同样的是有一个简单的方法供我们使用BroadcastMessage(“ApplyDamage”, “libufan”);方法,如果自己的子对象里面有ApplyDamage方法的也会有反应。
之前的文章里面我写过一个递归函数同时设置对象以及子对象的活动状态,今天还发现了另外的一个简单的方法,原来使用SetActiveRecursively(true)这个方法就可以了,在很多Unity3D交流群里面大家也在说SetActive,如果还在为如何设置子对象的活动状态发愁的话,可以看看我的递归函数,或者聪明的直接使用Unity3D官方的函数吧!

参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:12909次
排名:千里之外
原创:12篇
(1)(3)(2)(1)(5)1600人阅读
【Unity】(92)
【Unity网络】(1)
【C#】(32)
多个客户端一同使用就是一个简单的公共聊天室。服务端为一个控制台程序使用C#实现,当然,在Unity3D中也相应地使用了C#语言实现客户端,服务端和客户端能实现消息的互通,当服务端接收到某客户端发送过来的消息时将会对客户端列表成员进行广播,这是公共聊天室的最基本的形式。Socket通讯是网络游戏最为基础的知识,因此这个实例能向有志投身于网游行业的初学者提供指导意义。
using System.Collections.G
using System.L
using System.T
using System.Net.S
namespace TestServer
class Program
// 设置连接端口
const int portNo = 500;
static void Main(string[] args)
// 初始化服务器IP
System.Net.IPAddress localAdd = System.Net.IPAddress.Parse(&127.0.0.1&);
// 创建TCP侦听器
TcpListener listener = new TcpListener(localAdd, portNo);
listener.Start();
// 显示服务器启动信息
Console.WriteLine(&Server is starting...\n&);
// 循环接受客户端的连接请求
while (true)
ChatClient user = new ChatClient(listener.AcceptTcpClient());
// 显示连接客户端的IP与端口
Console.WriteLine(user._clientIP + & is joined...\n&);
ChatClient.cs
using System.Collections.G
using System.L
using System.T
using System.C
using System.Net.S
namespace TestServer
class ChatClient
public static Hashtable ALLClients = new Hashtable(); // 客户列表
private TcpClient _
// 客户端实体
string _clientIP;
// 客户端IP
private string _clientN // 客户端昵称
private byte[]
// 消息数据
private bool ReceiveNick =
public ChatClient(TcpClient client)
this._client =
this._clientIP = client.Client.RemoteEndPoint.ToString();
// 把当前客户端实例添加到客户列表当中
ALLClients.Add(this._clientIP, this);
data = new byte[this._client.ReceiveBufferSize];
// 从服务端获取消息
client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
// 从客戶端获取消息
public void ReceiveMessage(IAsyncResult ar)
int bytesR
lock (this._client.GetStream())
bytesRead = this._client.GetStream().EndRead(ar);
if (bytesRead & 1)
ALLClients.Remove(this._clientIP);
Broadcast(this._clientNick + & has left the chat&);
string messageReceived = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
if (ReceiveNick)
this._clientNick = messageR
Broadcast(this._clientNick + & has joined the chat.&);
//this.sendMessage(&hello&);
ReceiveNick =
Broadcast(this._clientNick + &&& + messageReceived);
lock (this._client.GetStream())
this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
catch (Exception ex)
ALLClients.Remove(this._clientIP);
Broadcast(this._clientNick + & has left the chat.&);
// 向客戶端发送消息
public void sendMessage(string message)
System.Net.Sockets.NetworkS
lock (this._client.GetStream())
ns = this._client.GetStream();
// 对信息进行编码
byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(message);
ns.Write(bytesToSend, 0, bytesToSend.Length);
ns.Flush();
catch (Exception ex)
// 向客户端广播消息
public void Broadcast(string message)
Console.WriteLine(message);
foreach (DictionaryEntry c in ALLClients)
((ChatClient)(c.Value)).sendMessage(message + Environment.NewLine);
Unity3d客户端的代码ClientHandler.cs
using UnityE
using System.C
using System.Collections.G
using System.T
using System.Net.S
public class ClientHandler : MonoBehaviour
const int portNo = 500;
private TcpClient _
// Use this for initialization
void Start ()
// Update is called once per frame
void Update ()
public string nickName = &&;
public string message = &&;
public string sendMsg = &&;
void OnGUI()
nickName = GUI.TextField(new Rect(10, 10, 100, 20), nickName);
message = GUI.TextArea(new Rect(10, 40, 300, 200), message);
sendMsg = GUI.TextField(new Rect(10, 250, 210, 20), sendMsg);
if(GUI.Button(new Rect(120, 10, 80, 20), &Connect&))
//Debug.Log(&hello&);
this._client = new TcpClient();
this._client.Connect(&127.0.0.1&, portNo);
data = new byte[this._client.ReceiveBufferSize];
//SendMessage(txtNick.Text);
SendMessage(nickName);
this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
if(GUI.Button(new Rect(230, 250, 80, 20), &Send&))
SendMessage(sendMsg);
sendMsg = &&;
public void SendMessage(string message)
NetworkStream ns = this._client.GetStream();
byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
ns.Write(data, 0, data.Length);
ns.Flush();
catch (Exception ex)
//MessageBox.Show(ex.ToString());
public void ReceiveMessage(IAsyncResult ar)
int bytesR
bytesRead = this._client.GetStream().EndRead(ar);
if (bytesRead & 1)
Debug.Log(System.Text.Encoding.ASCII.GetString(data, 0, bytesRead));
message += System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
catch (Exception ex)
&版权声明:转载时请以超链接形式标明文章原始出处和作者信息
&本文来自:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:704098次
积分:15863
积分:15863
排名:第512名
原创:166篇
转载:12篇
评论:22条&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!

我要回帖

更多关于 unity发送消息函数 的文章

 

随机推荐