PhotonNetworking Unity Player Spawn无法通过网络工作

时间:2014-09-03 23:59:53

标签: c# unity3d unityscript photon

这是解决的代码。

解决[]

using UnityEngine;
using System.Collections;

public class NetworkManager : MonoBehaviour {
    /*
    public Camera standbyCamera;*/
    // Use this for initialization
    SpawnSpot[] spawnSpots; 
    void Start () {
        Connect ();
        spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
    }

    void Connect(){
        PhotonNetwork.ConnectUsingSettings ("1.0.0");
    }

    void OnGui(){
        Debug.Log ("OnGui" + PhotonNetwork.connectionStateDetailed.ToString ());
        GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    }
    // Update is called once per frame
    void OnJoinedLobby () {
        Debug.Log ("Joined Lobby");
        PhotonNetwork.JoinRandomRoom ();
    }
    void OnPhotonRandomJoinFailed(){
        Debug.Log ("Failed Join");
        PhotonNetwork.CreateRoom (null);
    }
    void OnJoinedRoom() {
        Debug.Log ("Joined Room");
        SpawnMyPlayer ();
    }
    private GameObject pivot;
    private GameObject topDownCam;
    public void SpawnMyPlayer(){
        SpawnSpot mySpawnSpot = spawnSpots [ Random.Range (0, spawnSpots.Length) ];
        GameObject myPlayer = PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
        //standbyCamera.enabled = false;
        myPlayer.gameObject.SetActive (true);

        topDownCam = GameObject.FindWithTag("TopDown");
        topDownCam.GetComponent<Camera>().enabled = true;

        try
        {

            pivot = GameObject.FindWithTag("PivotObject");


            pivot.GetComponent<MouseLook>().enabled = true;

            MouseLook mouseScript = myPlayer.GetComponentInChildren<MouseLook>();
            if(mouseScript != null)
            {
                mouseScript.enabled = true;
            }

            CharMove moveScript = myPlayer.GetComponentInChildren<CharMove>();
            if(moveScript != null)
            {
                moveScript.enabled = true;
            }

            CharacterController controlScript = myPlayer.GetComponentInChildren<CharacterController>();
            if(controlScript != null)
            {
                controlScript.enabled = true;
            }

            Camera childCam = myPlayer.GetComponentInChildren<Camera>();
            if(childCam != null)
            {
                childCam.enabled = true;
            }

        }
        catch
        {
            Debug.Log ("Error finding children");
        }



    }
}

所以我有一个团结项目即时工作我们正在使用光子网络我附上了一个播放器结构图像和与之相关的脚本。出于某种原因,当我加载两个玩家时,他们无法看到对方。

我知道如果我不禁用播放器,我会看到两者但是控件将全部搞砸了,但是如果我禁用与角色的每个元素相关联的脚本,并保持字符启用,如教程所说我出于某些原因无法选择脚本我无法弄清楚如何获取组件。 enter image description here

我确实在控制台中获得了失败的连接,这是输出

Failed Join
UnityEngine.Debug:Log(Object)
NetworkManager:OnPhotonRandomJoinFailed() (at Assets/Game Assets/Prefab/Resources/Scripts/NetworkManager.cs:28)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1865)
NetworkingPeer:OnOperationResponse(OperationResponse) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1223)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)

然后它加入并且不会失败

Joined Room
UnityEngine.Debug:Log(Object)
NetworkManager:OnJoinedRoom() (at Assets/Game Assets/Prefab/Resources/Scripts/NetworkManager.cs:32)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
NetworkingPeer:SendMonoMessage(PhotonNetworkingMessage, Object[]) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1865)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1708)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)

这是我的NetworkManager

    /*
    public Camera standbyCamera;*/
    // Use this for initialization
    SpawnSpot[] spawnSpots; 
    void Start () {
        Connect ();
        spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
    }

    void Connect(){
        PhotonNetwork.ConnectUsingSettings ("1.0.0");
    }

    void OnGui(){
        Debug.Log ("OnGui" + PhotonNetwork.connectionStateDetailed.ToString ());
        GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    }
    // Update is called once per frame
    void OnJoinedLobby () {
        Debug.Log ("Joined Lobby");
        PhotonNetwork.JoinRandomRoom ();
    }
    void OnPhotonRandomJoinFailed(){
        Debug.Log ("Failed Join");
        PhotonNetwork.CreateRoom (null);
    }
    void OnJoinedRoom() {
        Debug.Log ("Joined Room");
        SpawnMyPlayer ();
    }
    void SpawnMyPlayer(){
        SpawnSpot mySpawnSpot = spawnSpots [ Random.Range (0, spawnSpots.Length) ];
        GameObject myPlayer = PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
        //standbyCamera.enabled = false;
        myPlayer.gameObject.SetActive (true);
        //((MonoBehaviour)myPlayer.GetComponent("CharacterController")).enabled = true;
        //((MonoBehaviour)myPlayer.GetComponent("CharMove")).enabled = true;



    }
}

新发展 如果我加入团结,然后再次加入其他玩家,我会收到以下错误

