Search Unity

UI Button Instead of OnGUI Button need help?

Discussion in 'Immediate Mode GUI (IMGUI)' started by DankerCoder, Oct 18, 2018.

  1. DankerCoder

    DankerCoder

    Joined:
    Oct 18, 2018
    Posts:
    1
    Hello, I am trying to create the main menu for a game I am making. it will need to be made using unity UI but I currently have it made using OnGUI in Visual Studio and I am not sure how to make this change as I am very new to the OnGUI and I have been watching tutorials/random videos on ways around but can not find any help so I'm reaching out to anyone willing to lend me a hand.
    I will need the following
    - The Main Menu (With 3 buttons 1. The Host Game 2. The Join Game .3 The Exit)
    - The Host Game Menu (With 4 items 1.The PlayerName 2. The Room Name 3. The Create button .4 The Back Button)
    - The Join Lobby Menu (With 3 Buttons 1. The Name 2. The Select Lobby 3. The Back Button)
    I Have some useful photos of my project currently this should help with an understanding of the things I need for my menu thanks
    https://drive.google.com/open?id=1pjuMJRcm1mWgbNH0NvfZBMRN3DUmETsp
    Here is my Room Manager Script this script has most of the code that I use for the GUI in it but I have some more I will post below
    using UnityEngine;

    Code (CSharp):
    1.     public class RoomManager : Photon.MonoBehaviour
    2.     {
    3.  
    4.         public string verNum = "0.1";
    5.         public string roomName = "room01";
    6.         public string playerName = "player 420";
    7.         public Transform spawnPoint;
    8.         public GameObject playerPref;
    9.         public bool isConnected = false;
    10.         public Transform[] spawnPoints;
    11.         public bool isInRoom = false;
    12.         public InRoomChat chat;
    13.  
    14.         void Update()
    15.         {
    16.             if (isInRoom)
    17.             {
    18.                 chat.enabled = true;
    19.             }
    20.             else
    21.             {
    22.                 chat.enabled = false;
    23.             }
    24.          
    25.         }
    26.  
    27.         void Start()
    28.         {
    29.             roomName = "Room " + Random.Range(0, 999);
    30.             playerName = "Player " + Random.Range(0, 999);
    31.             PhotonNetwork.ConnectUsingSettings(verNum);
    32.             Debug.Log("Starting Connection!");
    33.         }
    34.  
    35.         public void OnJoinedLobby()
    36.         {
    37.             //PhotonNetwork.JoinOrCreateRoom (roomName, null, null);
    38.             isConnected = true;
    39.             Debug.Log("Starting Server!");
    40.         }
    41.  
    42.         public void OnJoinedRoom()
    43.         {
    44.             PhotonNetwork.playerName = playerName;
    45.             isConnected = false;
    46.             isInRoom = true;
    47.             //spawnPlayer();
    48.         }
    49.  
    50.  
    51.         public void spawnPlayer()
    52.         {
    53.             isInRoom = false;
    54.             GameObject pl = PhotonNetwork.Instantiate(playerPref.name, spawnPoints[Random.Range (0, spawnPoints.Length)].position, spawnPoint.rotation, 0) as GameObject;
    55.             pl.GetComponent<RigidbodyFPSWalker>().enabled = true;
    56.             pl.GetComponent<RigidbodyFPSWalker>().fpsCam.SetActive(true);
    57.             pl.GetComponent<RigidbodyFPSWalker>().graphics.SetActive(false);
    58.  
    59.         }
    60.  
    61.         void OnGUI()
    62.         {
    63.  
    64.             if (isConnected)
    65.             {
    66.                 GUILayout.BeginArea(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 250, 500, 500));
    67.                 playerName = GUILayout.TextField(playerName);
    68.                 roomName = GUILayout.TextField(roomName);
    69.  
    70.                 if (GUILayout.Button("Create"))
    71.                 {
    72.                     PhotonNetwork.JoinOrCreateRoom(roomName, null, null);
    73.                 }
    74.  
    75.                 foreach (RoomInfo game in PhotonNetwork.GetRoomList())
    76.                 {
    77.                     if (GUILayout.Button(game.name + " " + game.playerCount + "/" + game.maxPlayers))
    78.                     {
    79.                         PhotonNetwork.JoinOrCreateRoom(game.name, null, null);
    80.                     }
    81.                 }
    82.                 GUILayout.EndArea();
    83.             }
    84.             if(isInRoom)
    85.             {
    86.                 GUILayout.BeginArea(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 250, 500, 500));
    87.                 GUILayout.Box("Score: " + PhotonNetwork.player.GetScore());
    88.  
    89.                 if (GUILayout.Button("Spawn"))
    90.                 {
    91.                     spawnPlayer();
    92.                 }
    93.  
    94.                 if (GUILayout.Button("Disconnect"))
    95.                 {
    96.                     PhotonNetwork.Disconnect ();
    97.                     Application.LoadLevel (0);
    98.                 }
    99.  
    100.                 GUILayout.EndArea();
    101.  
    102.             }
    103.         }
    104.  
    105.  
    106.     }
    here is My RigidbodyFps Script some code near the bottom
    using UnityEngine;


    Code (CSharp):
    1.   public class RigidbodyFPSWalker : MonoBehaviour {
    2.  
    3.         public float speed = 10.0f;
    4.         public float gravity = 10.0f;
    5.         public float maxVelocityChange = 10.0f;
    6.         public bool canJump = true;
    7.         public float jumpHeight = 2.0f;
    8.         private bool grounded = false;
    9.  
    10.         public GameObject me;
    11.         public GameObject graphics;
    12.         public int health = 100;
    13.         public GameObject fpsCam;
    14.         private Rigidbody rigidbody;
    15.         public PhotonView name;
    16.         public bool isPause = false;
    17.         public AudioSource sound1;
    18.         public AudioClip soundClip;
    19.         public GameObject ragDoll;
    20.  
    21.  
    22.         void Awake()
    23.         {
    24.             name.RPC("updateName", PhotonTargets.AllBuffered, PhotonNetwork.playerName);
    25.             rigidbody = GetComponent<Rigidbody>();
    26.             rigidbody.freezeRotation = true;
    27.             rigidbody.useGravity = false;
    28.         }
    29.  
    30.         void FixedUpdate()
    31.         {
    32.             if (grounded)
    33.             {
    34.                 // Calculate how fast we should be moving
    35.                 Vector3 targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    36.                 targetVelocity = transform.TransformDirection(targetVelocity);
    37.                 targetVelocity *= speed;
    38.  
    39.                 // Apply a force that attempts to reach our target velocity
    40.                 Vector3 velocity = rigidbody.velocity;
    41.                 Vector3 velocityChange = (targetVelocity - velocity);
    42.                 velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    43.                 velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    44.                 velocityChange.y = 0;
    45.                 rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    46.  
    47.                 // Jump
    48.                 if (canJump && Input.GetButton("Jump"))
    49.                 {
    50.                     rigidbody.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    51.                 }
    52.             }
    53.  
    54.             if (Input.GetKeyDown(KeyCode.Tab))
    55.             {
    56.                 isPause = true;
    57.             }
    58.             if (Input.GetKeyUp(KeyCode.Tab))
    59.             {
    60.                 isPause = false;
    61.             }
    62.  
    63.             // We apply gravity manually for more tuning control
    64.             rigidbody.AddForce(new Vector3(0, -gravity * rigidbody.mass, 0));
    65.  
    66.             grounded = false;
    67.  
    68.             if (health <= 0)
    69.             {
    70.                 GetComponent<PhotonView>().RPC("DIE", PhotonTargets.AllBuffered, null);
    71.             }
    72.         }
    73.  
    74.         void OnCollisionStay()
    75.         {
    76.             grounded = true;
    77.         }
    78.  
    79.         float CalculateJumpVerticalSpeed()
    80.         {
    81.             // From the jump height and gravity we deduce the upwards speed
    82.             // for the character to reach at the apex.
    83.             return Mathf.Sqrt(2 * jumpHeight * gravity);
    84.         }
    85.  
    86.         private void OnGUI()
    87.         {
    88.             GUI.Box(new Rect(10, 10, 100, 30), "HP | " + health);
    89.  
    90.             if (isPause)
    91.             {
    92.                 GUILayout.BeginArea(new Rect(Screen.width / 2 - 250, Screen.height / 2 - 250, 500, 500));
    93.                 foreach (PhotonPlayer pl in PhotonNetwork.playerList)
    94.                 {
    95.                     GUILayout.Box(pl.name + " | " + pl.GetScore());
    96.                 }
    97.                 GUILayout.EndArea();
    98.             }
    99.         }
    100.  
    101.         [PunRPC]
    102.         public void applyDamage(int dmg)
    103.         {
    104.             health = health - dmg;
    105.             Debug.Log("hit!" + health);
    106.         }
    107.  
    108.         [PunRPC]
    109.         public void DIE()
    110.         {
    111.             if(GetComponent<PhotonView> ().isMine)
    112.             {
    113.                 Destroy(me);
    114.                 Destroy(ragDoll, 3);
    115.                 GameObject rDoll = PhotonNetwork.Instantiate(ragDoll.name, transform.position, transform.rotation, 0);
    116.                 PhotonNetwork.Destroy(me);
    117.                 GameObject.Find("_ROOM").GetComponent<RoomManager>().OnJoinedRoom();
    118.             }
    119.         }
    120.  
    121.         public void getClip (AudioClip soundToRecieve)
    122.         {
    123.             soundClip = soundToRecieve;
    124.         }
    125.  
    126.         public AudioClip[] sounds;
    127.  
    128.         [PunRPC]
    129.         public void playSound()
    130.         {
    131.             sound1.PlayOneShot (soundClip);
    132.         }
    133.     }
    And here is my playername Script
    Code (CSharp):
    1. using UnityEngine;
    2.     using UnityEngine.UI;
    3.  
    4.     public class playerName : MonoBehaviour
    5.     {
    6.         public Text nameTag;
    7.  
    8.         [PunRPC]
    9.         public void updateName(string name)
    10.         {
    11.             nameTag.text = name;
    12.             Debug.Log(nameTag.text);
    13.         }
    14.     }