Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. using UnityEngine.SceneManagement;
  7.  
  8. public class UINetwork : MonoBehaviour
  9. {
  10.     GameObject panelKoneksi;
  11.     Button btnHost;
  12.     Button btnJoin;
  13.     Button btnCancel;
  14.     Text txInfo;
  15.     NetworkManager network;
  16.     int status = 0;
  17.     public bool isEscapeToExit;
  18.  
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.         panelKoneksi = GameObject.Find("PanelKoneksi");
  23.         panelKoneksi.transform.localPosition = Vector3.zero;
  24.         btnHost = GameObject.Find("BtnHost").GetComponent<Button>();
  25.         btnJoin = GameObject.Find("BtnJoin").GetComponent<Button>();
  26.         btnCancel = GameObject.Find("BtnCancel").GetComponent<Button>();
  27.         txInfo = GameObject.Find("Info").GetComponent<Text>();
  28.         btnHost.onClick.AddListener(StartHostGame);
  29.         btnJoin.onClick.AddListener(StartJoinGame);
  30.         btnCancel.onClick.AddListener(CancelConection);
  31.         btnCancel.interactable= false;
  32.         network = GameObject.Find("Network Manager").GetComponent<NetworkManager>();
  33.         txInfo.text = "Info: Server Address" + network.networkAddress + "with port" + network.networkPort;
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {
  39.         if (NetworkClient.active || NetworkServer.active)
  40.         {
  41.             btnHost.interactable = false;
  42.             btnJoin.interactable = false;
  43.             btnCancel.interactable = true;
  44.         }
  45.         else
  46.         {
  47.             btnHost.interactable = true;
  48.             btnJoin.interactable = true;
  49.             btnCancel.interactable = false;
  50.         }
  51.         if (NetworkServer.connections.Count == 2 && status == 0)
  52.         {
  53.             status = 1;
  54.             MulaiPermainan();
  55.         }
  56.         if (ClientScene.ready && !NetworkServer.active && status == 0)
  57.         {
  58.             status = 1;
  59.             MulaiPermainan();
  60.         }
  61.  
  62.         if (Input.GetKeyUp(KeyCode.Escape))
  63.         {
  64.             if (isEscapeToExit)
  65.             {
  66.                 Application.Quit();
  67.             }
  68.             else
  69.             {
  70.                 KembaliKeMenu();
  71.             }
  72.         }
  73.     }
  74.  
  75.     private void StartHostGame()
  76.     {
  77.         Debug.Log("Dijalankan ketika Host ditekan");
  78.         if (!NetworkServer.active)
  79.         {
  80.             network.StartHost();
  81.         }
  82.         if (NetworkServer.active) txInfo.text = "Info: Menunggu Player lain (Jika Server Aktif)";
  83.     }
  84.  
  85.     private void StartJoinGame()
  86.     {
  87.         Debug.Log("Dijalankan ketika tombol Join Ditekan");
  88.         if (!NetworkClient.active)
  89.         {
  90.             network.StartClient();
  91.             network.client.RegisterHandler(MsgType.Disconnect, ConnectionError);
  92.         }
  93.         if (NetworkClient.active) txInfo.text = "Info: Mencoba mengkoneksikan dengan server";
  94.     }
  95.  
  96.     private void CancelConection()
  97.     {
  98.         Debug.Log("Dijalankan ketika Cancel Ditekan");
  99.         network.StopHost();
  100.         btnHost.interactable = true;
  101.         btnJoin.interactable = true;
  102.         btnCancel.interactable = false;
  103.         txInfo.text = "Info: Server Addres" + network.networkAddress + "with port" + network.networkPort;
  104.     }
  105.    
  106.     private void ConnectionError(NetworkMessage netMsg)
  107.     {
  108.         //network.StopClient();
  109.         //txInfo.text = "Info: Koneksi terputus dari Server";
  110.         KembaliKeMain();
  111.     }
  112.  
  113.     private void MulaiPermainan()
  114.     {
  115.         panelKoneksi.transform.localPosition = new Vector3(-1500, 0, 0);
  116.     }
  117.  
  118.     public void KembaliKeMain()
  119.     {
  120.         network.StopHost();
  121.         SceneManager.LoadScene("Main");
  122.     }
  123.  
  124.     public void KembaliKeMenu()
  125.     {
  126.         network.StopHost();
  127.         SceneManager.LoadScene("Menu");
  128.     }
  129.  
  130.     public void KeluarScene() {
  131.         Application.Quit();
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement