Ejemplo n.º 1
0
    public void RefreshRectList()
    {
        string _str = text.text;
//		string _log = "";

        TextGenerator _generator = text.cachedTextGenerator;

        IList <UIVertex> _vertList = _generator.verts;

        worldRectList.Clear();
        uiRectList.Clear();

        if (canvasScaler == null)
        {
            canvasScaler = GameObjectExtend.GetComponentInScene <CanvasScaler> ();
            if (canvasScaler == null)
            {
                Debug.LogError("UiTextPositioning No CanvasScaler");
                return;
            }
        }

        RectTransform _rectTransform    = canvasScaler.gameObject.GetComponent <RectTransform>();
        Vector2       _uiResolution     = _rectTransform.sizeDelta;
        Vector2       _screenResolution = GameObjectExtend.GetViewSize();
        Vector2       _uiScale          = new Vector2(_uiResolution.x / _screenResolution.x,
                                                      _uiResolution.y / _screenResolution.y);
        Vector2 _worldScale = (Vector2)transform.lossyScale;



        int f;
        int _vertLel = _vertList.Count / 4;
        int len      = _str.Length;

        if (_vertLel != len)
        {
            len = Mathf.Min(len, _vertLel - 1);
        }

        for (f = 0; f < len; f++)
        {
            Vector2 _rtPos = _vertList[f * 4 + 1].position;
            Vector2 _lbPos = _vertList[f * 4 + 3].position;

            _rtPos.x *= _uiScale.x;
            _rtPos.y *= _uiScale.y;
            _lbPos.x *= _uiScale.x;
            _lbPos.y *= _uiScale.y;

            Rect _uiRect = new Rect(_lbPos, _rtPos - _lbPos);
            uiRectList.Add(_uiRect);

            _rtPos.x *= _worldScale.x;
            _rtPos.y *= _worldScale.y;
            _lbPos.x *= _worldScale.x;
            _lbPos.y *= _worldScale.y;
            Rect _worldRect = new Rect(_lbPos, _rtPos - _lbPos);
            worldRectList.Add(_worldRect);
        }

        #if UNITY_EDITOR
        lastStr = _str;
        SceneView.RepaintAll();
        #endif
    }