zoukankan      html  css  js  c++  java
  • 屏幕适配(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 : 加特效适配

  • 相关阅读:
    P1082 同余方程
    P2678 跳石头
    P2827 蚯蚓
    P1351 联合权值
    P2822 组合数问题
    P3958 奶酪
    P2296 寻找道路
    P2661 信息传递
    平时问题总结
    平时总结
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9875675.html
Copyright © 2011-2022 走看看