[Unity&Photon]Photon Chat 对话框测试

来源:互联网 发布:最新韩国网络剧2017 编辑:程序博客网 时间:2024/05/16 15:52

本文主要是根据 参考资料1,进行的测试。


Photon Chat Intro 手册地址如下所示

https://doc.photonengine.com/en-us/chat/current/getting-started/chat-intro


新建 对象 UI 如下所示,

Panel_Intro 的界面如下所示



Panel_Msg 页面如下所示



文本框位置如下所示:



新建一个空对象,命名为Chat,其设置如下所示



Photon Engine 官网 : https://www.photonengine.com/en-US/Photon

在Photon Engine 官网,登录账号,点击如下图所示位置



给App 自定义 一个名字


复制 App ID 


找到 Resources 文件夹下面 的 PhotonServerSettings

PhotonServerSettings的设置如下所示,把复制 的App ID 粘贴到如下图所示的 Chat AppId 位置



---------------------------------------------------------------------------------------


代码

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.UI;using ExitGames.Client.Photon.Chat;//using ExitGames.Client.Photon;using System;public class TC_chat : MonoBehaviour, IChatClientListener{    public ChatClient chatClient;    public InputField p1rName;    public Text connectionState;    private string worldChat;    public InputField msgInput;    public Text msgArea;    public GameObject introPanel;    public GameObject msgPanel;    private void Start()    {        Application.runInBackground = true;        if (string.IsNullOrEmpty(PhotonNetwork.PhotonServerSettings.ChatAppID))        {            print(" No chat ID provided  ");            return;        }        {            print(" Have ID provided  ");        }        connectionState.text = "Connecting....";        worldChat = "world";    }        private void Update()    {        if (this.chatClient != null)            this.chatClient.Service();    }    public void getConnected()    {        print("Trying to connect");        this.chatClient = new ChatClient(this);//        this.chatClient.Connect(PhotonNetwork.PhotonServerSettings.ChatAppID,"anything",new ExitGames.Client.Photon.Chat.AuthenticationValues(p1rName.text));//        //string str_var = "Connecting to chat";        connectionState.text = "Connecting to chat";//        //this.chatClient.Connect(ChatSettings.Instance.AppId, "1.0", new ExitGames.Client.Photon.Chat.AuthenticationValues(UserName));    }    public void sendMsg()        {        Debug.Log("  提交信息 ");        this.chatClient.PublishMessage(worldChat,msgInput.text);        }    #region    public void DebugReturn(DebugLevel level, string message)    { }    public void OnDisconnected()    {        Debug.Log("**************************   DisConnected");    }    public void OnConnected()    {        msgArea.text = "";        introPanel.SetActive(false);        msgPanel.SetActive(true);        connectionState.text = "Conncted";        Debug.Log("**************************   Connected");        this.chatClient.Subscribe(new string[] { "world"});//worldChat = "world"        this.chatClient.SetOnlineStatus(ChatUserStatus.Online);        //OnSubscribed(new string[] { worldChat },new bool[]{true });//worldChat = "world"    }    public void OnChatStateChange(ChatState state)    { }    public void OnGetMessages(string channelName, string[] senders, object[] messages)    {       // Debug.Log("  msgArea  "+msgArea);        if (msgArea!=null)        {            for (int i = 0; i < senders.Length; i++)            {                msgArea.text += senders[i] + " : " + messages[i] + "\n";            }        }    }    public void OnPrivateMessage(string sender, object message, string channelName)    { }    public void OnSubscribed(string[] channels, bool[] results)    {        connectionState.text = "In World Chat";        this.chatClient.PublishMessage(worldChat,"Joined");    }    public void OnUnsubscribed(string[] channels)    { }    public void OnStatusUpdate(string user, int status, bool gotMessage, object message)    { }    #endregion}


---------------------------------------------------------------------------------------

可能会遇到的问题

state 字符串 显示不全,进行如下设置



如果是

public class TC_chat : MonoBehaviour, IChatClientListener

问题,查看参考资料2


---------------------------------------------------------------------------------------

参考资料:

1.PUN : Photon Unity Network - Photon Chat Intro

2. 

[Unity&接口]子类即继承接口类也继承MonoBehaviour的快速操作和重构实现

3.

4.

原创粉丝点击