1.
isMine : Network View 是否是由本机创建的。
2.
observed:指定被观察及同步的组件或者脚本。
3.
stateSynchronization:Network View的类型。OFF为没有数据将被同步,ReliableDeltaCompressed
4.
viewID:NetworkViewID类型的ID。
1.
RPC(Remote Procedure Call,远程过程调用)。在Network系统中可以理解为向其他机器发送消息,如果
2.
networkView.RPC(string funName,RPCMode mode,params object[] args);参数funName为函数
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Button = UnityEngine.UI.Button;
public class MyChat : MonoBehaviour
{
private Text peopleCount;
private Text peopleNames;
private Text chatText;
private InputField inputField;
private Button senon;
private InputField myNameField;
private Button createServerButton;
private Text createServeText;
private Button connectServerButton;
private Text connectServerText;
private Text warn;
private string ip = "192.168.0.118";
private int port = 9001;
private bool hasConnectServer;
private bool hasCreateServer;
private string myName;
private Dictionary namesInfoDictionary = new Dictionary();
private NetworkView networkView;
private string namesStr;
void Start()
{
peopleCount = transform.Find("bg/count").GetComponent< Text>();
peopleNames = transform.Find("bg/name").GetComponent< Text>();
chatText = transform.Find("bg/Image/Scroll View/Viewport/Content/Text").GetComponent< Text>();
inputField = transform.Find("bg/InputField").GetComponent< InputField>();
senon = transform.Find("bg/sendBtn").GetComponent< Button>();
myNameField = transform.Find("myname").GetComponent< InputField>();
createServerButton = transform.Find("createBtn").GetComponent< Button>();
createServeText = createServerButton.transform.Find("Text").GetComponent< Text>();
connectServerButton = transform.Find("connectBtn").GetComponent< Button>();
connectServerText = connectServerButton.transform.Find("Text").GetComponent< Text>();
warn = transform.Find("warn").GetComponent< Text>();
peopleCount.text = "0";
peopleNames.text = "";
chatText.text = "";
createServeText.text = "CreateServer";
connectServerText.text = "ConnectServer";
warn.text = "请先创建自己的昵称,根据自己需要创建/连接服务器。";
networkView = transform.GetComponent< NetworkView>();
senon.onClick.AddListener(delegate
{
networkView.RPC("SendMsg", RPCMode.All, myName + ":" + inputField.text);
});
createServerButton.onClick.AddListener(delegate
{
if (!hasCreateServer)
{
if (myNameField.text != null)
{
NetworkConnectionError error = Network.InitializeServer(20, port, false);
if (error == NetworkConnectionError.NoError)
{
myName = myNameField.text;
hasCreateServer = true;
myNameField.text = "";
createServeText.text = "DestroyServer";
warn.text = "";
namesInfoDictionary.Add(Network.player.ToString(), myName);
StoreName(namesInfoDictionary);
networkView.RPC("GetNames", RPCMode.All, namesStr);
networkView.RPC("SendMsg", RPCMode.All, myName + "创建了聊天室");
}
}
else
{
warn.text = "请创建自己的名字";
}
}
else
{
Network.Disconnect();
hasCreateServer = false;
createServeText.text = "CreateServer";
}
});
connectServerButton.onClick.AddListener(delegate
{
if (!hasConnectServer)
{
if (myNameField.text != null)
{
NetworkConnectionError error = Network.Connect(ip, port);
if (error == NetworkConnectionError.NoError)
{
myName = myNameField.text;
hasConnectServer = true;
myNameField.text = "";
connectServerText.text = "DisConnect";
warn.text = "";
}
}
else
{
warn.text = "请创建自己的名字";
}
}
else
{
Network.Disconnect();
hasConnectServer = false;
connectServerText.text = "ConnectServer";
}
});
}
---------------记录name列表,因为RPC不可以传入List集合,所以要把信息转为字符串------------
private void StoreName(Dictionary namesDictionary)
{
namesStr = "";
foreach (var VARIABLE in namesDictionary)
{
namesStr += VARIABLE.Value + ";";
}
}
-------------------------------------聊天,发送信息---------------------------------------
[RPC]
private void SendMsg(string info)
{
if (info != "")
{
chatText.text += info + "\n";
inputField.text = "";
}
}
----------------------客户端调用,通知服务器,更改namesInfoDictionary---------------------
void OnConnectedToServer()
{
networkView.RPC("PlayerJoin", RPCMode.Server, myName);
networkView.RPC("SendMsg", RPCMode.All, myName + "进入了聊天室");
}
--------------------服务器调用,更改namesInfoDictionary,并共享name列表------------------
[RPC]
private void PlayerJoin(string name, NetworkMessageInfo info)
{
namesInfoDictionary.Add(info.sender.ToString(), name);
StoreName(namesInfoDictionary);
networkView.RPC("GetNames", RPCMode.All, namesStr);
}
-------------------------------客户端断开连接,服务器调用----------------------------------
void OnPlayerDisconnected(NetworkPlayer player)
{
networkView.RPC("SendMsg", RPCMode.All, namesInfoDictionary[player.ToString()] + "离开了聊天室");
PlayerRemove(player.ToString());
}
--------------不必使用RPC,因为更新namesInfoDictionary只需在服务器端进行。----------------
private void PlayerRemove(string nameID)
{
if (namesInfoDictionary.ContainsKey(nameID))
{
namesInfoDictionary.Remove(nameID);
}
StoreName(namesInfoDictionary);
networkView.RPC("GetNames", RPCMode.All, namesStr);
}
---------------------------客户端与服务器更新名字列表,参数不能为List-------------------------
[RPC]
private void GetNames(string names)
{
namesStr = names;
peopleNames.text = "";
string[] str = namesStr.Split(';');
foreach (var VARIABLE in str)
{
if (VARIABLE != "")
peopleNames.text += VARIABLE + "\n";
}
peopleCount.text = (str.Length - 1).ToString();
}
}