Received OnSerialization for view ID 2001. We have no such PhotonView! Ignored this if you're leaving a room. State: Joined
UnityEngine.Debug:LogWarning(Object)
NetworkingPeer:OnSerializeRead(Hashtable, PhotonPlayer, Int32, Int16) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:3342)
NetworkingPeer:OnEvent(EventData) (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/NetworkingPeer.cs:1758)
ExitGames.Client.Photon.PeerBase:DeserializeMessageAndCallback(Byte[])
ExitGames.Client.Photon.EnetPeer:DispatchIncomingCommands()
ExitGames.Client.Photon.PhotonPeer:DispatchIncomingCommands()
PhotonHandler:Update() (at Assets/Photon Unity Networking/Plugins/PhotonNetwork/PhotonHandler.cs:83)

我已经改变了一些代码,除了相机之外它似乎正在工作

    SpawnSpot[] spawnSpots; 
    void Start () {
        Connect ();
        spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
    }

    void Connect(){
        PhotonNetwork.ConnectUsingSettings ("1.0.0");
    }

    void OnGui(){
        Debug.Log ("OnGui" + PhotonNetwork.connectionStateDetailed.ToString ());
        GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    }
    // Update is called once per frame
    void OnJoinedLobby () {
        Debug.Log ("Joined Lobby");
        PhotonNetwork.JoinRandomRoom ();
    }
    void OnPhotonRandomJoinFailed(){
        Debug.Log ("Failed Join");
        PhotonNetwork.CreateRoom (null);
    }
    void OnJoinedRoom() {
        Debug.Log ("Joined Room");
        SpawnMyPlayer ();
    }
    public void SpawnMyPlayer(){
        SpawnSpot mySpawnSpot = spawnSpots [ Random.Range (0, spawnSpots.Length) ];
        GameObject myPlayer = PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
        //standbyCamera.enabled = false;
        myPlayer.gameObject.SetActive (true);
        //((MonoBehaviour)myPlayer.GetComponent("CharacterController")).enabled = true;
        ((MonoBehaviour)myPlayer.GetComponent("CharMove")).enabled = true;

        Transform[] allChildren = GetComponentsInChildren<Transform>();
        foreach (Transform child in allChildren) {
            ((MonoBehaviour)child.GetComponent("MouseLook")).enabled = true;
            child.transform.FindChild("Camera").gameObject.SetActive (true);
        }

        //myPlayer.transform.FindChild ("Camera").gameObject.SetActive (true);

    }
}

当我产生第二个角色时,它不会启动相机并且技术上控制第一个角色相机中的第二个角色。

当我继续编码时,我想出了这个

        spawnSpots = GameObject.FindObjectsOfType<SpawnSpot> ();
    }

    void Connect(){
        PhotonNetwork.ConnectUsingSettings ("1.0.0");
    }

    void OnGui(){
        Debug.Log ("OnGui" + PhotonNetwork.connectionStateDetailed.ToString ());
        GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
    }
    // Update is called once per frame
    void OnJoinedLobby () {
        Debug.Log ("Joined Lobby");
        PhotonNetwork.JoinRandomRoom ();
    }
    void OnPhotonRandomJoinFailed(){
        Debug.Log ("Failed Join");
        PhotonNetwork.CreateRoom (null);
    }
    void OnJoinedRoom() {
        Debug.Log ("Joined Room");
        SpawnMyPlayer ();
    }
    public void SpawnMyPlayer(){
        SpawnSpot mySpawnSpot = spawnSpots [ Random.Range (0, spawnSpots.Length) ];
        GameObject myPlayer = PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
        //standbyCamera.enabled = false;
        myPlayer.gameObject.SetActive (true);
        //((MonoBehaviour)myPlayer.GetComponent("CharacterController")).enabled = true;



        try
        {
            MouseLook mouseScript = myPlayer.GetComponentInChildren<MouseLook>();
            if(mouseScript != null)
            {
                mouseScript.enabled = true;
            }
            CharMove moveScript = myPlayer.GetComponentInChildren<CharMove>();
            if(moveScript != null)
            {
                moveScript.enabled = true;
            }
            CharacterController controlScript = myPlayer.GetComponentInChildren<CharacterController>();
            if(controlScript != null)
            {
                controlScript.enabled = true;
            }
            Camera childCam = myPlayer.GetComponentInChildren<Camera>();
            if(childCam != null)
            {
                childCam.enabled = true;
            }

        }
        catch
        {
            Debug.Log ("Error finding children");
        }



    }
}

所以我没有得到任何错误,但它没有找到Turret的孩子,PivotPoint和启用它的鼠标外观它只能启用炮塔鼠标外观。

1 个答案:

答案 0 :(得分:0)

我在你设置的SpawnMyPlayer函数中有一个答案

GameObject myPlayer = PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0); 

必须是

GameObject myPlayer = (GameObject)PhotonNetwork.Instantiate ("Player", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);

同样在编辑器中尝试添加Photon View组件:) 希望这有助于!