Unity3D結合NGUI的螢幕自適應程式碼分享
using UnityEngine; using System.Collections; public class CalculateScreen : MonoBehaviour { public UIRoot uiRoot = null;//繫結NGUI的Root層 public UISprite mBackgroundSprite=null;//繫結背景Sprite層 public UISprite mFullScreenSprite = null; //繫結需要全屏的Sprite層 private const int cDesignWidth = 2560;//最大設計螢幕Width private const int cDesignHeight = 1440;//最大設計螢幕Hight private int mRealScreenWidth = 0; private int mRealScreenHeight = 0; private readonly float mWidthScale = ConvertHelper.ToFloat(Screen.width,0) / cDesignWidth; private readonly float mHeightScale = ConvertHelper.ToFloat(Screen.height,0) / cDesignHeight; // Use this for initialization void Awake(){ CalculateScreenWidthHeight(); } void Start () { AdaptiveManualHeight(uiRoot); AdaptiveBackgroundSprite(mBackgroundSprite); AdaptiveFullScreenSprite(mFullScreenSprite); } private void CalculateScreenWidthHeight(){ float scale = (float)uiRoot.activeHeight/Screen.height; mRealScreenWidth = Mathf.CeilToInt(Screen.width*scale); mRealScreenHeight = Mathf.CeilToInt(Screen.height*scale); } public void AdaptiveManualHeight(UIRoot root){ if(ConvertHelper.ToFloat(Screen.height,0)/Screen.width>ConvertHelper.ToFloat(cDesignHeight,0)/cDesignWidth){ root.manualHeight = Mathf.RoundToInt(ConvertHelper.ToFloat(cDesignWidth,0)/Screen.width*Screen.height); }else{ root.manualHeight = cDesignHeight; } } public void AdaptiveBackgroundSprite(UISprite backgroundSprite){ if(mRealScreenWidth>backgroundSprite.width||mRealScreenHeight>backgroundSprite.height){ int adaptiveHeight = Mathf.RoundToInt(cDesignHeight*mHeightScale); int adaptiveWidth = Mathf.RoundToInt(cDesignWidth*mHeightScale); if(mHeightScale<=mWidthScale){ adaptiveHeight = Mathf.RoundToInt(cDesignHeight*mWidthScale); adaptiveWidth = Mathf.RoundToInt(cDesignWidth*mWidthScale); } backgroundSprite.SetDimensions(adaptiveWidth,adaptiveHeight); } } public void AdaptiveFullScreenSprite(UISprite fullScreenSprite){ fullScreenSprite.SetDimensions(mRealScreenWidth,mRealScreenHeight); } // Update is called once per frame void Update () { } }
內容均為作者獨立觀點,不代表八零IT人立場,如涉及侵權,請及時告知。
相關文章
- Android 螢幕自適應Android
- 網站如何自適應手機螢幕?網站
- Qt:Qt自適應高解析度螢幕QT
- 學習筆記:自適應佈局,多螢幕適配筆記
- 怎麼讓body高度自適應螢幕?為什麼?
- Android 螢幕適配終結者Android
- 螢幕適配
- Android螢幕適配總結和思考Android
- Android技能樹 — 螢幕適配小結Android
- WebRTC本地分享螢幕,錄製螢幕Web
- AutoLayout螢幕適配
- android 螢幕適配Android
- Flutter螢幕適配Flutter
- iPhone XS/XS Max/XR螢幕區別對比 哪塊螢幕最適合你?iPhone
- Android螢幕適配方案Android
- android螢幕適配方法Android
- Android 主流螢幕以及適配Android
- Android螢幕適配方案分析Android
- java 企業 網站原始碼 模版 螢幕自適應 有前後臺 springmvc SSM 生成靜態化Java網站原始碼SpringMVCSSM
- 如何通過一個SAPGUI螢幕反查這個螢幕對應的事務碼GUI
- flutter 螢幕尺寸適配 字型大小適配Flutter
- [教程文件]html5實現圖片自適應手機螢幕頁面的cssHTMLCSS
- 好程式設計師web前端分享高度自適應程式設計師Web前端
- android 今日頭條的螢幕適配理解Android
- 極其簡單的Flutter 螢幕適配Flutter
- @media 移動端螢幕適配
- 【postcss-px-to-viewport】螢幕適配CSSView
- Flutter螢幕適配 - 等比縮放Flutter
- Flutter螢幕適配,簡單粗暴的全域性適配方式Flutter
- 適合新手的鏈棧程式碼
- textarea文字框高度自適應程式碼例項
- 一種低成本的Flutter螢幕適配方案Flutter
- 淺談-web螢幕適配的解決方案Web
- Android螢幕適配(理論適配100%機型)Android
- 移動 web 端螢幕適配 – remWebREM
- H5 分層螢幕適配H5
- Android APP全方位效能調優之螢幕適配終結者AndroidAPP
- Android技術分享 | 一行程式碼實現螢幕、聲音採集Android行程