DemoGUI.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityARInterface;
  5. public class DemoGUI : ARBase
  6. {
  7. public float guiHeight { get; private set; }
  8. [SerializeField]
  9. private GameObject m_LevelGeometry;
  10. [SerializeField]
  11. private GUISkin m_GuiSkin;
  12. private ObjectShooter m_ObjectShooter;
  13. private ARController m_ARController;
  14. private float m_RotationAngle;
  15. void OnEnable()
  16. {
  17. m_ObjectShooter = GetComponent<ObjectShooter>();
  18. m_ARController = GetFirstEnabledControllerInChildren();
  19. }
  20. void OnGUI()
  21. {
  22. if (m_ARController == null || !m_ARController.enabled)
  23. return;
  24. guiHeight = Screen.height / 5;
  25. var buttonWidth = Screen.width / 2;
  26. if (GUI.Button(new Rect(Screen.width - buttonWidth, Screen.height - guiHeight, buttonWidth, guiHeight), "Fire!", m_GuiSkin.button))
  27. m_ObjectShooter.RequestFire(new Vector2(Screen.width / 2, Screen.height / 2));
  28. var sliderWidth = Screen.width / 2;
  29. var sliderHeight = guiHeight / 2;
  30. var angle = GUI.HorizontalSlider(
  31. new Rect(0, Screen.height - sliderHeight * 2, sliderWidth, sliderHeight),
  32. m_RotationAngle, 0f, 360f,
  33. m_GuiSkin.horizontalSlider,
  34. m_GuiSkin.horizontalSliderThumb);
  35. if (angle != m_RotationAngle)
  36. {
  37. m_ARController.rotation = Quaternion.AngleAxis(m_RotationAngle, Vector3.up);
  38. m_RotationAngle = angle;
  39. }
  40. var scale = GUI.HorizontalSlider(
  41. new Rect(0, Screen.height - sliderHeight, sliderWidth, sliderHeight),
  42. m_ARController.scale, 1f, 100f,
  43. m_GuiSkin.horizontalSlider,
  44. m_GuiSkin.horizontalSliderThumb);
  45. if (scale != m_ARController.scale)
  46. m_ARController.scale = scale;
  47. }
  48. void Update()
  49. {
  50. if (Input.GetKeyUp(KeyCode.Space))
  51. m_ObjectShooter.RequestFire(Input.mousePosition);
  52. if (Input.GetMouseButton(0) && Input.mousePosition.y > guiHeight)
  53. {
  54. var camera = GetCamera();
  55. Ray ray = camera.ScreenPointToRay(Input.mousePosition);
  56. var planeLayer = GetComponent<ARPlaneVisualizer>().planeLayer;
  57. int layerMask = 1 << planeLayer;
  58. RaycastHit rayHit;
  59. if (Physics.Raycast(ray, out rayHit, float.MaxValue, layerMask))
  60. {
  61. m_ARController.pointOfInterest = m_LevelGeometry.transform.position;
  62. m_ARController.AlignWithPointOfInterest(rayHit.point);
  63. m_ObjectShooter.minimumYValue = rayHit.point.y;
  64. }
  65. }
  66. }
  67. }