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

BlockSpawner.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class BlockSpawner : MonoBehaviour
  6. {
  7. <<<<<<< HEAD
  8. =======
  9. private void Start()
  10. {
  11. _playerInput = GetComponent<PlayerInput>();
  12. }
  13. >>>>>>> shader
  14. [SerializeField]
  15. private GameObject _blockToPlace;
  16. public GameObject BlockToPlace
  17. {
  18. get
  19. {
  20. return _blockToPlace;
  21. }
  22. set
  23. {
  24. DisablePreviewMode();
  25. _blockToPlace = value;
  26. }
  27. }
  28. PlayerInput _playerInput;
  29. public float spawnDistance = 3f;
  30. public Shader previewShader;
  31. private GameObject _blockInstance;
  32. private bool _isPreview = false;
  33. private Shader _initialShader;
  34. public bool _blockRotating = false;
  35. private Quaternion _lastRotationState;
  36. private Shader _initialShader;
  37. public void OnPlaceBlock()
  38. {
  39. if (!_isPreview) return;
  40. _blockInstance.transform.SetParent(null);
  41. SwitchToDefaultRender();
  42. _blockInstance = null;
  43. DisablePreviewMode();
  44. }
  45. public void OnTogglePreviewMode(InputValue value)
  46. {
  47. float valueAsFloat = value.Get<float>();
  48. if (valueAsFloat == 1f)
  49. {
  50. EnablePreviewMode();
  51. }
  52. else
  53. {
  54. DisablePreviewMode();
  55. }
  56. }
  57. public void OnEnterRotateBlock(InputValue value)
  58. {
  59. float valueAsFloat = value.Get<float>();
  60. Debug.Log(valueAsFloat);
  61. _blockRotating = true;
  62. }
  63. public void OnExitRotateBlock(InputValue value)
  64. {
  65. float valueAsFloat = value.Get<float>();
  66. Debug.Log(valueAsFloat);
  67. _blockRotating = false;
  68. }
  69. public void OnRotateBlock(InputValue value)
  70. {
  71. if (_isPreview && _blockRotating)
  72. {
  73. Vector2 iMouseMovement = value.Get<Vector2>();
  74. Transform vTransform = _blockInstance.transform;
  75. //vTransform.Rotate(iMouseMovement.y / 10, iMouseMovement.x / 10, 0);
  76. vTransform.RotateAround(vTransform.position, transform.right, iMouseMovement.y / 10.0f);
  77. vTransform.RotateAround(vTransform.position, transform.up, iMouseMovement.x / 10.0f);
  78. //vTransform.eulerAngles += vTransform.worldToLocalMatrix.MultiplyVector(new Vector3(iMouseMovement.y / 10, iMouseMovement.x / 10, 0));
  79. //vTransform.Rotate(iMouseMovement.y/10, iMouseMovement.x/10, 0);
  80. _lastRotationState = vTransform.rotation;
  81. }
  82. }
  83. private void EnablePreviewMode()
  84. {
  85. if (_blockToPlace == null) return;
  86. Vector3 spawnPos = Vector3.forward * spawnDistance;
  87. _blockInstance = Instantiate(_blockToPlace, spawnPos, Quaternion.identity);
  88. _blockInstance.transform.SetParent(this.transform, false);
  89. _blockInstance.transform.rotation = _lastRotationState;
  90. _isPreview = true;
  91. SwitchToMeshRender();
  92. }
  93. private void DisablePreviewMode()
  94. {
  95. if (_blockInstance != null) Destroy(_blockInstance);
  96. _isPreview = false;
  97. }
  98. private void SwitchToMeshRender()
  99. {
  100. MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
  101. Material vMaterial = vRenderer.material;
  102. _initialShader = vMaterial.shader;
  103. vMaterial.shader = previewShader;
  104. }
  105. private void SwitchToDefaultRender()
  106. {
  107. MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
  108. Material vMaterial = vRenderer.material;
  109. vMaterial.shader = _initialShader;
  110. }
  111. <<<<<<< HEAD
  112. =======
  113. >>>>>>> shader
  114. }