• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
~Joke_crazy
爱生活,爱拉芳!
   首页    新随笔       管理     

屏幕适配(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 : 加特效适配

posted @ 2018-10-30 11:09  ~Joke_crazy  阅读(279)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3