繁体   English   中英

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

[英]PhotonNetworking Unity Player Spawn Not Working Over Network

这里是解决的代码。

解决了[]

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



    }
}

因此,我有一个统一的项目正在研究,我们正在使用光子网络,我附加了播放器结构和与之相关的脚本的图像。 由于某些原因,当我加载两个播放器时,它们将无法看到对方。

我知道如果不禁用播放器,我会看到两者,但是控件都会搞砸,但是,如果我禁用与角色的每个元素相关联的脚本,并保持角色启用,如教程中所述,我可以由于某种原因我无法选择脚本,因此我无法弄清楚如何使用这些组件。 在此处输入图片说明

我确实在控制台中加入失败,这是输出

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



    }
}

因此,我没有收到任何错误,但没有找到“炮塔”,“ PivotPoint”和“启用其鼠标外观”的子级,它仅启用了炮塔鼠标外观。

我在您设置的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组件:)希望对您有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM