误打误撞学习了一下Photo服务器,自己去百度上找,有些问题大家也没说清楚,所以,在这里补充说明一下:
现在Photo官网已经更新到4.0.29(说不定有更新了),但是很多教程都是Photo3.0的版本,虽然大体还是差不多,但是对于初学者来说还是非常苦恼的
所以本人在这提供Photo3.0版本的下载链接:链接:http://pan.baidu.com/s/1pL16a2v 密码:hvwi
我使用Photo中提供的Lite实现了一个简单的聊天,因为Lite服务器中没有聊天的事件,所以我们要自己写并且注册聊天事件
首先我们仿照Lite服务器中的事件使用枚举声明聊天事件
然后,在OnGUI函数中注册这个事件:
然后,在OnEvent()函数中就可以半段这个方法了:
完成这些,一个简单的聊天的程序就完成了,附上完整的脚本。
using UnityEngine;using System.Collections;using ExitGames.Client.Photon;using ExitGames.Client.Photon.Lite;using System.Collections.Generic;public class GameClient : MonoBehaviour,IPhotonPeerListener { // Use this for initialization LitePeer peer; public enum ChatEventCode : byte { Chat = 10} public enum ChatEventKey : byte { UserName = 11,ChatMessage = 12} void Start () { peer = new LitePeer(this, ConnectionProtocol.Udp); peer.Connect("127.0.0.1:4530","Lite"); } // Update is called once per frame void Update () { peer.Service(); } void SendMessage(OperationRequest operationRequest) { peer.OpCustom(operationRequest,true,0,false); } string Username = ""; string Message = ""; string chatMessage = ""; Dictionary<int, string> ActorLister = new Dictionary<int, string>(); void OnGUI() { Username = GUI.TextField(new Rect(Screen.width / 2 - 50, 70, 100, 20), Username); if(GUI.Button(new Rect(Screen.width/2-50,100,100,30),"进入游戏房间")) { Hashtable gameProperties, actorProperties; gameProperties = new Hashtable(); actorProperties = new Hashtable(); actorProperties.Add((byte)ChatEventKey.UserName, Username); peer.OpJoin("LOL",gameProperties,actorProperties,true); } if (GUI.Button(new Rect(Screen.width / 2 - 50, 150, 100, 30), "离开游戏房间")) { peer.OpLeave(); } GUI.Label(new Rect(Screen.width / 2 - 50, 200, 150, 20), Message); chatMessage = GUI.TextField(new Rect(Screen.width / 2 - 50, 250, 150, 20), chatMessage); if (GUI.Button(new Rect(Screen.width / 2 - 50, 300, 100, 30), "发送消息")) { Hashtable chatConent = new Hashtable(); chatConent.Add(ChatEventKey.ChatMessage,chatMessage); peer.OpRaiseEvent((byte)ChatEventCode.Chat,chatConent,true); } } public void DebugReturn(DebugLevel level, string message) { Debug.Log(message); } public void OnEvent(EventData eventData) { Debug.Log(eventData.ToStringFull()); switch (eventData.Code) { case LiteEventCode.Join: { string userName = ((Hashtable)eventData.Parameters[LiteEventKey.ActorProperties])[(byte)ChatEventKey.UserName].ToString(); Message = "玩家" + userName + "进入游戏"; int actNum = (int)eventData.Parameters[LiteEventKey.ActorNr]; if (!ActorLister.ContainsKey(actNum)) { ActorLister.Add(actNum, userName); } else { ActorLister[actNum] = userName; } } break; case LiteEventCode.Leave: { int actNum = (int)eventData.Parameters[LiteEventKey.ActorNr]; if (ActorLister.ContainsKey(actNum)) { Message = "玩家" + ActorLister[actNum] + "离开游戏"; } } break; case (byte)ChatEventCode.Chat: { Hashtable customEventContent = eventData.Parameters[LiteEventKey.Data] as Hashtable; string Chatmessage = customEventContent[(byte)ChatEventKey.ChatMessage].ToString(); int actNum = (int)eventData.Parameters[LiteEventKey.ActorNr]; if (ActorLister.ContainsKey(actNum)) { Message = ActorLister[actNum] + "说:"+Chatmessage; } } break; } } public void OnOperationResponse(OperationResponse operationResponse) { Debug.Log("Server response"+operationResponse.ToStringFull()); switch (operationResponse.OperationCode) { case LiteOpCode.Join: { int num = (int)operationResponse.Parameters[LiteOpKey.ActorNr]; Debug.Log("进入游戏房间,玩家编号:"+num); } break; case LiteOpCode.Leave: { Debug.Log("离开游戏房间"); } break; default: ExcuteMessage(operationResponse); break; } } void ExcuteMessage(OperationResponse operationResponse) { } public void OnStatusChanged(StatusCode statusCode) { switch(statusCode) { case StatusCode.Connect: Debug.Log("Connect Success"); break; case StatusCode.Disconnect: Debug.Log("Connect faile"); break; case StatusCode.ExceptionOnConnect: Debug.Log("Exception"); break; } }}
转载于:https://www.cnblogs.com/cJJJ/p/6758061.html
相关资源:JAVA上百实例源码以及开源项目