diff --git a/Gameton-06/Assets/Gameton/Scripts/Character/PlayerSpawner.cs b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerSpawner.cs new file mode 100644 index 00000000..a15372b9 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerSpawner.cs @@ -0,0 +1,51 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace TON +{ + public static class PlayerSpawner + { + + public static void SpawnPlayerCharacter() + { + List playerDatas = PlayerDataManager.Singleton.players; + // 저장된 인덱스 가져오기 + int selectedIndex = PlayerPrefs.GetInt("SelectedPlayerIndex", 0); + + // 인덱스가 범위를 벗어나지 않는지 확인 + if (selectedIndex < 0 || selectedIndex >= playerDatas.Count) + { + Debug.LogError($"Invalid player index: {selectedIndex}"); + return; + } + + string prefabName = playerDatas[selectedIndex].type == "b" ? "TON.Player_B" : "TON.Player_W"; + // Resources에서 프리팹 로드 + GameObject characterPrefab = Resources.Load($"Player/{prefabName}"); + if (characterPrefab == null) + { + Debug.LogError($"Failed to load character prefab: {playerDatas[selectedIndex].type}"); + return; + } + + // TON.Player 오브젝트 찾기 + GameObject playerObj = GameObject.Find("TON.Player"); + if (playerObj == null) + { + Debug.LogError("TON.Player not found in the scene!"); + return; + } + + // 기존 플레이어 제거 (필요 시) + foreach (Transform child in playerObj.transform) + { + GameObject.Destroy(child.gameObject); + } + + // 캐릭터 프리팹을 TON.Player 위치에 배치 + GameObject playerInstance = GameObject.Instantiate(characterPrefab, playerObj.transform.position, Quaternion.identity); + playerInstance.transform.SetParent(playerObj.transform); + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Character/PlayerSpawner.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerSpawner.cs.meta new file mode 100644 index 00000000..2b5b2c38 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Character/PlayerSpawner.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 955be0658135d1c49adc269ccc9ccfcf +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Scripts/Scenes/LobbyScene.cs b/Gameton-06/Assets/Gameton/Scripts/Scenes/LobbyScene.cs index b21ed645..6238461f 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Scenes/LobbyScene.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Scenes/LobbyScene.cs @@ -9,7 +9,6 @@ namespace TON { public SerializableDictionary CharacterSpriteDict = new SerializableDictionary(); - [SerializeField] private List playerDatas; public override IEnumerator OnStart() { @@ -22,49 +21,10 @@ namespace TON yield return null; } - SpawnPlayerCharacter(); + PlayerSpawner.SpawnPlayerCharacter(); } - private void SpawnPlayerCharacter() - { - playerDatas = PlayerDataManager.Singleton.players; - // 저장된 인덱스 가져오기 - int selectedIndex = PlayerPrefs.GetInt("SelectedPlayerIndex", 0); - // 인덱스가 범위를 벗어나지 않는지 확인 - if (selectedIndex < 0 || selectedIndex >= playerDatas.Count) - { - Debug.LogError($"Invalid player index: {selectedIndex}"); - return; - } - - string prefabName = playerDatas[selectedIndex].type == "b" ? "TON.Player_B" : "TON.Player_W"; - // Resources에서 프리팹 로드 - GameObject characterPrefab = Resources.Load($"Player/{prefabName}"); - if (characterPrefab == null) - { - Debug.LogError($"Failed to load character prefab: {playerDatas[selectedIndex].type}"); - return; - } - - // TON.Player 오브젝트 찾기 - GameObject playerObj = GameObject.Find("TON.Player"); - if (playerObj == null) - { - Debug.LogError("TON.Player not found in the scene!"); - return; - } - - // 기존 플레이어 제거 (필요 시) - foreach (Transform child in playerObj.transform) - { - GameObject.Destroy(child.gameObject); - } - - // 캐릭터 프리팹을 TON.Player 위치에 배치 - GameObject playerInstance = Instantiate(characterPrefab, playerObj.transform.position, Quaternion.identity); - playerInstance.transform.SetParent(playerObj.transform); - } public override IEnumerator OnEnd() {