zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    转:10分钟掌握XML、JSON及其解析
    转:关于C++14:你需要知道的新特性
    2014/11/4~2014/12/20阶段性目标
    转:快速掌握一个语言最常用的50%
    推荐!国外程序员整理的 C++ 资源大全
    数据库面试宝典
    sqlite学习
    android
    转:c的回归-云风
    原创: 开题报告中摘要部分快速将一段文字插入到word的表格中
  • 原文地址:https://www.cnblogs.com/JD85/p/4648768.html
Copyright © 2011-2022 走看看