屏幕适配(UGUI)非UI

using UnityEngine;

public enum Suit_UIType
{
    Background,
    Effect,
}

[RequireComponent(typeof(Transform))]
public class Suit_UI : MonoBehaviour
{
    public Transform target;
    public Suit_UIType type;

    private float screen_width_o = 1366f;
    private float screen_height_o = 768f;
    private float screen_width_c;
    private float screen_height_c;

    private float ratio;
    private float ratio_w;
    private float ratio_h;

    private void Awake()
    {
        screen_width_c = Screen.width;
        screen_height_c = Screen.height;

        float ratio_o = screen_width_o / screen_height_o;
        float ratio_c = screen_width_c / screen_height_c;
        ratio_w = screen_width_c / screen_width_o;
        ratio_h = screen_height_c / screen_height_o;

        ratio = ratio_o / ratio_c;

        if (target == null)
        {
            target = transform;
        }

        switch (type)
        {
            case Suit_UIType.Background:
                Suit_Background();
                break;
            case Suit_UIType.Effect:
                Suit_Effect();
                break;
            default:
                Suit_Effect();
                break;
        }
    }

    private void Suit_Background()
    {
        Vector3 _scale = target.localScale;
        if (ratio > 1)
        {
            _scale.z = _scale.z * (ratio_h / ratio_w);
        }
        else
        {
            _scale.x = _scale.x * (ratio_w / ratio_h);
        }
        target.localScale = _scale;
    }

    private void Suit_Effect()
    {
        Vector3 _scale = target.localScale;
        if (ratio > 1)
        {
            target.localScale = _scale * (ratio_w / ratio_h);
        }
        else
        {
            //长屏适配暂时不用改
        }
    }
}
View Code

适应非UI背景,特效

example : 加特效适配

转载于:https://www.cnblogs.com/Joke-crazy/p/9875675.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值