具体细节可以参考另外一篇随笔!
以下提供的算法完成的事:
1.自适应1280x720分辨率以下的屏幕
2.自适应1280x720分辨率以上的屏幕
在我设定的要求内包括的分辨率大部分都测过了,背景图、全屏透明Sprite(主要用于九宫格区域的控件摆放)自适应都没问题(不会变形),其他的控件当然是由UIRoot组件搞定的!下面的算法主要就是说明背景、全屏透明Sprite的自适应!
之前的随笔已经说过实际屏幕比设定屏幕大的自适应,现在加入自适应比设定屏幕小的算法!
detail:
使用NGUI版本为3.6
设置:UIRoot中Manual Height=768(由于我们游戏UI定的大小是1280x768),Minimum=480(最小支持高为480的屏,更小的我没测过), Maximum=1536
脚本(将其挂在游戏对象下,每一个需要自适应的UI,都调用这个脚本里面的函数,传进去对应的参数,如AdaptiveUI函数、背景Sprite函数、全屏Sprite函数)
//注:此脚本所在UIRoot的状态必须是活动的 public class Adjust : MonoBehaviour { public UIRoot mRoot = null; private static Adjust mInstance = null; public static Adjust Instance { get { return mInstance; } } /// <summary> /// 自适应相关变量声明 /// </summary> ;//实际屏幕宽 ;//实际屏幕高 ; ; private readonly float mWidthScale = Convert.ToSingle(Screen.width) / cDesignWidth; private readonly float mHeightScale = Convert.ToSingle(Screen.height) / cDesignHeight; private float mScreenSizeIsUnchanged = 0f;//屏幕大小不变 private readonly bool mIsMinScreen = (Screen.height < cDesignHeight && Screen.width < cDesignWidth);//是否小屏幕(比设定屏幕1280x768小) void Awake() { if (mInstance == null) { mInstance = this; } CalculateScreenWidthHeight(); Output.Log(string.Format("Adjust.Awake(), mRealScreenWidth = {0}, mRealScreenHeight = {1}", mRealScreenWidth, mRealScreenHeight)); } private void CalculateScreenWidthHeight() { if (mRoot != null) { float scale = 1.0f; if (mRoot.activeHeight < Screen.height) { scale = (float)mRoot.activeHeight / Screen.height; } mRealScreenWidth = Mathf.CeilToInt(Screen.width * scale); mRealScreenHeight = Mathf.CeilToInt(Screen.height * scale); mScreenSizeIsUnchanged = Mathf.Abs(Convert.ToSingle(mRealScreenWidth) / mRealScreenHeight - Convert.ToSingle(cDesignWidth) / cDesignHeight); return; } Output.Error("Adjust.CalculateScreenWidthHeight(), mRoot is null"); } public void AdaptiveUI(UIRoot root) { //宽高比不变 if (mScreenSizeIsUnchanged < 0.0001f) return; //实际屏幕宽高比设定宽高小 if (mIsMinScreen) return; if (Convert.ToSingle(Screen.height) / Screen.width > Convert.ToSingle(cDesignHeight) / cDesignWidth) root.manualHeight = Mathf.RoundToInt(Convert.ToSingle(cDesignWidth) / Screen.width * Screen.height); else root.manualHeight = cDesignHeight; Output.Log(string.Format("Adjust.AdaptiveUI(), Screen.height={0} Screen.width={1}", Screen.height, Screen.width)); } //自适应背景 public void AdaptiveBackground(UISprite backgroundSprite) { if (mScreenSizeIsUnchanged < 0.0001f) return; if (mIsMinScreen) { if (Convert.ToSingle(mRealScreenWidth) / mRealScreenHeight < (float)cDesignWidth / cDesignHeight) { //实际屏幕宽高比 比 设定的屏幕宽高比小,不需做适配 return; } else { float scale = cDesignWidth * mHeightScale / mRealScreenWidth; int minScreenW = Convert.ToInt32(Convert.ToSingle(cDesignWidth) / scale); int minScreenH = Convert.ToInt32(Convert.ToSingle(cDesignHeight) / scale); backgroundSprite.SetDimensions(minScreenW, minScreenH); return; } } //实际宽高比设定宽高大 做适配 int maxScreenW = Mathf.RoundToInt(cDesignWidth * mHeightScale); int maxScreenH = Mathf.RoundToInt(cDesignHeight * mHeightScale); if (mHeightScale < mWidthScale) { maxScreenW = Mathf.RoundToInt(cDesignWidth * mWidthScale); maxScreenH = Mathf.RoundToInt(cDesignHeight * mWidthScale); } Output.Log(string.Format("maxScreenW = {0}, maxScreenH = {1}", maxScreenW, maxScreenH)); backgroundSprite.SetDimensions(maxScreenW, maxScreenH); } //自适应全屏(透明)Sprite public void AdaptiveFullScreenSprite(UISprite fullScreenSprite) { if (mScreenSizeIsUnchanged < 0.0001f) return; if (mIsMinScreen) { //小屏的height肯定小于设定height,因此只对width做适配 int w = Convert.ToInt32(Convert.ToSingle(cDesignWidth) / ((cDesignWidth * mHeightScale) / mRealScreenWidth)); int h = fullScreenSprite.height; fullScreenSprite.SetDimensions(w, h); return; } fullScreenSprite.SetDimensions(mRealScreenWidth, mRealScreenHeight); } }
如果帮到你了,给个赞顶一下吧!转载请注明出处,谢谢!