| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- //多准心渲染姿态角-测试用
- public class MultipleCrossHair : MonoBehaviour
- {
- public static MultipleCrossHair ins;
- List<RectTransform> crossHairList = new List<RectTransform>();
- void Start()
- {
- ins = this;
- BowCamera.ins.onAfterLateUpdate += MyUpdate;
- crossHairList.Add(CreateNewCrossHair(Color.green));
- crossHairList.Add(CreateNewCrossHair(Color.yellow));
- }
- readonly Vector3 centerPoint = new Vector3(0.5f, 0.5f, 0);
- void MyUpdate()
- {
- }
- public void UpdateCrossHairByRotation2(int crossHairIndex, Quaternion rotation)
- {
- RectTransform rectTF = crossHairList[crossHairIndex];
- UpdateCrossHairByRotation(rectTF, rotation);
- }
- void UpdateCrossHairByRotation(RectTransform rectTF, Quaternion rotation)
- {
- //把z轴旋转弄成0
- rotation = Quaternion.LookRotation(rotation * Vector3.forward);
- //比率转化,边界限制
- Vector3 theAngles = rotation.eulerAngles;
- float bowRotateConvertRate = UserSettings.ins.bowRotateConvert.GetRate();
- theAngles.x = Mathf.Clamp((theAngles.x > 180 ? theAngles.x - 360 : theAngles.x) * bowRotateConvertRate,
- -80, 80);
- theAngles.y = Mathf.Clamp((theAngles.y > 180 ? theAngles.y - 360 : theAngles.y) * bowRotateConvertRate,
- -80, 80);
- //rotation参数看作本地四元组,还得根据游戏场景转成世界四元组
- Transform TF = BowCamera.ins.transform;
- Vector3 forward = GameMgr.ins.transform.rotation * (Quaternion.Euler(theAngles) * Vector3.forward);
- Ray ray = new Ray(TF.position, forward);
- RaycastHit hitInfo;
- Vector3 targetPosition;
- if (Physics.Raycast(ray, out hitInfo, 100))
- {
- targetPosition = hitInfo.point;
- }
- else
- {
- targetPosition = TF.position + forward * 100f;
- }
- RectTransform parentRTF = CrossHair.ins.transform.parent.GetComponent<RectTransform>();
- Vector3 v3 = Camera.main.WorldToViewportPoint(targetPosition) - centerPoint;
- v3.x *= parentRTF.rect.width;
- v3.y *= parentRTF.rect.height;
- v3.z = 0;
- rectTF.localPosition = v3;
- }
- //创建一个新的准心
- RectTransform CreateNewCrossHair(Color color)
- {
- Transform parentTF = CrossHair.ins.transform.parent;
- RectTransform RTF = new GameObject().AddComponent<RectTransform>();
- RTF.SetParent(parentTF);
- RTF.localRotation = Quaternion.identity;
- RTF.localPosition = Vector3.zero;
- RTF.localScale = new Vector3(1, 1, 1);
- RTF.gameObject.AddComponent<CanvasRenderer>().cullTransparentMesh = false;
- Image imgComp = RTF.gameObject.AddComponent<Image>();
- imgComp.sprite = CrossHair.ins.GetComponent<Image>().sprite;
- imgComp.SetNativeSize();
- Material mat = new Material(Shader.Find("Custom/Outlight"));
- mat.SetColor("_LightColor", color);
- mat.SetInt("_Size", 30);
- imgComp.material = mat;
- return RTF;
- }
- }
|