• unity中全屏背景图缩放


     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class BgPicScript : MonoBehaviour {
     5 
     6     // Use this for initialization
     7     public UITexture bg;
     8     void Start () {
     9 
    10         Debug.Log("device宽高: " + Screen.width + "_" + Screen.height);
    11 
    12         UIRoot root = GameObject.FindObjectOfType<UIRoot>();
    13 
    14         if (root != null) 
    15         {
    16             //终极大杀器 整体资源缩放比例
    17             float s = (float)root.activeHeight / Screen.height;
    18             int height =  Mathf.CeilToInt(Screen.height * s);
    19             int width = Mathf.CeilToInt(Screen.width * s);
    20           
    21             Debug.Log("按设计分辨率屏幕width = " + width + "_height = " + height);
    22 
    23             Debug.Log("背景图片的设计分辨率bgWIdth:" + bg.width + "_bgHeight:" + bg.height);
    24 
    25             float bgRatio = (float) bg.width / bg.height;
    26             float manualRatio = (float)width / height;
    27 
    28 
    29             if (bgRatio < manualRatio) //遇到按照屏幕高度缩放水平两边出现黑边的情况,要按照宽度来缩放背景图片
    30             {
    31                 //bg.width = width;
    32                 //bg.height = Mathf.CeilToInt(width / );
    33 
    34                 float targetScale = (float)width / bg.width;
    35                 bg.transform.localScale = new Vector3(targetScale,targetScale, 1);
    36             }
    37         }
    38 
    39       
    40     }
    41     
    42     // Update is called once per frame
    43     void Update () {
    44     
    45     }
    46 }
    47 
    48 
    49 
    50 using UnityEngine;
    51 using System.Collections;
    52 
    53 public class UIRootScript : MonoBehaviour {
    54 
    55     // Use this for initialization
    56     void Start () {
    57         AdaptiveUI();
    58     }
    59     
    60     // Update is called once per frame
    61     void Update () {
    62     
    63     }
    64 
    65     static private void AdaptiveUI()
    66     {
    67         int ManualWidth = 1024;
    68         int ManualHeight = 768;
    69         UIRoot uiRoot = GameObject.FindObjectOfType<UIRoot>();
    70         if (uiRoot != null)
    71         {
    72             if (System.Convert.ToSingle(Screen.height) / Screen.width > System.Convert.ToSingle(ManualHeight) / ManualWidth)
    73                 uiRoot.manualHeight = Mathf.RoundToInt(System.Convert.ToSingle(ManualWidth) / Screen.width * Screen.height);
    74             else
    75                 uiRoot.manualHeight = ManualHeight;
    76         }
    77         Debug.Log("uiRoot Height:" + uiRoot.manualHeight);
    78     }
    79 }
  • 相关阅读:
    介绍一篇关于session的好文章,写的很详细
    介绍一篇关于session的好文章,写的很详细
    Web文件的ContentType类型大全
    介绍一篇关于session的好文章,写的很详细
    C++面向对象学习1
    归并排序,不错~~
    在博客园写给自己
    简单的数字图形
    再不写博客就老了
    python日志按时间切分TimedRotatingFileHandler
  • 原文地址:https://www.cnblogs.com/JD85/p/4648768.html
Copyright © 2020-2023  润新知