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
- android系統如何自適應螢幕大小Android
- Qt:Qt自適應高解析度螢幕QT
- Unity + ZXing + 螢幕旋轉自動自適應 + 自定義掃碼介面Unity
- 學習筆記:自適應佈局,多螢幕適配筆記
- Android 螢幕適配終結者Android
- WebRTC本地分享螢幕,錄製螢幕Web
- Flutter螢幕適配Flutter
- UIWebView 適配螢幕UIWebView
- android自適應滑動鍵盤產生的螢幕尺寸變化Android
- iPhone XS/XS Max/XR螢幕區別對比 哪塊螢幕最適合你?iPhone
- Android螢幕適配總結和思考Android
- Android技能樹 — 螢幕適配小結Android
- 安卓螢幕適配的方案安卓
- VMware ubuntu 自適應螢幕和檔案共享不顯示 解決Ubuntu
- android 螢幕適配Android
- HTML5 移動頁面自適應手機螢幕四類方法HTML
- Unity3D熱更新全書-指令碼(五) NGUIUnity3D指令碼NGUI
- android螢幕適配方法Android
- Android螢幕適配方案Android
- Android 螢幕適配方案Android
- 基於rem的螢幕適配方案REM
- android螢幕適配的問題Android
- flutter 螢幕尺寸適配 字型大小適配Flutter
- iframe高度自適應程式碼例項
- <iframe>高度自適應程式碼例項
- 使用自定義材質球,實現NGUI螢幕溶解和灰顯NGUI
- [教程文件]html5實現圖片自適應手機螢幕頁面的cssHTMLCSS
- javascript實現的iframe高度自適應程式碼JavaScript
- 如何通過一個SAPGUI螢幕反查這個螢幕對應的事務碼GUI
- 好程式設計師web前端分享高度自適應程式設計師Web前端
- 判斷螢幕旋轉的事件程式碼事件
- java 企業 網站原始碼 模版 螢幕自適應 有前後臺 springmvc SSM 生成靜態化Java網站原始碼SpringMVCSSM
- Android螢幕適配方案分析Android
- Flutter螢幕適配 - 等比縮放Flutter
- Android 主流螢幕以及適配Android