Equipment.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Animancer // https://kybernetik.com.au/animancer // Copyright 2022 Kybernetik //
  2. #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value.
  3. using UnityEngine;
  4. namespace Animancer.Examples.StateMachines
  5. {
  6. /// <summary>Manages the items equipped by a <see cref="Character"/>.</summary>
  7. /// <example><see href="https://kybernetik.com.au/animancer/docs/examples/fsm/weapons">Weapons</see></example>
  8. /// https://kybernetik.com.au/animancer/api/Animancer.Examples.StateMachines/Equipment
  9. ///
  10. [AddComponentMenu(Strings.ExamplesMenuPrefix + "Weapons - Equipment")]
  11. [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(StateMachines) + "/" + nameof(Equipment))]
  12. public sealed class Equipment : MonoBehaviour
  13. {
  14. /************************************************************************************************************************/
  15. [SerializeField] private Transform _WeaponHolder;
  16. [SerializeField] private Weapon _Weapon;
  17. /************************************************************************************************************************/
  18. public Weapon Weapon
  19. {
  20. get => _Weapon;
  21. set
  22. {
  23. DetachWeapon();
  24. _Weapon = value;
  25. AttachWeapon();
  26. }
  27. }
  28. /************************************************************************************************************************/
  29. private void Awake()
  30. {
  31. AttachWeapon();
  32. }
  33. /************************************************************************************************************************/
  34. private void AttachWeapon()
  35. {
  36. if (_Weapon == null)
  37. return;
  38. var transform = _Weapon.transform;
  39. transform.parent = _WeaponHolder;
  40. transform.localPosition = Vector3.zero;
  41. transform.localRotation = Quaternion.identity;
  42. transform.localScale = Vector3.one;
  43. _Weapon.gameObject.SetActive(true);
  44. }
  45. /************************************************************************************************************************/
  46. private void DetachWeapon()
  47. {
  48. if (_Weapon == null)
  49. return;
  50. _Weapon.transform.parent = transform;
  51. _Weapon.gameObject.SetActive(false);
  52. }
  53. /************************************************************************************************************************/
  54. }
  55. }