Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using System.Collections;
- using UnityEngine.UI;
- using Unity.Netcode;
- using UnityEngine;
- using TMPro;
- //using System;
- public class GameManager : NetworkBehaviour
- {
- public static GameManager gameManager;
- public float spawnTime = 10.5f;
- [SerializeField]
- private GameObject
- gameItemPrefab,
- itemPrefab,
- cubePrefab;
- [SerializeField]
- private Transform prefabSpawnpoint;
- public List<GameObject> customePlayers;
- [SerializeField]
- private NetworkManager networkManager;
- public int selectedPlayerID = 0;
- public NetworkPrefabsList plrefablist;
- //public NetworkPrefab netPrefab;
- List<GameObject> playerClones = new List<GameObject>();
- GameObject playerClone;
- // Start is called before the first frame update
- void Start()
- {
- gameManager = this;
- if (IsServer)
- {
- StartServerActions();
- }
- }
- #region ServerSide
- public void StartServerActions()
- {
- //MyDebugger.myDebugger.Log("checking to see if this is a server.");
- if (IsHost || IsServer)
- {
- StartCoroutine(TimedAction());
- }
- }
- IEnumerator TimedAction() {
- while (true)
- {
- yield return new WaitForSeconds(spawnTime);
- Color colorA = Random.value > 0.5f ? Color.green : Color.yellow;
- Color colorB = Random.value > 0.5f ? Color.red : Color.blue;
- Color colorC = Random.value > 0.5f ? Color.red : Color.blue;
- Color colorD = Random.value > 0.5f ? Color.red : Color.blue;
- SpawnSomethingClientRpc
- (
- colorA,
- colorB,
- colorC,
- colorD
- );
- }
- }
- [ClientRpc]
- private void SpawnSomethingClientRpc(
- Color colorOne,
- Color colorTwo,
- Color colorThree,
- Color colorFour
- )
- {
- Color[] myColors = new Color[] { colorOne , colorTwo, colorThree, colorFour };
- GameObject gameItemClone = Instantiate(gameItemPrefab, prefabSpawnpoint.position, prefabSpawnpoint.rotation);
- GameItem gameItem = gameItemClone.GetComponent<GameItem>();
- for (int i = 0; i < 4; i++)
- {
- GameObject itemClone = Instantiate(itemPrefab, gameItem.itemSpawnpoints[i].position, gameItem.itemSpawnpoints[i].rotation);
- itemClone.transform.SetParent(gameItemClone.transform);
- itemClone.GetComponent<Renderer>().material.color = myColors[i];
- gameItemClone.GetComponent<Rigidbody>().isKinematic = false;
- }
- //NetworkObject.Despawn();
- //Destroy(gameItemClone.gameObject, 4.5f);
- StartCoroutine(DespawnTime(4.5f, gameItemClone.GetComponent<NetworkObject>()));
- }
- IEnumerator DespawnTime(float waitTime, NetworkObject netObj) {
- yield return new WaitForSeconds(waitTime);
- netObj.Despawn();
- }
- #endregion
- #region ServerToClient
- public void PublicColor(int colorId) {
- ColorPickServerRpc(colorId);
- }
- [ServerRpc(RequireOwnership = false)]//I dont think i need to request onership in this situation but i did so i can see how to use it if i need it in the future
- private void ColorPickServerRpc(int colorId)
- {
- ColorPickClientRpc(colorId);
- }
- [ClientRpc]
- private void ColorPickClientRpc(int colorId)
- {
- GameObject cubePrefabClone = Instantiate(cubePrefab);
- if (colorId == 0)
- {
- cubePrefabClone.GetComponent<Renderer>().material.color = Color.red;
- }
- if (colorId == 1)
- {
- cubePrefabClone.GetComponent<Renderer>().material.color = Color.green;
- }
- if (colorId == 2)
- {
- cubePrefabClone.GetComponent<Renderer>().material.color = Color.blue;
- }
- if (colorId == 3)
- {
- cubePrefabClone.GetComponent<Renderer>().material.color = Color.yellow;
- }
- }
- #endregion
- // Update is called once per frame
- void Update()
- {
- }
- public void StartHostButton(int _playerId)
- {
- selectedPlayerID = _playerId;
- //Instantiate(customePlayers[0]);
- MyDebugger.myDebugger.Log("Host Started");
- //SetupPlayerServerRpc();
- //networkManager.AddNetworkPrefab
- //networkManager.SpawnManager.GetPlayerNetworkObject(0);
- //networkManager.SpawnManager//(networkManager.SpawnManager.SpawnedObjectsList,Vector3.zero,Quaternion.identity);
- NetworkManager.Singleton.StartHost();
- StartServerActions();
- SetupPlayer();
- }
- [ContextMenu("hat")]//can be run from eidter
- public void StartClientButton(int _playerId)
- {
- selectedPlayerID = _playerId;
- //Instantiate(customePlayers[1]);
- MyDebugger.myDebugger.Log("Client Started");
- //SetupPlayerServerRpc();
- NetworkManager.Singleton.StartClient();
- SetupPlayer();
- }
- public void StartServerButton()
- {
- MyDebugger.myDebugger.Log("Server Started");
- NetworkManager.Singleton.StartServer();
- StartServerActions();
- }
- public void SetupPlayer() {
- MyDebugger.myDebugger.Log("SetupPlayer");
- //,customePlayers[0].GetComponent<NetPlayer>().GetNetwokObject());
- //SetupPlayerServerRpc();
- }
- //public override on
- public override void OnNetworkSpawn()
- {
- MyDebugger.myDebugger.Log("Setup Player Server Rpc");
- if (IsServer)
- {
- MyDebugger.myDebugger.Log("On Network Spawn - Is Server");
- //return;
- }
- if (IsClient) {
- MyDebugger.myDebugger.Log("On Network Spawn - Is Client");
- }
- if (IsHost) {
- MyDebugger.myDebugger.Log("On Network Spawn - Is Host");
- }
- MyDebugger.myDebugger.Log($"Prefab List " + plrefablist);
- for (int i = 0; i < plrefablist.PrefabList.Count; i++)
- {
- if (i == selectedPlayerID)
- {
- playerClone = Instantiate(plrefablist.PrefabList[i].Prefab);
- playerClones.Add(playerClone);
- MyDebugger.myDebugger.Log($"Prefab Name " + plrefablist.PrefabList[i].Prefab);
- }
- }
- SetupPlayerServerRpc();
- }
- [ServerRpc(RequireOwnership = false)]
- private void SetupPlayerServerRpc()
- {
- MyDebugger.myDebugger.Log($"Setup PlayerServer Rpc");
- foreach (GameObject playerClone in playerClones)
- {
- NetworkObject playerNetObj = playerClone.GetComponent<NetworkObject>();
- playerNetObj.Spawn();
- //playerNetObjRef.TryGet(out NetworkObject playerNetObj);
- }
- }
- public void StopServer() {
- if (IsServer)
- {
- networkManager.Shutdown();
- }
- }
- }
Advertisement
Comments
-
- It probably doesn't work, although who knows maby one day ill figure this stuff out.
Add Comment
Please, Sign In to add comment
Advertisement