public virtual void Init(Image target) { this.target = target; eventPenetrate = GetComponent <GuidanceEventPenetrate>(); if (eventPenetrate != null) { eventPenetrate.SetTargetImage(target); } target.rectTransform.GetWorldCorners(corners); canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); }
private void Awake() { _eventPenetrate = GetComponent <GuidanceEventPenetrate>(); if (_eventPenetrate != null) { _eventPenetrate.SetTargetImage(Target); } //获取画布 Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); //获取高亮区域四个顶点的世界坐标 Target.rectTransform.GetWorldCorners(_corners); //计算高亮显示区域咋画布中的范围 _targetOffsetX = Vector2.Distance(WorldToCanvasPos(canvas, _corners[0]), WorldToCanvasPos(canvas, _corners[3])) / 2f; _targetOffsetY = Vector2.Distance(WorldToCanvasPos(canvas, _corners[0]), WorldToCanvasPos(canvas, _corners[1])) / 2f; //计算高亮显示区域的中心 float x = _corners[0].x + ((_corners[3].x - _corners[0].x) / 2f); float y = _corners[0].y + ((_corners[1].y - _corners[0].y) / 2f); Vector3 centerWorld = new Vector3(x, y, 0); Vector2 center = WorldToCanvasPos(canvas, centerWorld); //设置遮罩材料中中心变量 Vector4 centerMat = new Vector4(center.x, center.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(WorldToCanvasPos(canvas, _corners[i]), center), _currentOffsetX); } else { _currentOffsetY = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, _corners[i]), center), _currentOffsetY); } } } //设置遮罩材质中当前偏移的变量 _material.SetFloat("_SliderX", _currentOffsetX); _material.SetFloat("_SliderY", _currentOffsetY); }
public void Init(Image target) { this.target = target; eventPenetrate = GetComponent <GuidanceEventPenetrate>(); if (eventPenetrate != null) { eventPenetrate.SetTargetImage(target); } Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); //获取高亮区域的四个顶点的世界坐标 target.rectTransform.GetWorldCorners(corners); //计算高亮显示区域在画布中的范围 targetOffsetX = Vector2.Distance(WorldToCanvasPos(canvas, corners[0]), WorldToCanvasPos(canvas, corners[3])) / 2f; targetOffsetY = Vector2.Distance(WorldToCanvasPos(canvas, corners[0]), WorldToCanvasPos(canvas, corners[1])) / 2f; //计算高亮显示区域的中心 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 centerWorld = new Vector3(x, y, 0); Vector2 center = WorldToCanvasPos(canvas, centerWorld); //设置遮罩材质中的中心变量 Vector4 centerMat = new Vector4(center.x, center.y, 0, 0); material = GetComponent <Image>().material; material.SetVector("_Center", centerMat); //计算当前高亮显示区域的半径 RectTransform canRectTransform = canvas.transform as RectTransform; if (canRectTransform != null) { //获取画布区域的四个顶点 canRectTransform.GetWorldCorners(corners); //计算偏移初始值 for (int i = 0; i < corners.Length; i++) { if (i % 2 == 0) { currentOffsetX = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, corners[i]), center), currentOffsetX); } else { currentOffsetY = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, corners[i]), center), currentOffsetY); } } } //设置遮罩材质中当前偏移的变量 material.SetFloat("_SliderX", currentOffsetX); material.SetFloat("_SliderY", currentOffsetY); }
public void Init(Image target) { this.target = target; eventPenetrate = GetComponent <GuidanceEventPenetrate>(); if (eventPenetrate != null) { eventPenetrate.SetTargetImage(target); } Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>(); //获取高亮区域的四个顶点的世界坐标 target.rectTransform.GetWorldCorners(corners); //计算最终高亮显示区域的半径 radius = Vector2.Distance(WorldToCanvasPos(canvas, corners[0]), WorldToCanvasPos(canvas, corners[2])) / 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 centerWorld = new Vector3(x, y, 0); Vector2 center = WorldToCanvasPos(canvas, centerWorld); //设置遮罩材质中的中心变量 Vector4 centerMat = new Vector4(center.x, center.y, 0, 0); material = GetComponent <Image>().material; material.SetVector("_Center", centerMat); //计算当前高亮显示区域的半径 RectTransform canRectTransform = canvas.transform as RectTransform; if (canRectTransform != null) { //获取画布区域的四个顶点 canRectTransform.GetWorldCorners(corners); //将画布顶点距离高亮区域中心最近的距离昨晚当前高亮区域半径的初始值 foreach (var corner in corners) { currentRadius = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, corner), corner), currentRadius); } } material.SetFloat("_Slider", currentRadius); }