2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.5 KiB

5 years ago
  1. using System.Collections.Generic;
  2. using Unity.FPS.Game;
  3. using Unity.FPS.Gameplay;
  4. using UnityEngine;
  5. namespace Unity.FPS.UI
  6. {
  7. public class WeaponHUDManager : MonoBehaviour
  8. {
  9. [Tooltip("UI panel containing the layoutGroup for displaying weapon ammos")]
  10. public RectTransform AmmosPanel;
  11. [Tooltip("Prefab for displaying weapon ammo")]
  12. public GameObject AmmoCounterPrefab;
  13. PlayerWeaponsManager m_PlayerWeaponsManager;
  14. List<AmmoCounter> m_AmmoCounters = new List<AmmoCounter>();
  15. void Start()
  16. {
  17. m_PlayerWeaponsManager = FindObjectOfType<PlayerWeaponsManager>();
  18. DebugUtility.HandleErrorIfNullFindObject<PlayerWeaponsManager, WeaponHUDManager>(m_PlayerWeaponsManager,
  19. this);
  20. WeaponController activeWeapon = m_PlayerWeaponsManager.GetActiveWeapon();
  21. if (activeWeapon)
  22. {
  23. AddWeapon(activeWeapon, m_PlayerWeaponsManager.ActiveWeaponIndex);
  24. ChangeWeapon(activeWeapon);
  25. }
  26. m_PlayerWeaponsManager.OnAddedWeapon += AddWeapon;
  27. m_PlayerWeaponsManager.OnRemovedWeapon += RemoveWeapon;
  28. m_PlayerWeaponsManager.OnSwitchedToWeapon += ChangeWeapon;
  29. }
  30. void AddWeapon(WeaponController newWeapon, int weaponIndex)
  31. {
  32. GameObject ammoCounterInstance = Instantiate(AmmoCounterPrefab, AmmosPanel);
  33. AmmoCounter newAmmoCounter = ammoCounterInstance.GetComponent<AmmoCounter>();
  34. DebugUtility.HandleErrorIfNullGetComponent<AmmoCounter, WeaponHUDManager>(newAmmoCounter, this,
  35. ammoCounterInstance.gameObject);
  36. newAmmoCounter.Initialize(newWeapon, weaponIndex);
  37. m_AmmoCounters.Add(newAmmoCounter);
  38. }
  39. void RemoveWeapon(WeaponController newWeapon, int weaponIndex)
  40. {
  41. int foundCounterIndex = -1;
  42. for (int i = 0; i < m_AmmoCounters.Count; i++)
  43. {
  44. if (m_AmmoCounters[i].WeaponCounterIndex == weaponIndex)
  45. {
  46. foundCounterIndex = i;
  47. Destroy(m_AmmoCounters[i].gameObject);
  48. }
  49. }
  50. if (foundCounterIndex >= 0)
  51. {
  52. m_AmmoCounters.RemoveAt(foundCounterIndex);
  53. }
  54. }
  55. void ChangeWeapon(WeaponController weapon)
  56. {
  57. UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(AmmosPanel);
  58. }
  59. }
  60. }