我正在使用光子为Android制作FPS游戏。 这是代码:
using UnityEngine;
using UnityEngine.UI;
public class SceneLoaderButton : Photon.PunBehaviour {
public string roomName, mapNameGL, password;
public GameObject loadingPan;
public MenuRooms menuManager;
// Use this for initialization
void Start () {
Button btn = GetComponent<Button> ();
btn.onClick.AddListener (ConnectCustomRoom);
}
// Update is called once per frame
void Update () {
}
void ConnectCustomRoom(){
string room = roomName;
RoomInfo[] ri;
ri = PhotonNetwork.GetRoomList ();
bool correct = false;
string passwd, mapName = "";
passwd = password;
foreach (RoomInfo info in ri) {
if (info.name == room) {
if (info.customProperties ["Password"].ToString() == passwd) {
print(info.playerCount + "/" + info.maxPlayers);
if (info.playerCount < info.maxPlayers)
{
correct = true;
}
else
{
menuManager.error("No room for you");
}
mapName = info.customProperties ["MapName"].ToString ();
}
else
{
menuManager.error("Incorrect password");
}
}
}
mapNameGL = mapName;
print(mapNameGL);
if (correct) {
print("Correct");
loadingPan.active = !loadingPan.active;
PhotonNetwork.playerName = "Player" + UnityEngine.Random.Range (1000,9999).ToString();
PhotonNetwork.JoinRoom(room);
}
}
void OnJoinedRoom()
{
print("Joined room: " + roomName);
//We joined room, load respective map
Application.LoadLevel(mapNameGL);
}
}
这是按钮的代码。它被实例化,它应该加入房间,然后加载场景。在其他脚本中,“onjoinedroom”回调工作,即使我从Photon.MonoBehaveiour继承,而不是从PUNBehaveiour继承。有什么问题?
答案 0 :(得分:1)
基于PUN文档,它是一个虚拟成员,因此您可以覆盖该方法。
尝试将方法更改为:
override void OnJoinedRoom ()
{
//your codes
}
//or
public override void OnJoinedRoom ()
{
//your codes
}
此类提供.photonView以及PUN的所有回调/事件 能打电话。覆盖要使用的事件/方法。通过扩展 在这个类中,您可以将各个方法实现为覆盖。
希望能帮到你。