Advertisement
MatiGe

GameManager

Aug 18th, 2024
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System.Collections.Generic;
  4.  
  5. public class GameManager : MonoBehaviour
  6. {
  7.     public static GameManager instance;
  8.  
  9.     public GameObject playerPrefab;
  10.     private GameObject currentPickedObject;
  11.     private Dictionary<string, GameObject> objectPrefabs = new Dictionary<string, GameObject>();
  12.     private Vector3 playerSpawnPosition;
  13.     private string pickedObjectName;
  14.     private Vector3 pickedObjectPosition;
  15.  
  16.     private void Awake()
  17.     {
  18.         if (instance == null)
  19.         {
  20.             instance = this;
  21.             DontDestroyOnLoad(gameObject);
  22.         }
  23.         else
  24.         {
  25.             Destroy(gameObject);
  26.         }
  27.     }
  28.  
  29.     public void RegisterObjectPrefab(string key, GameObject prefab)
  30.     {
  31.         if (!objectPrefabs.ContainsKey(key))
  32.         {
  33.             objectPrefabs.Add(key, prefab);
  34.             Debug.Log($"Registered prefab with key: {key}");
  35.         }
  36.     }
  37.  
  38.     public void SetPickedObject(GameObject pickedObject)
  39.     {
  40.         if (pickedObject == null)
  41.         {
  42.             currentPickedObject = null;
  43.             pickedObjectName = null;
  44.             return;
  45.         }
  46.  
  47.         string objectName = pickedObject.name.Replace("(Clone)", "").Trim();
  48.         Debug.Log($"Setting picked object: {objectName}");
  49.  
  50.         if (currentPickedObject != null)
  51.         {
  52.             Destroy(currentPickedObject);
  53.         }
  54.  
  55.         if (objectPrefabs.ContainsKey(objectName))
  56.         {
  57.             pickedObjectName = objectName;
  58.             pickedObjectPosition = pickedObject.transform.position;
  59.             currentPickedObject = pickedObject;
  60.             currentPickedObject.transform.SetParent(null);
  61.             DontDestroyOnLoad(currentPickedObject);
  62.         }
  63.         else
  64.         {
  65.             Debug.LogError($"Object prefab not found for {objectName}");
  66.             currentPickedObject = null;
  67.             pickedObjectName = null;
  68.         }
  69.     }
  70.  
  71.     public GameObject GetPickedObject()
  72.     {
  73.         return currentPickedObject;
  74.     }
  75.  
  76.     public void SetPlayerSpawnPosition(Vector3 position)
  77.     {
  78.         playerSpawnPosition = position;
  79.     }
  80.  
  81.     public void TransferPickedObject(Transform playerTransform)
  82.     {
  83.         if (currentPickedObject != null)
  84.         {
  85.             currentPickedObject.transform.SetParent(playerTransform);
  86.             currentPickedObject.transform.localPosition = Vector3.zero; // Adjust as needed
  87.         }
  88.     }
  89.  
  90.     public void SpawnPlayer()
  91.     {
  92.         GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
  93.         if (players.Length > 1)
  94.         {
  95.             Debug.LogWarning("Multiple players detected! Destroying extras...");
  96.             for (int i = 1; i < players.Length; i++)
  97.             {
  98.                 Destroy(players[i]);
  99.             }
  100.         }
  101.  
  102.         GameObject existingPlayer = players.Length > 0 ? players[0] : null;
  103.  
  104.         if (existingPlayer == null)
  105.         {
  106.             GameObject player = Instantiate(playerPrefab, playerSpawnPosition, Quaternion.identity);
  107.             DontDestroyOnLoad(player);
  108.             if (currentPickedObject != null)
  109.             {
  110.                 currentPickedObject.transform.SetParent(player.transform);
  111.                 currentPickedObject.transform.localPosition = Vector3.zero; // Adjust as needed
  112.             }
  113.         }
  114.         else
  115.         {
  116.             // Update player position if player already exists
  117.             existingPlayer.transform.position = playerSpawnPosition;
  118.             if (currentPickedObject != null)
  119.             {
  120.                 currentPickedObject.transform.SetParent(existingPlayer.transform);
  121.                 currentPickedObject.transform.localPosition = Vector3.zero; // Adjust as needed
  122.             }
  123.         }
  124.     }
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement