NGUI 屏幕自适应大屏与小屏(初始设定宽高为1280x720,能适应比其小或者更大的屏)...

具体细节可以参考另外一篇随笔!

以下提供的算法完成的事:

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     }

如果帮到你了,给个赞顶一下吧!转载请注明出处,谢谢!

转载于:https://www.cnblogs.com/AlphaAI/p/4056489.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值