Création d'un petit party-game anonyme de Build + Bagar sous Unity
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

BlockSpawner.cs 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class BlockSpawner : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private GameObject _blockToPlace;
  9. public GameObject BlockToPlace
  10. {
  11. get
  12. {
  13. return _blockToPlace;
  14. }
  15. set
  16. {
  17. DisablePreviewMode();
  18. _blockToPlace = value;
  19. }
  20. }
  21. public float spawnDistance = 3f;
  22. private GameObject _blockInstance;
  23. private bool _isPreview = false;
  24. public void OnPlaceBlock()
  25. {
  26. if (!_isPreview) return;
  27. _blockInstance.transform.SetParent(null);
  28. _blockInstance = null;
  29. DisablePreviewMode();
  30. }
  31. public void OnTogglePreviewMode(InputValue value)
  32. {
  33. float valueAsFloat = value.Get<float>();
  34. if (valueAsFloat == 1f) EnablePreviewMode();
  35. else DisablePreviewMode();
  36. }
  37. private void EnablePreviewMode()
  38. {
  39. if (_blockToPlace == null) return;
  40. Vector3 spawnPos = Vector3.forward * spawnDistance;
  41. _blockInstance = Instantiate(_blockToPlace, spawnPos, Quaternion.identity);
  42. _blockInstance.transform.SetParent(this.transform, false);
  43. _isPreview = true;
  44. }
  45. private void DisablePreviewMode()
  46. {
  47. if (_blockInstance != null) Destroy(_blockInstance);
  48. _isPreview = false;
  49. }
  50. }