SHOW:
|
|
- or go back to the newest paste.
1 | using System.Collections; | |
2 | using System.Collections.Generic; | |
3 | using UnityEngine; | |
4 | ||
5 | using Photon.Pun; | |
6 | using TMPro; | |
7 | using Photon.Realtime; | |
8 | ||
9 | public class GameLauncher : MonoBehaviourPunCallbacks | |
10 | { | |
11 | byte maxPlayerPerRoom = 2; //liczba graczy w pokoju | |
12 | bool isConnecting; //czy istnieje połączenie | |
13 | public TMP_Text networkText; //nasz network text | |
14 | string gameVersion = "0.1"; //wersja gry | |
15 | ||
16 | void Awake() | |
17 | { | |
18 | //właczamy synchronizację | |
19 | PhotonNetwork.AutomaticallySyncScene = true; | |
20 | } | |
21 | ||
22 | ||
23 | ||
24 | public override void OnConnectedToMaster() | |
25 | { | |
26 | if(isConnecting) | |
27 | { | |
28 | networkText.text += "OnConnectToMaster...\n"; | |
29 | PhotonNetwork.JoinRandomRoom(); | |
30 | } | |
31 | } | |
32 | ||
33 | //Wywołanie przy rozłączeniu | |
34 | public override void OnDisconnected(DisconnectCause cause) | |
35 | { | |
36 | networkText.text += "Disconnected because " + cause + "\n"; | |
37 | isConnecting = false; | |
38 | } | |
39 | ||
40 | //Wywołanie przy dołączeniu do pokoju | |
41 | public override void OnJoinedRoom() | |
42 | { | |
43 | networkText.text = "Joined Room with " + PhotonNetwork.CurrentRoom.PlayerCount + "players.\n"; | |
44 | //Uruchomienie sceny z grą | |
45 | PhotonNetwork.LoadLevel("PongOnline"); | |
46 | } | |
47 | ||
48 | //Wywołanie kiedy nie udało się dołączyć do żadnego pokoju | |
49 | public override void OnJoinRandomFailed(short returnCode, string message) | |
50 | { | |
51 | networkText.text += "Failed to join random room. \n"; | |
52 | //Stworzenie nowego pokoju | |
53 | PhotonNetwork.CreateRoom(null, new RoomOptions { MaxPlayers = this.maxPlayerPerRoom }); | |
54 | } | |
55 | ||
56 | //Połączenie z siecią | |
57 | public void ConnectNetwork() | |
58 | { | |
59 | networkText.text = ""; | |
60 | isConnecting = true; | |
61 | if (PhotonNetwork.IsConnected) | |
62 | { | |
63 | networkText.text += "Joining Room...\n"; | |
64 | PhotonNetwork.JoinRandomRoom(); | |
65 | } else | |
66 | { | |
67 | networkText.text += "Connecting...\n"; | |
68 | PhotonNetwork.GameVersion = gameVersion; | |
69 | PhotonNetwork.ConnectUsingSettings(); | |
70 | } | |
71 | } | |
72 | } | |
73 |