屏幕适配(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