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

  • 相关阅读:
    Maven常用命令
    JUnit注解与hamcrest
    Maven发布工程到私服和JUnit的安装使用
    -Java- Maven命令
    -Java基础-构造器
    Maven命令与pom文件常量
    Maven聚合模块与继承和Maven的生命周期
    BigDecimal
    垃圾000000000000000000000写了很多,保存不上,发送失败了。。。。。A
    自己把源码生成jar,在android项目中调用
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9875675.html
Copyright © 2011-2022 走看看