private void Awake() { guideEventPenetrate = GetComponentInChildren <GuideEventPenetrate>(); if (guideEventPenetrate != null) { guideEventPenetrate.SetTargetImage(target); } Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); //获取高亮区域 的四个顶点的世界坐标 target.rectTransform.GetWorldCorners(corners); //计算高亮显示区域在画布中的范围 0是左下角 1是左上角 2是右上角 3是右下角 targetOffsetX = Vector2.Distance(WorldToCanvas(canvas, corners[0]), WorldToCanvas(canvas, corners[3])) / 2; targetOffsetY = Vector2.Distance(WorldToCanvas(canvas, corners[0]), WorldToCanvas(canvas, corners[1])) / 2; //计算显示的中心 float x = corners[0].x + ((corners[3].x - corners[0].x)) / 2; float y = corners[0].y + ((corners[1].y - corners[0].y)) / 2; Vector3 centerWorldPos = new Vector3(x, y, 0); Vector2 centerCanvsPos = WorldToCanvas(canvas, centerWorldPos); //设置遮罩材料中心变量 Vector4 centerMat = new Vector4(centerCanvsPos.x, centerCanvsPos.y, 0, 0); material = GetComponent <Image>().material; material.SetVector("_Center", centerMat); //计算当前的偏移 RectTransform canvasRectTransform = canvas.transform as RectTransform; if (canvasRectTransform != null) { //获取画布的四个 世界坐标顶点 canvasRectTransform.GetWorldCorners(corners); //计算初始值 for (int i = 0; i < corners.Length; i++) { if (i % 2 == 0) { currentOffsetX = Mathf.Max(Vector3.Distance(WorldToCanvas(canvas, corners[i]), centerCanvsPos), currentOffsetX); } else { currentOffsetY = Mathf.Max(Vector3.Distance(WorldToCanvas(canvas, corners[i]), centerCanvsPos), currentOffsetY); } } } //设置当前的偏移量 material.SetFloat("_SliderX", currentOffsetX); material.SetFloat("_SliderY", currentOffsetY); }
private void Awake() { guideEventPenetrate = GetComponentInChildren <GuideEventPenetrate>(); if (guideEventPenetrate != null) { guideEventPenetrate.SetTargetImage(target); } Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); //获取高亮区域 的四个顶点的世界坐标 target.rectTransform.GetWorldCorners(corners); //计算最终显示高亮显示区域的半径 radius = Vector2.Distance(WorldToCanvas(canvas, corners[0]), WorldToCanvas(canvas, corners[2])) / 2.0f; //计算高亮显示区域的原型 0是左下角 1是左上角 2是右上角 3是右下角 float x = (corners[0].x + corners[3].x) / 2.0f; float y = (corners[0].y + corners[1].y) / 2.0f; Vector3 centerWorldPos = new Vector3(x, y, 0); Vector2 centerCanvsPos = WorldToCanvas(canvas, centerWorldPos); //设置材质的圆心变量 Vector4 centerMat = new Vector4(centerCanvsPos.x, centerCanvsPos.y, 0, 0); material = GetComponent <Image>().material; material.SetVector("_Center", centerMat); //计算当前显示高亮区域的半径 RectTransform canvasRectTransform = canvas.transform as RectTransform; if (canvasRectTransform != null) { //获取画布的四个 世界坐标顶点 canvasRectTransform.GetWorldCorners(corners); //计算高亮区域的初始半径 for (int i = 0; i < corners.Length; i++) { currentRadius = Mathf.Max(Vector3.Distance(WorldToCanvas(canvas, corners[i]), centerCanvsPos), currentRadius); } } material.SetFloat("_Slider", currentRadius); }