Création d'un petit party-game anonyme de Build + Bagar sous Unity
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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. }