暫無描述
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.

ScreenSpacePlacement.cs 1.0KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteAlways]
  5. public class ScreenSpacePlacement : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private Camera m_Cam;
  9. [SerializeField]
  10. private Transform m_FlareObject;
  11. private bool m_MouseDown;
  12. void OnGUI()
  13. {
  14. Event currentEvent = Event.current;
  15. Vector2 mousePos = new Vector2();
  16. if (currentEvent.type == EventType.MouseDown) m_MouseDown = true;
  17. if (currentEvent.type == EventType.MouseUp) m_MouseDown = false;
  18. mousePos.x = currentEvent.mousePosition.x;
  19. mousePos.y = m_Cam.pixelHeight - currentEvent.mousePosition.y;
  20. if (m_FlareObject != null && mousePos.x > 0 && mousePos.y > 0 && mousePos.x < m_Cam.pixelWidth && mousePos.y < m_Cam.pixelHeight)
  21. {
  22. Vector3 point = m_Cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, m_Cam.nearClipPlane));
  23. if (m_MouseDown)
  24. {
  25. m_FlareObject.position = point;
  26. }
  27. }
  28. }
  29. }