MultipleCrossHair.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. //多准心渲染姿态角-测试用
  6. public class MultipleCrossHair : MonoBehaviour
  7. {
  8. public static MultipleCrossHair ins;
  9. List<RectTransform> crossHairList = new List<RectTransform>();
  10. void Start()
  11. {
  12. ins = this;
  13. BowCamera.ins.onAfterLateUpdate += MyUpdate;
  14. crossHairList.Add(CreateNewCrossHair(Color.green));
  15. crossHairList.Add(CreateNewCrossHair(Color.yellow));
  16. }
  17. readonly Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
  18. void MyUpdate()
  19. {
  20. }
  21. public void UpdateCrossHairByRotation2(int crossHairIndex, Quaternion rotation)
  22. {
  23. RectTransform rectTF = crossHairList[crossHairIndex];
  24. UpdateCrossHairByRotation(rectTF, rotation);
  25. }
  26. void UpdateCrossHairByRotation(RectTransform rectTF, Quaternion rotation)
  27. {
  28. //把z轴旋转弄成0
  29. rotation = Quaternion.LookRotation(rotation * Vector3.forward);
  30. //比率转化,边界限制
  31. Vector3 theAngles = rotation.eulerAngles;
  32. float bowRotateConvertRate = UserSettings.ins.bowRotateConvert.GetRate();
  33. theAngles.x = Mathf.Clamp((theAngles.x > 180 ? theAngles.x - 360 : theAngles.x) * bowRotateConvertRate,
  34. -80, 80);
  35. theAngles.y = Mathf.Clamp((theAngles.y > 180 ? theAngles.y - 360 : theAngles.y) * bowRotateConvertRate,
  36. -80, 80);
  37. //rotation参数看作本地四元组,还得根据游戏场景转成世界四元组
  38. Transform TF = BowCamera.ins.transform;
  39. Vector3 forward = GameMgr.ins.transform.rotation * (Quaternion.Euler(theAngles) * Vector3.forward);
  40. Ray ray = new Ray(TF.position, forward);
  41. RaycastHit hitInfo;
  42. Vector3 targetPosition;
  43. if (Physics.Raycast(ray, out hitInfo, 100))
  44. {
  45. targetPosition = hitInfo.point;
  46. }
  47. else
  48. {
  49. targetPosition = TF.position + forward * 100f;
  50. }
  51. RectTransform parentRTF = CrossHair.ins.transform.parent.GetComponent<RectTransform>();
  52. Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
  53. v3.x *= parentRTF.rect.width;
  54. v3.y *= parentRTF.rect.height;
  55. v3.z = 0;
  56. rectTF.localPosition = v3;
  57. }
  58. //创建一个新的准心
  59. RectTransform CreateNewCrossHair(Color color)
  60. {
  61. Transform parentTF = CrossHair.ins.transform.parent;
  62. RectTransform RTF = new GameObject().AddComponent<RectTransform>();
  63. RTF.SetParent(parentTF);
  64. RTF.localRotation = Quaternion.identity;
  65. RTF.localPosition = Vector3.zero;
  66. RTF.localScale = new Vector3(1, 1, 1);
  67. RTF.gameObject.AddComponent<CanvasRenderer>().cullTransparentMesh = false;
  68. Image imgComp = RTF.gameObject.AddComponent<Image>();
  69. imgComp.sprite = CrossHair.ins.GetComponent<Image>().sprite;
  70. imgComp.SetNativeSize();
  71. Material mat = new Material(Shader.Find("Custom/Outlight"));
  72. mat.SetColor("_LightColor", color);
  73. mat.SetInt("_Size", 30);
  74. imgComp.material = mat;
  75. return RTF;
  76. }
  77. }