Make PlayerManager a singleton; update to editor version 6000.0.33f1
This commit is contained in:
@@ -17,7 +17,7 @@ public class PlatformerCameraMovement : MonoBehaviour
|
|||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
if (players.Count == 0) return;
|
if (PlayerManager.Instance.players.Count == 0) return;
|
||||||
|
|
||||||
Vector3 playerAverage = Vector3.zero;
|
Vector3 playerAverage = Vector3.zero;
|
||||||
foreach (GameObject player in players)
|
foreach (GameObject player in players)
|
||||||
|
|||||||
@@ -1,12 +1,21 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.InputSystem;
|
using UnityEngine.InputSystem;
|
||||||
|
|
||||||
public class PlayerManager : MonoBehaviour
|
public class PlayerManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
public static PlayerManager Instance;
|
||||||
|
|
||||||
|
public List<GameObject> players;
|
||||||
[SerializeField] private Vector2 spawnPosition;
|
[SerializeField] private Vector2 spawnPosition;
|
||||||
|
|
||||||
private PlatformerCameraMovement playerCamera;
|
private PlatformerCameraMovement playerCamera;
|
||||||
|
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
GetComponent<PlayerInputManager>().onPlayerJoined += OnPlayerJoined;
|
GetComponent<PlayerInputManager>().onPlayerJoined += OnPlayerJoined;
|
||||||
@@ -18,14 +27,26 @@ public class PlayerManager : MonoBehaviour
|
|||||||
private void OnPlayerJoined(PlayerInput playerInput)
|
private void OnPlayerJoined(PlayerInput playerInput)
|
||||||
{
|
{
|
||||||
playerInput.transform.position = spawnPosition;
|
playerInput.transform.position = spawnPosition;
|
||||||
playerCamera.players.Add(playerInput.gameObject);
|
players.Add(playerInput.gameObject);
|
||||||
print("Player joined");
|
print("Player joined");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPlayerLeft(PlayerInput playerInput)
|
private void OnPlayerLeft(PlayerInput playerInput)
|
||||||
{
|
{
|
||||||
Destroy(playerInput.gameObject);
|
Destroy(playerInput.gameObject);
|
||||||
playerCamera.players.Remove(playerInput.gameObject);
|
players.Remove(playerInput.gameObject);
|
||||||
print("Player left");
|
print("Player left");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Init()
|
||||||
|
{
|
||||||
|
if (Instance == null)
|
||||||
|
{
|
||||||
|
Instance = this;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print("A PlayerManager already exists.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
m_EditorVersion: 6000.0.32f1
|
m_EditorVersion: 6000.0.33f1
|
||||||
m_EditorVersionWithRevision: 6000.0.32f1 (b2e806cf271c)
|
m_EditorVersionWithRevision: 6000.0.33f1 (433b0a79340b)
|
||||||
|
|||||||
Reference in New Issue
Block a user