Unity3d自适应屏幕方案

如果做手机游戏,必不可少的需要涉及到自适应屏幕这个问题,本篇文章不做过多讲解。直接上代码。

AspectUtility.cs脚本来自网络,因考虑到宽屏和窄屏适应问题,设置一个可调节的区间段会比较好,所以此处做过部分修改。

[code]csharpcode:

 

1. [code]csharpcode

2. using System;

3. using System.Collections.Generic;

4. using UnityEngine;

5. 

6. public class AspectUtility : MonoBehaviour

7. {

8.     public Vector2 maxAspectRatio = new Vector2(16, 9);

9.     public Vector2 minAspectRatio = new Vector2(3, 2);

10.     public bool landscapeModeOnly = true;

11. 

12.     private float _wantedAspectRatio = 1.5f;

13.     static public bool _landscapeModeOnly = true;

14.     static float wantedAspectRatio;

15.     static Camera cam;

16.     static Camera backgroundCam;

17. 

18.     void Awake()

19.     {

20.         if (Screen.width * maxAspectRatio.y > Screen.height * maxAspectRatio.x)

21.         {

22.             _wantedAspectRatio = maxAspectRatio.x / maxAspectRatio.y;

23.         }

24.         else if (Screen.width * minAspectRatio.y < Screen.height * minAspectRatio.x)

25.         {

26.             _wantedAspectRatio = minAspectRatio.x / minAspectRatio.y;

27.         }

28.         else

29.         {

30.             _wantedAspectRatio = (float)Screen.width / Screen.height;

31.         }

32. 

33.         _landscapeModeOnly = landscapeModeOnly;

34.         cam = camera;

35.         if (!cam)

36.         {

37.             cam = Camera.main;

38.             Debug.Log("Setting the main camera " + cam.name);

39.         }

40.         else

41.         {

42.             Debug.Log("Setting the main camera " + cam.name);

43.         }

44. 

45.         if (!cam)

46.         {

47.             Debug.LogError("No camera available");

48.             return;

49.         }

50.         wantedAspectRatio = _wantedAspectRatio;

51.         SetCamera();

52.     }

53. 

54.     public static void SetCamera()

55.     {

56.         float currentAspectRatio = 0.0f;

57.         if (Screen.orientation == ScreenOrientation.LandscapeRight ||

58.             Screen.orientation == ScreenOrientation.LandscapeLeft)

59.         {

60.             Debug.Log("Landscape detected...");

61.             currentAspectRatio = (float)Screen.width / Screen.height;

62.         }

63.         else

64.         {

65.             Debug.Log("Portrait detected...?");

66.             if (Screen.height > Screen.width && _landscapeModeOnly)

67.             {

68.                 currentAspectRatio = (float)Screen.height / Screen.width;

69.             }

70.             else

71.             {

72.                 currentAspectRatio = (float)Screen.width / Screen.height;

73.             }

74.         }

75.         // If the current aspect ratio is already approximately equal to the desired aspect ratio,

76.         // use a full-screen Rect (in case it was set to something else previously)

77. 

78.         Debug.Log("currentAspectRatio = " + currentAspectRatio + ", wantedAspectRatio = " + wantedAspectRatio);

79. 

80.         if ((int)(currentAspectRatio * 100) / 100.0f == (int)(wantedAspectRatio * 100) / 100.0f)

81.         {

82.             cam.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);

83.             if (backgroundCam)

84.             {

85.                 Destroy(backgroundCam.gameObject);

86.             }

87.             return;

88.         }

89. 

90.         // Pillarbox

91.         if (currentAspectRatio > wantedAspectRatio)

92.         {

93.             float inset = 1.0f - wantedAspectRatio / currentAspectRatio;

94.             cam.rect = new Rect(inset / 2, 0.0f, 1.0f - inset, 1.0f);

95.         }

96.         // Letterbox

97.         else

98.         {

99.             float inset = 1.0f - currentAspectRatio / wantedAspectRatio;

100.             cam.rect = new Rect(0.0f, inset / 2, 1.0f, 1.0f - inset);

101.         }

102.         if (!backgroundCam)

103.         {

104.             // Make a new camera behind the normal camera which displays black; otherwise the unused space is undefined

105.             backgroundCam = new GameObject("BackgroundCam", typeof(Camera)).camera;

106.             backgroundCam.depth = int.MinValue;

107.             backgroundCam.clearFlags = CameraClearFlags.SolidColor;

108.             backgroundCam.backgroundColor = Color.black;

109.             backgroundCam.cullingMask = 0;

110.         }

111.     }

112. 

113.     public static int screenHeight

114.     {

115.         get

116.         {

117.             return (int)(Screen.height * cam.rect.height);

118.         }

119.     }

120. 

121.     public static int screenWidth

122.     {

123.         get

124.         {

125.             return (int)(Screen.width * cam.rect.width);

126.         }

127.     }

128. 

129.     public static int xOffset

130.     {

131.         get

132.         {

133.             return (int)(Screen.width * cam.rect.x);

134.         }

135.     }

136. 

137.     public static int yOffset

138.     {

139.         get

140.         {

141.             return (int)(Screen.height * cam.rect.y);

142.         }

143.     }

144. 

145.     public static Rect screenRect

146.     {

147.         get

148.         {

149.             return new Rect(cam.rect.x * Screen.width, cam.rect.y * Screen.height, cam.rect.width * Screen.width, cam.rect.height * Screen.height);

150.         }

151.     }

152. 

153.     public static Vector3 mousePosition

154.     {

155.         get

156.         {

157.             Vector3 mousePos = Input.mousePosition;

158.             mousePos.y -= (int)(cam.rect.y * Screen.height);

159.             mousePos.x -= (int)(cam.rect.x * Screen.width);

160.             return mousePos;

161.         }

162.     }

163. 

164.     public static Vector2 guiMousePosition

165.     {

166.         get

167.         {

168.             Vector2 mousePos = Event.current.mousePosition;

169.             mousePos.y = Mathf.Clamp(mousePos.y, cam.rect.y * Screen.height, cam.rect.y * Screen.height + cam.rect.height * Screen.height);

170.             mousePos.x = Mathf.Clamp(mousePos.x, cam.rect.x * Screen.width, cam.rect.x * Screen.width + cam.rect.width * Screen.width);

171.             return mousePos;

172.         }

173.     }

174. }

 

以上脚本可以实现3D场景的自适应,如果采用NGUI布局UI界面,只需要再进行以下处理即可,文章出处【狗刨学习网】:

[code]csharpcode:

 

1. UIRoot root = _uiCamera2D.transform.parent.GetComponent<UIRoot>();

2.         int height;

3.         if (AspectUtility.screenWidth * UIConfig.instance.uiSize.y > AspectUtility.screenHeight * UIConfig.instance.uiSize.x)

4.         {

5.             height = (int)UIConfig.instance.uiSize.y;

6.             if (root != null)

7.             {

8.                 root.maximumHeight = height;

9.                 root.minimumHeight = height;

10.             }

11.         }

12.         else

13.         {

14.             height = (int)(AspectUtility.screenHeight * UIConfig.instance.uiSize.x / AspectUtility.screenWidth);

15.             if (root != null)

16.             {

17.                 root.minimumHeight = height;

18.                 root.maximumHeight = height;

19.             }

20.         }

 

其中的_uiCamera2D自然就是NGUI的2D照相机了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值