在旧教程中重新定位旧的Unity GUI按钮

时间:2016-12-12 02:32:47

标签: unity5 photon

我一直在尝试使用Photon Unity Network设置团队。我的两个问题中的第一个是关于旧的GUI按钮。大多数答案都指向了quill18,在他的FPS系列中,#19和20.问题是它是否使用旧的GUI按钮,他使用了一些代码自动将它放在场景的中心。此位置隐藏背景图像中的按钮。我尝试将OnGui方法中的Y值替换为降低到底部的三分之一,但没有任何反应。如何降低这些按钮?这似乎比尝试用新的GUI替换旧版本更容易。

using UnityEngine;
using System.Collections.Generic;

public class NetworkManager : MonoBehaviour {

    public GameObject standbyCamera;
    SpawnSpot[] spawnSpots;

    public bool offlineMode = false;

    bool connecting = false;

    List<string> chatMessages;
    int maxChatMessages = 5;

    public float respawnTimer = 0;

    bool hasPickedTeam = false;
    int teamID=0;

    // Use this for initialization
    void Start () {
        spawnSpots = GameObject.FindObjectsOfType<SpawnSpot>();
        PhotonNetwork.player.name = PlayerPrefs.GetString("Username", "Awesome Dude");
        chatMessages = new List<string>();
    }

    void OnDestroy() {
        PlayerPrefs.SetString("Username", PhotonNetwork.player.name);
    }

    public void AddChatMessage(string m) {
        GetComponent<PhotonView>().RPC ("AddChatMessage_RPC", PhotonTargets.AllBuffered, m);
    }

    [PunRPC]
    void AddChatMessage_RPC(string m) {
        while(chatMessages.Count >= maxChatMessages) {
            chatMessages.RemoveAt(0);
        }
        chatMessages.Add(m);
    }

    void Connect() {
        PhotonNetwork.ConnectUsingSettings( "MultiFPS v004" );
    }

    void OnGUI() {
        GUILayout.Label( PhotonNetwork.connectionStateDetailed.ToString() );

        if(PhotonNetwork.connected == false && connecting == false ) {
            // We have not yet connected, so ask the player for online vs offline mode.
            GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();

            GUILayout.BeginHorizontal();
            GUILayout.Label("Username: ");
            PhotonNetwork.player.name = GUILayout.TextField(PhotonNetwork.player.name);
            GUILayout.EndHorizontal();

            if( GUILayout.Button("Single Player") ) {
                connecting = true;
                PhotonNetwork.offlineMode = true;
                OnJoinedLobby();
            }

            if( GUILayout.Button("Multi Player") ) {
                connecting = true;
                Connect ();
            }

            GUILayout.FlexibleSpace();
            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
            GUILayout.EndArea();
        }

        if(PhotonNetwork.connected == true && connecting == false) {

            if(hasPickedTeam) {
                // We are fully connected, make sure to display the chat box.
                GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
                GUILayout.BeginVertical();
                GUILayout.FlexibleSpace();

                foreach(string msg in chatMessages) {
                    GUILayout.Label(msg);
                }

                GUILayout.EndVertical();
                GUILayout.EndArea();
            }
            else {
                // Player has not yet selected a team.

                GUILayout.BeginArea( new Rect(0, 0, Screen.width, Screen.height) );
                GUILayout.BeginHorizontal();
                GUILayout.FlexibleSpace();
                GUILayout.BeginVertical();
                GUILayout.FlexibleSpace();

                if( GUILayout.Button("Red Team") ) {
                    SpawnMyPlayer(1);
                }

                if( GUILayout.Button("Green Team") ) {
                    SpawnMyPlayer(2);
                }

                if( GUILayout.Button("Random") ) {
                    SpawnMyPlayer(Random.Range(1,3));   // 1 or 2
                }

                if( GUILayout.Button("Renegade!") ) {
                    SpawnMyPlayer(0);
                }

                GUILayout.FlexibleSpace();
                GUILayout.EndVertical();
                GUILayout.FlexibleSpace();
                GUILayout.EndHorizontal();
                GUILayout.EndArea();


            }

        }

    }

    void OnJoinedLobby() {
        Debug.Log ("OnJoinedLobby");
        PhotonNetwork.JoinRandomRoom();
    }

    void OnPhotonRandomJoinFailed() {
        Debug.Log ("OnPhotonRandomJoinFailed");
        PhotonNetwork.CreateRoom( null );
    }

    void OnJoinedRoom() {
        Debug.Log ("OnJoinedRoom");

        connecting = false;
        //SpawnMyPlayer();
    }

    void SpawnMyPlayer(int teamID) {
        this.teamID = teamID;
        hasPickedTeam = true;
        AddChatMessage("Spawning player: " + PhotonNetwork.player.name);

        if(spawnSpots == null) {
            Debug.LogError ("WTF?!?!?");
            return;
        }

        SpawnSpot mySpawnSpot = spawnSpots[ Random.Range (0, spawnSpots.Length) ];
        GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate("PlayerController", mySpawnSpot.transform.position, mySpawnSpot.transform.rotation, 0);
        standbyCamera.SetActive(false);

        //((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;
        ((MonoBehaviour)myPlayerGO.GetComponent("MouseLook")).enabled = true;
        ((MonoBehaviour)myPlayerGO.GetComponent("PlayerMovement")).enabled = true;
        ((MonoBehaviour)myPlayerGO.GetComponent("PlayerShooting")).enabled = true;

        myPlayerGO.GetComponent<TeamMember>().teamID = teamID;
        SkinnedMeshRenderer mySkin = myPlayerGO.transform.GetComponentInChildren<SkinnedMeshRenderer>();

        if(mySkin == null) {
            Debug.LogError("Couldn't find a SkinnedMeshRenderer!");
        }

        if(teamID==1)
            mySkin.material.color = Color.red;
        if(teamID==2)
            mySkin.material.color = new Color(.5f, 1f, .5f);

        myPlayerGO.transform.FindChild("Main Camera").gameObject.SetActive(true);
    }

    void Update() {
        if(respawnTimer > 0) {
            respawnTimer -= Time.deltaTime;

            if(respawnTimer <= 0) {
                // Time to respawn the player!
                SpawnMyPlayer(teamID);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我强烈建议更新到新的用户界面。

尝试在灵活空间https://docs.unity3d.com/ScriptReference/GUILayout.Space.html

旁边插入GUILayout.Space

这通常有助于在灵活的内容布局中移动内容。

再见,