unity 屏幕适配

//(需要父物体)RawImage 图片适配
public void SetTextureFullToImg(RawImage img_bg, RectTransform parentRect = null)
    {
        LuaScheduler.nextFrame(img_bg.gameObject, delegate ()
        {
            Rect _imgRect;
            if (parentRect == null)
            {
                _imgRect = img_bg.transform.parent.GetComponent().rect;
            }
            else
            {
                _imgRect = parentRect.rect;
            }
            Texture _txture = img_bg.texture;
            float txtScale = _txture.width / (_txture.height * 1.0f);
            float imgScale = _imgRect.width / (_imgRect.height * 1.0f);

            float expectScale = 0;
            if (imgScale > txtScale) //实际图片宽过大
            {
                expectScale = _imgRect.width / (_txture.width * 1.0f);
                //LuaDebug.error(expectScale + "-----" + _imgRect.width + " " + _txture.width);
            }
            else
            {
                expectScale = _imgRect.height / (_txture.height * 1.0f);
                //LuaDebug.error(expectScale + "-----" + _imgRect.height + " " + _txture.height);
            }
            img_bg.transform.localScale = new Vector3(expectScale, expectScale, expectScale);
        });
    }


//(需要父物体)Image 图片适配
    public void SetImageFullToImg(Image img_bg, RectTransform parentRect = null)
    {
        LuaScheduler.nextFrame(img_bg.gameObject, delegate ()
        {
            Rect _imgRect;
            if (parentRect == null)
            {
                _imgRect = img_bg.transform.parent.GetComponent().rect;
            }
            else
            {
                _imgRect = parentRect.rect;
            }
            float txtScale = img_bg.sprite.rect.width / (img_bg.sprite.rect.height * 1.0f);
            float imgScale = _imgRect.width / (_imgRect.height * 1.0f);

            float expectScale = 0;
            if (imgScale > txtScale) //实际图片宽过大
            {
                expectScale = _imgRect.width / (img_bg.sprite.rect.width * 1.0f);
                //LuaDebug.error(expectScale + "-----" + _imgRect.width + " " + _txture.width);
            }
            else
            {
                expectScale = _imgRect.height / (img_bg.sprite.rect.height * 1.0f);
                //LuaDebug.error(expectScale + "-----" + _imgRect.height + " " + _txture.height);
            }
            img_bg.transform.localScale = new Vector3(expectScale, expectScale, expectScale);
        });
    }


//屏幕适配 缩放比例 (竖屏)
 public float GetConfiScale
    {
        get
        {
            //if (screenScale == -1)
            //{
                float originale = 750 / 1334.0f;  //默认适配比例
                float curScreen = Screen.width / (Screen.height * 1.0f);
                if (curScreen > originale)
                {
                    //screenScale = 1;
                    screenScale = curScreen / originale;
                }
                else if (curScreen < originale)
                {
                    screenScale = originale / curScreen;
                }
            //}
            return screenScale;
        }
    }

你可能感兴趣的:(u3d)