facebook twitter hatena line email

Unity/photon/pun1/chat

提供: 初心者エンジニアの簡易メモ
2017年10月20日 (金) 16:44時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==サンプル== using System.Collections; using System.Collections.Generic; using UnityEngine; using ExitGames.Client.Photon.Chat; using ExitGames.Client.Photon; p...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

サンプル

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ExitGames.Client.Photon.Chat;
using ExitGames.Client.Photon;
public class ChatScript : MonoBehaviour, IChatClientListener {
	public static ChatScript instance;
	public ChatClient chatClient;
	private ConnectionProtocol connectionProtocol = ConnectionProtocol.Udp;
	private ExitGames.Client.Photon.Chat.AuthenticationValues authValues = new ExitGames.Client.Photon.Chat.AuthenticationValues();
	private string channelName;
	public string GetChannelName()
	{
		return channelName;
	}
	// Use this for initialization
	void Start () {
		//Singleton pattern
		instance = this;
		chatClient = new ChatClient (this, connectionProtocol);
		chatClient.ChatRegion = "EU";
		authValues.UserId = "guest1"; // PhotonNetwork.playerName;
		authValues.AuthType = ExitGames.Client.Photon.Chat.CustomAuthenticationType.None;
		chatClient.Connect(this.appId, "4.2", authValues); // PhotonNetwork.PhotonServerSettings.ChatAppID
		channelName = "channel" ; // PhotonNetwork.room.Name
	}
	// Update is called once per frame
	void Update () {
		if (chatClient != null) chatClient.Service();
	}
	public void PublishMessage(string channelName, string message)
	{
		chatClient.PublishMessage(channelName, message);
	}
	public void DebugReturn(DebugLevel level, string message)
	{
		Debug.Log("Debug level "+ level);
		Debug.Log("Message " + message);
	}
	public void OnChatStateChange(ChatState state)
	{
		Debug.Log("Chat state: " + state);
	}
	public void OnConnected()
	{
		chatClient.Subscribe(new string[] { channelName });

	}
	public void OnDisconnected()
	{
		if (chatClient != null) chatClient.Disconnect();
	}
	public void OnGetMessages(string channelName, string[] senders, object[] messages)
	{
		int msgCount = messages.Length;
		for (int i = 0; i < msgCount; i++) {
			string sender = senders[i];
			string msg = (string)messages[i];
			Debug.Log(sender + ": " + msg);
		}
	}
	public void OnPrivateMessage(string sender, object message, string channelName)
	{
		// throw new NotImplementedException();
	}
	public void OnStatusUpdate(string user, int status, bool gotMessage, object message)
	{
		Debug.Log("user: " + user);
		Debug.Log("status: " + status);
		Debug.Log("gotMessage: " + gotMessage);
		Debug.Log("message: " + message);
	}
	public void OnSubscribed(string[] channels, bool[] results)
	{
		Debug.Log("Suscribed to channel");
		// sumple送信
		PublishMessage( channelName, "So Long, and Thanks for All the Fish!" );
	}
	public void OnUnsubscribed(string[] channels)
	{
		Debug.Log("Unsubscribed");
	}
}

一応上記で送信まで行けた。

注意:PhotonUnityNetworkプラグインもimportするとAccountServiceクラスが2重になるなどのエラーが出る。

参考

http://answers.unity3d.com/questions/1366044/unity-crashes-when-compiling-this-script-where-is.html