Création d'un petit party-game anonyme de Build + Bagar sous Unity
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.

BlockSpawner.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4. public class BlockSpawner : MonoBehaviour
  5. {
  6. public Inventory inventory;
  7. public float spawnDistance = 3f;
  8. #pragma warning disable 0649
  9. [SerializeField] private UI_Inventory uiInventory;
  10. #pragma warning restore 0649
  11. private GameObject _blockInstance;
  12. private bool _isPreview;
  13. public void Awake()
  14. {
  15. _isPreview = false;
  16. inventory = new Inventory();
  17. uiInventory.SetInventory(inventory);
  18. }
  19. public void OnPlaceBlock ()
  20. {
  21. if (!_isPreview) return;
  22. // todo remove in inventory
  23. _blockInstance.transform.SetParent(null);
  24. _blockInstance = null;
  25. DisablePreviewMode();
  26. }
  27. public void OnTogglePreviewMode(InputValue value)
  28. {
  29. float valueAsFloat = value.Get<float>();
  30. if (valueAsFloat == 1f) EnablePreviewMode();
  31. else DisablePreviewMode();
  32. }
  33. public void OnScroll(InputValue value)
  34. {
  35. DisablePreviewMode();
  36. float valueAsFloat = value.Get<float>();
  37. if (valueAsFloat > 0) inventory.SelectPreviousItem();
  38. if (valueAsFloat < 0) inventory.SelectNextItem();
  39. }
  40. public void EnablePreviewMode()
  41. {
  42. Item selectedItem = inventory.GetSelectedItem();
  43. if (selectedItem == null) return;
  44. Vector3 spawnPos = Vector3.forward * spawnDistance;
  45. _blockInstance = Instantiate(selectedItem.Prefab, spawnPos, Quaternion.identity);
  46. _blockInstance.transform.SetParent(this.transform, false);
  47. _isPreview = true;
  48. }
  49. public void DisablePreviewMode()
  50. {
  51. if (_blockInstance != null) Destroy(_blockInstance);
  52. _isPreview = false;
  53. }
  54. }