ZyeTest.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using ShotSimulator.Tool;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. public class ZyeTest : MonoBehaviour
  7. {
  8. public GameObject obj_1;
  9. public GameObject obj_2;
  10. public GameObject obj_3;
  11. public GameObject obj_4;
  12. public GameObject obj_5;
  13. public GameObject obj_6;
  14. public Camera snipeCamera;
  15. public Image snipeImage;
  16. public RectTransform rect;
  17. private Ray ray;
  18. List<MonoTest> list = new List<MonoTest>();
  19. private void Awake()
  20. {
  21. Debug.Log(Screen.width + ":" + Screen.height);
  22. //rect = snipeImage.transform.parent.GetComponent<RectTransform>();
  23. //obj_1.transform.position = ExtraTool.GetWorldPositionWithResolutionPercent(0.4f, 0.75f, 10);
  24. //obj_2.transform.position = ExtraTool.GetWorldPositionWithResolutionPercent(0.3f, 0.5f, 10);
  25. //obj_3.transform.position = ExtraTool.GetWorldPositionWithResolutionPercent(0.4f, 0.25f, 10);
  26. //obj_4.transform.position = ExtraTool.GetWorldPositionWithResolutionPercent(0.6f, 0.75f, 10);
  27. //obj_5.transform.position = ExtraTool.GetWorldPositionWithResolutionPercent(0.7f, 0.5f, 10);
  28. //obj_6.transform.position = ExtraTool.GetWorldPositionWithResolutionPercent(0.6f, 0.25f, 10);
  29. }
  30. private void Update()
  31. {
  32. return;
  33. snipeImage.GetComponent<RectTransform>().anchoredPosition = ScreenPointToUILocalPoint(rect, Input.mousePosition);
  34. //obj.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 50));
  35. //snipeCamera.transform.LookAt(obj.transform);
  36. ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  37. if (Physics.Raycast(ray, out var hit, 100f))
  38. {
  39. if (Input.GetMouseButton(0))
  40. {
  41. Destroy(hit.transform.gameObject);
  42. }
  43. }
  44. }
  45. public static Vector2 ScreenPointToUILocalPoint(RectTransform parentRT, Vector2 screenPoint)
  46. {
  47. Vector2 localPos;
  48. Camera uiCamera = Camera.main;
  49. RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRT, screenPoint, null, out localPos);
  50. // 转换后的 localPos 使用下面方法赋值
  51. // target 为需要使用的 UI RectTransform
  52. // parentRT 是 target.parent.GetComponent<RectTransform>()
  53. // 最后赋值 target.anchoredPosition = localPos;
  54. return localPos;
  55. }
  56. }