Advertisement
Dieton

GameManager V2

Jun 22nd, 2023 (edited)
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Netcode;
  5.  
  6. public class GameManager : NetworkBehaviour
  7. {
  8.     //---- Network ----//
  9.     [SerializeField]private NetworkManager networkManager;
  10.     [SerializeField]private NetworkPrefabsList networkPrefabsList;
  11.  
  12.     //---- Spawn System ----//
  13.     //0 = BoyTeacher, 1 = GirlTeacher, 2 = BoyStudent, 3 = GirlStudent
  14.     private int playerTypeId;
  15.     [SerializeField] private Transform[] teacherSpawnPosition;
  16.     [SerializeField] private Transform[] studentSpawnPosition;
  17.  
  18.     //---- Debug ----//
  19.     [SerializeField]private bool debugIO = false;
  20.  
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         //GetComponent<NetworkObject>().Spawn();
  25.     }
  26.  
  27.     //==== On Network Spawn ====//
  28.     public override void OnNetworkSpawn()
  29.     {
  30.         GmDebugger("On Network Spawn");
  31.  
  32.         if (IsHost || IsClient) SpawnPlayerServerRpc();
  33.     }
  34.  
  35.     //==== Start Host Button
  36.     public void StartHostButton(int _playerTypeId)
  37.     {
  38.         GmDebugger("Host Started");
  39.  
  40.         playerTypeId = _playerTypeId;
  41.         networkManager.StartHost();
  42.         //this.GetComponent<NetworkObject>().Spawn();
  43.     }
  44.  
  45.     //==== Start Client Button
  46.     public void StartClientButton(int _playerTypeId)
  47.     {
  48.         GmDebugger("Client Started");
  49.  
  50.         playerTypeId = _playerTypeId;
  51.         networkManager.StartClient();
  52.         //this.GetComponent<NetworkObject>().Spawn();
  53.     }
  54.  
  55.     //==== Start Server Button
  56.     public void StartServerButton(int _playerTypeId)
  57.     {
  58.         GmDebugger("Server Started");
  59.  
  60.         playerTypeId = _playerTypeId;
  61.         networkManager.StartServer();
  62.     }
  63.  
  64.     //==== player Spawn System ====//
  65.     [ServerRpc(RequireOwnership = false)]
  66.     public void SpawnPlayerServerRpc() {
  67.         Debug.Log(networkPrefabsList.PrefabList[playerTypeId].Prefab);
  68.         GameObject NetPlayerPrefabClone = Instantiate(networkPrefabsList.PrefabList[playerTypeId].Prefab);
  69.  
  70.         if(playerTypeId < 2) NetPlayerPrefabClone.transform.position = teacherSpawnPosition[0].position;
  71.         if(playerTypeId > 1) NetPlayerPrefabClone.transform.position = studentSpawnPosition[0].position;
  72.  
  73.         NetPlayerPrefabClone.GetComponent<NetworkObject>().Spawn();
  74.  
  75.         //networkManager.AddNetworkPrefab(networkPrefabsList.PrefabList[playerTypeId.Value].Prefab);
  76.     }
  77.  
  78.     //==== Game Manager Debugger ====//
  79.     public void GmDebugger(object debugObj) {
  80.         if (debugIO) Debug.Log(debugObj);
  81.     }
  82. }
  83.  
Tags: Unity Netcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement