具体细节可以参考另外一篇随笔!
以下提供的算法完成的事:
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函数)
1 //注:此脚本所在UIRoot的状态必须是活动的 2 public class Adjust : MonoBehaviour 3 { 4 public UIRoot mRoot = null; 5 6 private static Adjust mInstance = null; 7 public static Adjust Instance { get { return mInstance; } } 8 9 /// <summary> 10 /// 自适应相关变量声明 11 /// </summary> 12 private int mRealScreenWidth = 0;//实际屏幕宽 13 private int mRealScreenHeight = 0;//实际屏幕高 14 private const int cDesignWidth = 1280; 15 private const int cDesignHeight = 768; 16 private readonly float mWidthScale = Convert.ToSingle(Screen.width) / cDesignWidth; 17 private readonly float mHeightScale = Convert.ToSingle(Screen.height) / cDesignHeight; 18 private float mScreenSizeIsUnchanged = 0f;//屏幕大小不变 19 private readonly bool mIsMinScreen = (Screen.height < cDesignHeight && Screen.width < cDesignWidth);//是否小屏幕(比设定屏幕1280x768小) 20 21 void Awake() 22 { 23 if (mInstance == null) 24 { 25 mInstance = this; 26 } 27 28 CalculateScreenWidthHeight(); 29 30 Output.Log(string.Format("Adjust.Awake(), mRealScreenWidth = {0}, mRealScreenHeight = {1}", mRealScreenWidth, mRealScreenHeight)); 31 } 32 33 private void CalculateScreenWidthHeight() 34 { 35 if (mRoot != null) 36 { 37 float scale = 1.0f; 38 if (mRoot.activeHeight < Screen.height) 39 { 40 scale = (float)mRoot.activeHeight / Screen.height; 41 } 42 mRealScreenWidth = Mathf.CeilToInt(Screen.width * scale); 43 mRealScreenHeight = Mathf.CeilToInt(Screen.height * scale); 44 45 mScreenSizeIsUnchanged = Mathf.Abs(Convert.ToSingle(mRealScreenWidth) / mRealScreenHeight - Convert.ToSingle(cDesignWidth) / cDesignHeight); 46 47 return; 48 } 49 Output.Error("Adjust.CalculateScreenWidthHeight(), mRoot is null"); 50 } 51 52 public void AdaptiveUI(UIRoot root) 53 { 54 //宽高比不变 55 if (mScreenSizeIsUnchanged < 0.0001f) 56 return; 57 58 //实际屏幕宽高比设定宽高小 59 if (mIsMinScreen) 60 return; 61 62 if (Convert.ToSingle(Screen.height) / Screen.width > Convert.ToSingle(cDesignHeight) / cDesignWidth) 63 root.manualHeight = Mathf.RoundToInt(Convert.ToSingle(cDesignWidth) / Screen.width * Screen.height); 64 else 65 root.manualHeight = cDesignHeight; 66 67 Output.Log(string.Format("Adjust.AdaptiveUI(), Screen.height={0} Screen.width={1}", Screen.height, Screen.width)); 68 } 69 70 //自适应背景 71 public void AdaptiveBackground(UISprite backgroundSprite) 72 { 73 if (mScreenSizeIsUnchanged < 0.0001f) 74 return; 75 76 if (mIsMinScreen) 77 { 78 if (Convert.ToSingle(mRealScreenWidth) / mRealScreenHeight < (float)cDesignWidth / cDesignHeight) 79 { 80 //实际屏幕宽高比 比 设定的屏幕宽高比小,不需做适配 81 return; 82 } 83 else 84 { 85 float scale = cDesignWidth * mHeightScale / mRealScreenWidth; 86 int minScreenW = Convert.ToInt32(Convert.ToSingle(cDesignWidth) / scale); 87 int minScreenH = Convert.ToInt32(Convert.ToSingle(cDesignHeight) / scale); 88 89 backgroundSprite.SetDimensions(minScreenW, minScreenH); 90 91 return; 92 } 93 } 94 95 //实际宽高比设定宽高大 做适配 96 int maxScreenW = Mathf.RoundToInt(cDesignWidth * mHeightScale); 97 int maxScreenH = Mathf.RoundToInt(cDesignHeight * mHeightScale); 98 if (mHeightScale < mWidthScale) 99 { 100 maxScreenW = Mathf.RoundToInt(cDesignWidth * mWidthScale); 101 maxScreenH = Mathf.RoundToInt(cDesignHeight * mWidthScale); 102 } 103 Output.Log(string.Format("maxScreenW = {0}, maxScreenH = {1}", maxScreenW, maxScreenH)); 104 backgroundSprite.SetDimensions(maxScreenW, maxScreenH); 105 } 106 107 //自适应全屏(透明)Sprite 108 public void AdaptiveFullScreenSprite(UISprite fullScreenSprite) 109 { 110 if (mScreenSizeIsUnchanged < 0.0001f) 111 return; 112 113 if (mIsMinScreen) 114 { 115 //小屏的height肯定小于设定height,因此只对width做适配 116 int w = Convert.ToInt32(Convert.ToSingle(cDesignWidth) / ((cDesignWidth * mHeightScale) / mRealScreenWidth)); 117 int h = fullScreenSprite.height; 118 fullScreenSprite.SetDimensions(w, h); 119 120 return; 121 } 122 123 fullScreenSprite.SetDimensions(mRealScreenWidth, mRealScreenHeight); 124 } 125 }
如果帮到你了,给个赞顶一下吧!转载请注明出处,谢谢!