unity用PUN进行信息交互模块


 

using UnityEngine;
using System.Collections.Generic;

public class MessageChat : Photon.MonoBehaviour
{
	public string mess ="Is Done";//用来发送的信息
    	public static MessageChat MC;//静态对象,方便全局调用
	
    	void Awake()
   	{
		MC = this; //初始实例化
   	}

	void Update()
	{
		if (Input.GetKeyDown (KeyCode.F1)) 
		{
			//给全部发送信息,其他发送方式参照PhotonTargets
			SendChat(PhotonTargets.All);
		}
		if (Input.GetKeyDown (KeyCode.F2)) 
		{
			foreach(PhotonPlayer palyer in PhotonNetwork.playerList)
			{
				SendChat(player);//给指定的用户发送信息
			}

		}
	}
	//定义信息发送函数, --重载
    	void SendChat(PhotonTargets target)
    	{
		photonView.RPC("SendChatMessage", target, mess);
    	}

    	void SendChat(PhotonPlayer target)
    	{
        	chatInput = "[PM] " + chatInput;
		photonView.RPC("SendChatMessage", target, mess);
    	}
	//RPC函数,在信息交互时,所有客户端都会执行这个函数
	[RPC]
	//第一个参数必须为基本类型,如string,int等,第二个参数可不加
	void SendChatMessage(string str, PhotonMessageInfo info)
	{
		Debug.Log (""+info.sender+str);
	}

   
}


 

 

原文地址:https://www.cnblogs.com/liang123/p/6325924.html