客户端播放器在服务器统一中不可见5

时间:2017-12-02 10:16:46

标签: c# unity3d unity5 multiplayer unity-networking

这里我试图通过按钮点击在客户端实例化播放器。这是我的剧本。我可以在客户端看到服务器,但无法在服务器中看到客户端播放器。任何人都可以提供帮助。?

我是使用自定义脚本的实例化播放器。按钮点击后播放器实例化。

按btnPlayer按钮播放器实例化。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Network_Manager_Custom : NetworkManager {

    private Animator anim;
    //[SerializeField]private GameObject btnDisconnect;
    [SerializeField]private GameObject _player, SpawnPointsParent, btnPlayer;
    [SerializeField]private List<GameObject> sPoints;
    private bool checkState = false;
    private int iPos = 0;

    GameObject ga;

    public void StartupHost()
    {       
        setPort ();
        NetworkManager.singleton.StartHost ();
        sPoints = new List<GameObject> ();
    }

    public void JoinGame()
    {
        setIPAddress ();
        setPort (); 
        NetworkManager.singleton.StartClient ();
        iPos = iPos + 1;
    }

    public void setIPAddress()
    {
        string IPAddress = GameObject.Find ("InputFieldIPAddress").transform.FindChild("Text").GetComponent<Text>().text;
        NetworkManager.singleton.networkAddress = IPAddress;
        Debug.Log ("IPAddress : "+ IPAddress);
    }
    void setPort()
    {
        NetworkManager.singleton.networkPort = 7777;
    }

    void OnLevelWasLoaded(int level)
    {
        if (level == 0) {
            SetupMenuScreenButtons ();
        } else {            
            SetupOtherSceneButtons ();
        }
    }
    void SetupMenuScreenButtons()
    {
        GameObject.Find ("btnStartHost").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find ("btnStartHost").GetComponent<Button> ().onClick.AddListener (StartupHost);

        GameObject.Find ("btnJoinGame").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find ("btnJoinGame").GetComponent<Button> ().onClick.AddListener (JoinGame);

    }
    void SetupOtherSceneButtons()
    {       
        //btnDisconnect = GameObject.Find ("btnDisconnect");
        //if (btnDisconnect != null) {
        GetListOfPlayer ();

        GameObject.Find ("btnDisconnect").GetComponent<Button> ().onClick.RemoveAllListeners ();
        GameObject.Find ("btnDisconnect").GetComponent<Button> ().onClick.AddListener (NetworkManager.singleton.StopHost);

        GameObject.Find ("btnPlayer").GetComponent<Button>().onClick.RemoveAllListeners();
        GameObject.Find ("btnPlayer").GetComponent<Button> ().onClick.AddListener (PlayerInstantiateHere);

        //} else {
        //  Debug.Log("btnDisconnect not available.!");
        //}
    }
    void GetListOfPlayer()
    {
        if(SpawnPointsParent == null)
            SpawnPointsParent = GameObject.Find ("SpawnPoints");

        foreach(Transform t in SpawnPointsParent.transform)
        {
            sPoints.Add (t.gameObject);
        }
    }

    public void PlayerInstantiateHere()
    {       
        if (_player != null) {
            //NetworkManager.Instantiate (_player, sPoints [iPos].transform.position, sPoints [iPos].transform.rotation);
            GameObject ga = (GameObject) NetworkBehaviour.Instantiate(_player, sPoints [iPos].transform.position, sPoints [iPos].transform.rotation);
            ClientScene.RegisterPrefab (ga);
            NetworkServer.Spawn (ga);

        }
        Debug.Log ("Player button clicked.!");
    }

}

1 个答案:

答案 0 :(得分:0)

你的代码似乎没问题,我想你们两个玩家都在同一个位置上产卵。由于这个原因,通过提供不同的位置使其成为radomize: 请添加一些空的游戏对象(位置)并添加NetworkStartPosition组件。最后还将PlayerSpawnMethod设置为循环。