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<RectTransform>().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<RectTransform>().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;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值