{"id":617,"date":"2012-11-22T17:30:55","date_gmt":"2012-11-22T17:30:55","guid":{"rendered":""},"modified":"2013-03-09T10:56:42","modified_gmt":"2013-03-09T02:56:42","slug":"617","status":"publish","type":"post","link":"https:\/\/1vr.cn\/?p=617","title":{"rendered":"Unity\u5f3a\u5236\u5c4f\u5e55\u7eb5\u5bbd\u6bd4\u811a\u672c"},"content":{"rendered":"<p>\u5f3a\u5236\u5c4f\u5e55\u5230\u6240\u9700\u8981\u7684\u957f\u5bbd\u6bd4,\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u9009\u62e9\u662f\u5426\u5e94\u7528\u5f3a\u5236\u6bd4\u4f8b.\u5176\u4e2d\u5305\u62ec\u8fd4\u56de\u6821\u6b63\u540e\u7684\u5c4f\u5e55\u5bbd\u5ea6,\u9ad8\u5ea6(Screen.width\/height)\u548c\u9f20\u6807\u4f4d\u7f6e(Input.mousePosition).<\/p>\n<p>\u5c06\u8fd9\u4e2a\u811a\u672c\u653e\u5728\u7f16\u8bd1\u987a\u5e8f\u8f83\u4f18\u5148\u7684\u6587\u4ef6\u5939\u4e2d,\u6bd4\u5982 Standard Assets, (Plugins\u662f\u6700\u5148\u7f16\u8bd1\u7684),\u56e0\u6b64\u4f60\u53ef\u4ee5\u7528Js,C#\u7b49\u8bed\u8a00\u8c03\u7528AspectUtility\u6765\u4f7f\u7528\u5b83.\u9996\u5148\u8981\u5c06\u5b83\u9644\u52a0\u5230Camera\u4e0a,\u5b83\u540c\u6837\u53ef\u4ee5\u9644\u52a0\u7ed9\u5176\u4ed6\u7269\u4f53,\u5982\u679c\u4f60\u8d4b\u7ed9\u5176\u4ed6\u7269\u4f53,\u5b83\u5c06\u4e3b\u52a8\u5c1d\u8bd5\u627e\u5230\u6807\u7b7e\u4e3a&#8221;Main Camera&#8221;\u7684\u76f8\u673a.\u5bf9\u4e8eWantedAspectRatio\u8fd9\u4e2a\u503c,\u5e38\u89c1\u7684\u662f4:3\u4e3a1.333333,16:10\u4e3a1.6,16:9\u4e3a1.777778,\u5982\u679c\u5c4f\u5e55\u7684\u7eb5\u5bbd\u6bd4\u4e0e\u8bbe\u5b9a\u7684\u76f8\u540c,\u4e0d\u4f1a\u6709\u4efb\u4f55\u53d8\u5316.\u5982\u679c\u4e0e\u8bbe\u5b9a\u7684\u7eb5\u5bbd\u6bd4\u4e0d\u540c,\u5c06\u7528\u9ed1\u8272\u586b\u5145\u7f3a\u5c11\u7684\u90e8\u5206.<br \/>\n\u8fd9\u6837\u4f1a\u5bfc\u81f4\u6709\u4e9b\u65b9\u6cd5\u8fd4\u56de\u4e0d\u6b63\u786e,\u6bd4\u5982Screen.width\u548cScreen.height\u4f1a\u8fd4\u56de\u5b9e\u9645\u5c4f\u5e55\u5c3a\u5bf8,\u800c\u4e0d\u662f\u4e3b\u76f8\u673a\u7684\u5c3a\u5bf8,\u4e3a\u4e86\u7ea0\u6b63\u8fd9\u4e2a\u95ee\u9898,\u53ef\u4ee5\u4f7f\u7528AspectUtility.screenWidth\u548cAspectUtility.screenHeight.<br \/>\n<!--more--><br \/>\n\u540c\u6837\u7684,Input.mousePosition\u4e5f\u4f1a\u6709\u4e9b\u95ee\u9898,\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u4f7f\u7528AspectUtility.mousePosition\u83b7\u53d6\u9700\u8981\u7684\u503c.<\/p>\n<p>\u6709\u4e00\u70b9\u8981\u6ce8\u610f,\u8fd9\u4e9b\u53d8\u91cf\u5728\u811a\u672c\u521a\u5524\u9192\u7684\u65f6\u5019\u53ef\u80fd\u4e0d\u80fd\u6b63\u786e\u83b7\u53d6\u5230,\u8fd9\u5c31\u9700\u8981\u7b49\u5f85\u811a\u672c\u5f00\u59cb\u540e\u518d\u8bbf\u95ee\u5b83\u4eec.<\/p>\n<p>\u5f53\u5c4f\u5e55\u5c3a\u5bf8\u53d8\u5316,\u4f8b\u5982Webplayer,\u91c7\u75284:3\u7684\u6bd4\u4f8b,\u7528\u6237\u5207\u6362\u5168\u5c4f\u540e\u53d8\u6210\u4e8616:9,\u8fd9\u65f6\u5019\u5c31\u9700\u8981\u7528AspectUtility.SetCamera()\u91cd\u8bbe\u811a\u672c\u76f8\u673a.<\/p>\n<p>OnGUI\u4ee3\u7801\u5728\u7528AspectRatioEnforcer\u65f6\u9700\u8981\u505a\u4e00\u4e9b\u989d\u5916\u7684\u5de5\u4f5c.OnGUI\u7684\u4ee3\u7801\u72ec\u7acb\u4e8eCamera\u753b\u5728\u5c4f\u5e55\u4e0a\u7684.\u6240\u4ee5\u76f8\u673a\u6539\u53d8\u4e86\u77e9\u9635\u5c3a\u5bf8,\u5b83\u4e0d\u4f1a\u81ea\u52a8\u53d8\u5316.\u8fd9\u65f6\u5019\u6211\u4eec\u8981\u7279\u522b\u5904\u7406\u4e00\u4e0b.<\/p>\n<p>\u5982\u679c\u4f60\u662f\u7528GUILayout\u6765\u6392\u5217GUI,\u53ef\u4ee5\u7528GUILayout.BeginArea\u548cEndArea\u6765\u5b9a\u4e49\u4e00\u4e2a\u8303\u56f4,\u50cf\u4e0b\u9762\u8fd9\u6837:<br \/>\n<code><br \/>\nfunction OnGUI () {<br \/>\nGUILayout.Label(\"Hello\");<br \/>\nGUILayout.Label(\"there\");<br \/>\n}<\/code><\/p>\n<p>\u4fee\u6539\u5b83\u4e3a:<\/p>\n<p><code>function OnGUI () {<br \/>\nGUILayout.BeginArea(AspectUtility.screenRect);<\/p>\n<p>GUILayout.Label(\"Hello\");<br \/>\nGUILayout.Label(\"there\");<\/p>\n<p>GUILayout.EndArea();<br \/>\n}<\/code><\/p>\n<p>\u5982\u679c\u5df2\u7ecf\u7528\u8fc7BeginArea\/EndArea,\u4f60\u53ef\u4ee5\u76f4\u63a5\u5728\u5916\u9762\u518d\u5d4c\u5957AspectUtility.screenRect,\u5b83\u4ecd\u65e7\u4f1a\u5904\u7406\u597d\u7684.<\/p>\n<p>\u5982\u679c\u4f60\u6ca1\u6709\u4f7f\u7528GUILayout,\u90a3\u4e48\u53ef\u4ee5\u6dfb\u52a0x\u548cy\u7684\u504f\u79fb\u6765\u4fee\u6b63\u6240\u6709GUI\u7684Rect,\u53ef\u4ee5\u7528AspectUtility.xOffset \u548c AspectUtility.yOffset\u6765\u5f97\u5230\u504f\u79fb\u91cf,\u6bd4\u5982\u4f60\u5e73\u65f6\u8fd9\u6837\u5199:<\/p>\n<p><code><br \/>\nfunction OnGUI () {<br \/>\nGUI.Label(Rect(50, 50, 100, 30), \"Hello\");<br \/>\nGUI.Label(Rect(75, 75, 100, 30), \"there\");<br \/>\n}<\/code><\/p>\n<p>\u73b0\u5728\u6539\u6210:<\/p>\n<p><code><br \/>\nfunction OnGUI () {<br \/>\nvar x = AspectUtility.xOffset;<br \/>\nvar y = AspectUtility.yOffset;<\/p>\n<p>GUI.Label(Rect(x + 50, y + 50, 100, 30), \"Hello\");<br \/>\nGUI.Label(Rect(x + 75, y + 75, 100, 30), \"there\");<br \/>\n}<\/code><\/p>\n<p>\u901a\u5e38\u5728OnGUI\u91cc\u83b7\u5f97\u9f20\u6807\u4f4d\u7f6e\u7528\u7684\u662fEvent.current.mousePosition,\u5728\u8fd9\u91cc\u6211\u4eec\u5e94\u8be5\u6539\u4f7f\u7528AspectUtility.guiMousePosition.<\/p>\n<p>AspectUtility.cs\u811a\u672c<br \/>\n<code>using UnityEngine;<\/p>\n<p>public class AspectUtility : MonoBehaviour {<\/p>\n<p>public float _wantedAspectRatio = 1.3333333f;<br \/>\nstatic float wantedAspectRatio;<br \/>\nstatic Camera cam;<br \/>\nstatic Camera backgroundCam;<\/p>\n<p>void Awake () {<br \/>\ncam = camera;<br \/>\nif (!cam) {<br \/>\ncam = Camera.main;<br \/>\n}<br \/>\nif (!cam) {<br \/>\nDebug.LogError (\"No camera available\");<br \/>\nreturn;<br \/>\n}<br \/>\nwantedAspectRatio = _wantedAspectRatio;<br \/>\nSetCamera();<br \/>\n}<\/p>\n<p>public static void SetCamera () {<br \/>\nfloat currentAspectRatio = (float)Screen.width \/ Screen.height;<br \/>\n\/\/ If the current aspect ratio is already approximately equal to the desired aspect ratio,<br \/>\n\/\/ use a full-screen Rect (in case it was set to something else previously)<br \/>\nif ((int)(currentAspectRatio * 100) \/ 100.0f == (int)(wantedAspectRatio * 100) \/ 100.0f) {<br \/>\ncam.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);<br \/>\nif (backgroundCam) {<br \/>\nDestroy(backgroundCam.gameObject);<br \/>\n}<br \/>\nreturn;<br \/>\n}<br \/>\n\/\/ Pillarbox<br \/>\nif (currentAspectRatio &gt; wantedAspectRatio) {<br \/>\nfloat inset = 1.0f - wantedAspectRatio\/currentAspectRatio;<br \/>\ncam.rect = new Rect(inset\/2, 0.0f, 1.0f-inset, 1.0f);<br \/>\n}<br \/>\n\/\/ Letterbox<br \/>\nelse {<br \/>\nfloat inset = 1.0f - currentAspectRatio\/wantedAspectRatio;<br \/>\ncam.rect = new Rect(0.0f, inset\/2, 1.0f, 1.0f-inset);<br \/>\n}<br \/>\nif (!backgroundCam) {<br \/>\n\/\/ Make a new camera behind the normal camera which displays black; otherwise the unused space is undefined<br \/>\nbackgroundCam = new GameObject(\"BackgroundCam\", typeof(Camera)).camera;<br \/>\nbackgroundCam.depth = int.MinValue;<br \/>\nbackgroundCam.clearFlags = CameraClearFlags.SolidColor;<br \/>\nbackgroundCam.backgroundColor = Color.black;<br \/>\nbackgroundCam.cullingMask = 0;<br \/>\n}<br \/>\n}<\/p>\n<p>public static int screenHeight {<br \/>\nget {<br \/>\nreturn (int)(Screen.height * cam.rect.height);<br \/>\n}<br \/>\n}<\/p>\n<p>public static int screenWidth {<br \/>\nget {<br \/>\nreturn (int)(Screen.width * cam.rect.width);<br \/>\n}<br \/>\n}<\/p>\n<p>public static int xOffset {<br \/>\nget {<br \/>\nreturn (int)(Screen.width * cam.rect.x);<br \/>\n}<br \/>\n}<\/p>\n<p>public static int yOffset {<br \/>\nget {<br \/>\nreturn (int)(Screen.height * cam.rect.y);<br \/>\n}<br \/>\n}<\/p>\n<p>public static Rect screenRect {<br \/>\nget {<br \/>\nreturn new Rect(cam.rect.x * Screen.width, cam.rect.y * Screen.height, cam.rect.width * Screen.width, cam.rect.height * Screen.height);<br \/>\n}<br \/>\n}<\/p>\n<p>public static Vector3 mousePosition {<br \/>\nget {<br \/>\nVector3 mousePos = Input.mousePosition;<br \/>\nmousePos.y -= (int)(cam.rect.y * Screen.height);<br \/>\nmousePos.x -= (int)(cam.rect.x * Screen.width);<br \/>\nreturn mousePos;<br \/>\n}<br \/>\n}<\/p>\n<p>public static Vector2 guiMousePosition {<br \/>\nget {<br \/>\nVector2 mousePos = Event.current.mousePosition;<br \/>\nmousePos.y = Mathf.Clamp(mousePos.y, cam.rect.y * Screen.height, cam.rect.y * Screen.height + cam.rect.height * Screen.height);<br \/>\nmousePos.x = Mathf.Clamp(mousePos.x, cam.rect.x * Screen.width, cam.rect.x * Screen.width + cam.rect.width * Screen.width);<br \/>\nreturn mousePos;<br \/>\n}<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>AspectUtilityEnhanced.cs\u811a\u672c:<\/p>\n<p><code>using UnityEngine;<\/p>\n<p>public class AspectUtility : MonoBehaviour {<\/p>\n<p>public float _wantedAspectRatio = 1.5f;<br \/>\npublic bool landscapeModeOnly = true;<br \/>\nstatic public bool _landscapeModeOnly = true;<br \/>\nstatic float wantedAspectRatio;<br \/>\nstatic Camera cam;<br \/>\nstatic Camera backgroundCam;<\/p>\n<p>void Awake () {<br \/>\n_landscapeModeOnly = landscapeModeOnly;<br \/>\ncam = camera;<br \/>\nif (!cam) {<br \/>\ncam = Camera.main;<br \/>\nDebug.Log (\"Setting the main camera \" + cam.name);<br \/>\n}<br \/>\nelse {<br \/>\nDebug.Log (\"Setting the main camera \" + cam.name);<br \/>\n}<\/p>\n<p>if (!cam) {<br \/>\nDebug.LogError (\"No camera available\");<br \/>\nreturn;<br \/>\n}<br \/>\nwantedAspectRatio = _wantedAspectRatio;<br \/>\nSetCamera();<br \/>\n}<\/p>\n<p>public static void SetCamera () {<br \/>\nfloat currentAspectRatio = 0.0f;<br \/>\nif(Screen.orientation == ScreenOrientation.LandscapeRight ||<br \/>\nScreen.orientation == ScreenOrientation.LandscapeLeft) {<br \/>\nDebug.Log (\"Landscape detected...\");<br \/>\ncurrentAspectRatio = (float)Screen.width \/ Screen.height;<br \/>\n}<br \/>\nelse {<br \/>\nDebug.Log (\"Portrait detected...?\");<br \/>\nif(Screen.height\u00a0\u00a0&gt; Screen.width &amp;&amp; _landscapeModeOnly) {<br \/>\ncurrentAspectRatio = (float)Screen.height \/ Screen.width;<br \/>\n}<br \/>\nelse {<br \/>\ncurrentAspectRatio = (float)Screen.width \/ Screen.height;<br \/>\n}<br \/>\n}<br \/>\n\/\/ If the current aspect ratio is already approximately equal to the desired aspect ratio,<br \/>\n\/\/ use a full-screen Rect (in case it was set to something else previously)<\/p>\n<p>Debug.Log (\"currentAspectRatio = \" + currentAspectRatio + \", wantedAspectRatio = \" + wantedAspectRatio);<\/p>\n<p>if ((int)(currentAspectRatio * 100) \/ 100.0f == (int)(wantedAspectRatio * 100) \/ 100.0f) {<br \/>\ncam.rect = new Rect(0.0f, 0.0f, 1.0f, 1.0f);<br \/>\nif (backgroundCam) {<br \/>\nDestroy(backgroundCam.gameObject);<br \/>\n}<br \/>\nreturn;<br \/>\n}<\/p>\n<p>\/\/ Pillarbox<br \/>\nif (currentAspectRatio &gt; wantedAspectRatio) {<br \/>\nfloat inset = 1.0f - wantedAspectRatio\/currentAspectRatio;<br \/>\ncam.rect = new Rect(inset\/2, 0.0f, 1.0f-inset, 1.0f);<br \/>\n}<br \/>\n\/\/ Letterbox<br \/>\nelse {<br \/>\nfloat inset = 1.0f - currentAspectRatio\/wantedAspectRatio;<br \/>\ncam.rect = new Rect(0.0f, inset\/2, 1.0f, 1.0f-inset);<br \/>\n}<br \/>\nif (!backgroundCam) {<br \/>\n\/\/ Make a new camera behind the normal camera which displays black; otherwise the unused space is undefined<br \/>\nbackgroundCam = new GameObject(\"BackgroundCam\", typeof(Camera)).camera;<br \/>\nbackgroundCam.depth = int.MinValue;<br \/>\nbackgroundCam.clearFlags = CameraClearFlags.SolidColor;<br \/>\nbackgroundCam.backgroundColor = Color.black;<br \/>\nbackgroundCam.cullingMask = 0;<br \/>\n}<br \/>\n}<\/p>\n<p>public static int screenHeight {<br \/>\nget {<br \/>\nreturn (int)(Screen.height * cam.rect.height);<br \/>\n}<br \/>\n}<\/p>\n<p>public static int screenWidth {<br \/>\nget {<br \/>\nreturn (int)(Screen.width * cam.rect.width);<br \/>\n}<br \/>\n}<\/p>\n<p>public static int xOffset {<br \/>\nget {<br \/>\nreturn (int)(Screen.width * cam.rect.x);<br \/>\n}<br \/>\n}<\/p>\n<p>public static int yOffset {<br \/>\nget {<br \/>\nreturn (int)(Screen.height * cam.rect.y);<br \/>\n}<br \/>\n}<\/p>\n<p>public static Rect screenRect {<br \/>\nget {<br \/>\nreturn new Rect(cam.rect.x * Screen.width, cam.rect.y * Screen.height, cam.rect.width * Screen.width, cam.rect.height * Screen.height);<br \/>\n}<br \/>\n}<\/p>\n<p>public static Vector3 mousePosition {<br \/>\nget {<br \/>\nVector3 mousePos = Input.mousePosition;<br \/>\nmousePos.y -= (int)(cam.rect.y * Screen.height);<br \/>\nmousePos.x -= (int)(cam.rect.x * Screen.width);<br \/>\nreturn mousePos;<br \/>\n}<br \/>\n}<\/p>\n<p>public static Vector2 guiMousePosition {<br \/>\nget {<br \/>\nVector2 mousePos = Event.current.mousePosition;<br \/>\nmousePos.y = Mathf.Clamp(mousePos.y, cam.rect.y * Screen.height, cam.rect.y * Screen.height + cam.rect.height * Screen.height);<br \/>\nmousePos.x = Mathf.Clamp(mousePos.x, cam.rect.x * Screen.width, cam.rect.x * Screen.width + cam.rect.width * Screen.width);<br \/>\nreturn mousePos;<br \/>\n}<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>\u539f\u6587:<a href=\"http:\/\/wiki.unity3d.com\/index.php?title=AspectRatioEnforcer\" target=\"_blank\" rel=\"external\">http:\/\/wiki.unity3d.com\/index.php?title=AspectRatioEnforcer<\/a><br \/>\n\u8bd1\u8005:\u5a01\u963f,\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u6765\u81ea1vr.cn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f3a\u5236\u5c4f\u5e55\u5230\u6240\u9700\u8981\u7684\u957f\u5bbd\u6bd4,\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u7531\u9009\u62e9\u662f\u5426\u5e94\u7528\u5f3a\u5236\u6bd4\u4f8b.\u5176\u4e2d\u5305\u62ec\u8fd4\u56de\u6821\u6b63\u540e\u7684\u5c4f\u5e55\u5bbd\u5ea6,\u9ad8\u5ea6(Scree &hellip; <a href=\"https:\/\/1vr.cn\/?p=617\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Unity\u5f3a\u5236\u5c4f\u5e55\u7eb5\u5bbd\u6bd4\u811a\u672c<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-617","post","type-post","status-publish","format-standard","hentry","without-featured-image"],"_links":{"self":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/617","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=617"}],"version-history":[{"count":2,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":1339,"href":"https:\/\/1vr.cn\/index.php?rest_route=\/wp\/v2\/posts\/617\/revisions\/1339"}],"wp:attachment":[{"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1vr.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}