public void SetTarget(RectTransform target, bool isPauseGame, float timer = 0.5f)
    {
        this.isPauseGame = isPauseGame;
        Target           = target;
        _shrinkTime      = timer;
        _eventPenetrate  = GetComponent <GuidanceEventPenetrate>();
        if (_eventPenetrate != null)
        {
            _eventPenetrate.SetTarget(Target);
        }
        //获取画布
        Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>();

        //获取高亮区域的四个顶点的世界坐标
        Target.GetWorldCorners(_corners);
        //计算最终高亮显示区域的半径
        _radius = Vector2.Distance(WorldToCanvasPos(canvas, _corners[0]), WorldToCanvasPos(canvas, _corners[2])) / 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 canRectTransform = canvas.transform as RectTransform;

        if (canRectTransform != null)
        {
            //获取画布区域的四个顶点
            canRectTransform.GetWorldCorners(_corners);
            //将画布顶点距离高亮区域中心最远的距离作为当前高亮区域半径的初始值
            foreach (Vector3 corner in _corners)
            {
                _currentRadius = Mathf.Max(Vector3.Distance(WorldToCanvasPos(canvas, corner), center), _currentRadius);
            }
        }
        _material.SetFloat("_Slider", _currentRadius);

        if (mShink != null)
        {
            StopCoroutine(mShink);
        }
        if (isPauseGame)
        {
            mShink = StartCoroutine(StartShink());
        }
    }
Пример #2
0
    public void SetTarget(RectTransform rectTransform, bool isPauseGame, float timer = 0.5f)
    {
        this.isPauseGame = isPauseGame;
        Target           = rectTransform;
        _shrinkTime      = timer;
        _eventPenetrate  = GetComponent <GuidanceEventPenetrate>();
        if (_eventPenetrate != null)
        {
            _eventPenetrate.SetTarget(Target);
        }
        //获取画布
        Canvas canvas = GameObject.Find("Canvas").GetComponent <Canvas>();

        //获取高亮区域四个顶点的世界坐标
        Target.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);

        if (mShink != null)
        {
            StopCoroutine(mShink);
        }
        if (isPauseGame)
        {
            mShink = StartCoroutine(StartShink());
        }
    }