PhotonNetworking Unity 播放器生成无法通过网络工作

问题描述 投票:0回答:2

这是已解决的代码。

已解决[]

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)

这是我的网络管理器

    /*
    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");
        }



    }
}

所以我没有收到任何错误,但它没有找到炮塔、枢轴点的子级并启用其鼠标外观,它只启用炮塔鼠标外观。

c# unity-game-engine unityscript photon
2个回答
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
组件:) 希望这有帮助!


0
投票

这对我没有帮助。你还有其他解决办法吗?

© www.soinside.com 2019 - 2024. All rights reserved.