unity中全屏背景图缩放
2015-07-15 16:34
681 查看
using UnityEngine; using System.Collections; public class BgPicScript : MonoBehaviour { // Use this for initialization public UITexture bg; void Start () { Debug.Log("device宽高: " + Screen.width + "_" + Screen.height); UIRoot root = GameObject.FindObjectOfType<UIRoot>(); if (root != null) { //终极大杀器 整体资源缩放比例 float s = (float)root.activeHeight / Screen.height; int height = Mathf.CeilToInt(Screen.height * s); int width = Mathf.CeilToInt(Screen.width * s); Debug.Log("按设计分辨率屏幕width = " + width + "_height = " + height); Debug.Log("背景图片的设计分辨率bgWIdth:" + bg.width + "_bgHeight:" + bg.height); float bgRatio = (float) bg.width / bg.height; float manualRatio = (float)width / height; if (bgRatio < manualRatio) //遇到按照屏幕高度缩放水平两边出现黑边的情况,要按照宽度来缩放背景图片 { //bg.width = width; //bg.height = Mathf.CeilToInt(width / ); float targetScale = (float)width / bg.width; bg.transform.localScale = new Vector3(targetScale,targetScale, 1); } } } // Update is called once per frame void Update () { } } using UnityEngine; using System.Collections; public class UIRootScript : MonoBehaviour { // Use this for initialization void Start () { AdaptiveUI(); } // Update is called once per frame void Update () { } static private void AdaptiveUI() { int ManualWidth = 1024; int ManualHeight = 768; UIRoot uiRoot = GameObject.FindObjectOfType<UIRoot>(); if (uiRoot != null) { if (System.Convert.ToSingle(Screen.height) / Screen.width > System.Convert.ToSingle(ManualHeight) / ManualWidth) uiRoot.manualHeight = Mathf.RoundToInt(System.Convert.ToSingle(ManualWidth) / Screen.width * Screen.height); else uiRoot.manualHeight = ManualHeight; } Debug.Log("uiRoot Height:" + uiRoot.manualHeight); } }
相关文章推荐
- 车轮碰撞体(wheelcollider)
- unity3d 代码加密方案
- Unity 3D 特殊文件夹
- Unity笔记 资源加载 内存优化
- unity3d calculatepath
- 火箭鼠激光版自动开关,换图,旋转
- Unity3D研究院之拓展系统自带组件的Inspector视图
- 利用Unity来实现插件开发
- Unity3d UnityEditor编辑器定制和开发插件
- Unity协程(Coroutine)原理深入剖析
- Unity3d-反编译C#和提取资源
- Unity3D研究之与根据动态的两个轨迹点绘制面详解
- Unity3D 游戏引擎之控制模型移动旋转与碰撞
- Unity3D研究之第一人称第三人称角色控制组件修改
- Unity3D开发:为地形添加水源和效果
- Unity3D开发:在Unity3D中创建一个山势地形图
- 实战Unity3D开发:从认识界面到游戏demo
- Unity3D基础知识梳理
- Unity3D开发:Unity3D中的脚本概述
- Unity 3D引擎:十大最火的插件推荐