{"id":1964,"date":"2019-11-14T10:50:38","date_gmt":"2019-11-14T02:50:38","guid":{"rendered":"http:\/\/www.u3d8.com\/?p=1964"},"modified":"2019-11-14T10:51:37","modified_gmt":"2019-11-14T02:51:37","slug":"ugui%e9%99%90%e5%88%b6ui%e5%9c%a8%e5%b1%8f%e5%b9%95%e5%86%85%e6%88%96%e6%8c%87%e5%ae%9a%e5%8c%ba%e5%9f%9f%e5%86%85","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1964","title":{"rendered":"UGUI\u9650\u5236ui\u5728\u5c4f\u5e55\u5185\u6216\u6307\u5b9a\u533a\u57df\u5185"},"content":{"rendered":"\n<p>\u672c\u6587\u4e3b\u8981\u5b9e\u73b0\u5c06ui\u9650\u5236\u5728\u4e00\u4e2a\u8303\u56f4\u5185\uff0c\u53ef\u4ee5\u662f\u5728\u5f53\u524d\u5c4f\u5e55\u5185\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u533a\u57df\u3002<\/p>\n\n\n\n<p>\u6548\u679c\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img title=\"UGUI\u9650\u5236ui\u5728\u5c4f\u5e55\u5185\u6216\u6307\u5b9a\u533a\u57df\u5185 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"UGUI\u9650\u5236ui\u5728\u5c4f\u5e55\u5185\u6216\u6307\u5b9a\u533a\u57df\u5185 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" width=\"539\" height=\"301\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/11\/ui_limit.gif\" alt=\"\" class=\"wp-image-1966\"\/><\/figure><\/div>\n\n\n\n<p>\u4e0a\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class UITools\n{\n    public static bool UIAreaLimit(RectTransform target, Rect area, Transform root)\n    {\n        Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(root, target);\n\n        Vector2 delta = default;\n        if (bounds.center.x - bounds.extents.x &lt; area.x)\/\/\u5de6\n        {\n            delta.x += Mathf.Abs(bounds.center.x - bounds.extents.x - area.x);\n        }\n        else if (bounds.center.x + bounds.extents.x > area.width \/ 2)\/\/\u53f3\n        {\n            delta.x -= Mathf.Abs(bounds.center.x + bounds.extents.x - area.width \/ 2);\n        }\n\n        if (bounds.center.y - bounds.extents.y &lt; area.y)\/\/\u4e0a\n        {\n            delta.y += Mathf.Abs(bounds.center.y - bounds.extents.y - area.y);\n        }\n        else if (bounds.center.y + bounds.extents.y > area.height \/ 2)\/\/\u4e0b\n        {\n            delta.y -= Mathf.Abs(bounds.center.y + bounds.extents.y - area.height \/ 2);\n        }\n\n\n        target.anchoredPosition += delta;\n\n        return delta != default;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u811a\u672c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\nusing UnityEngine.UI;\n\npublic class Test : MonoBehaviour\n{\n    public RectTransform rt1;\n\n    private Rect rect;\n    CanvasScaler canvaScaler;\n\n    void Awake()\n    {\n        canvaScaler = GetComponent&lt;CanvasScaler>();\n        rect = new Rect(-Screen.width \/ 2, -Screen.height \/ 2, Screen.width, Screen.height);\n        float scale = canvaScaler.matchWidthOrHeight == 1 ? canvaScaler.referenceResolution.y \/ Screen.height : canvaScaler.referenceResolution.x \/ Screen.width;\n        rect = new Rect(rect.x * scale, rect.y * scale, rect.width * scale, rect.height * scale);\n    }\n\n\n    void Update()\n    {\n        UITools.UIAreaLimit(rt1, rect, transform);\n    }\n}\n<\/code><\/pre>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img title=\"UGUI\u9650\u5236ui\u5728\u5c4f\u5e55\u5185\u6216\u6307\u5b9a\u533a\u57df\u5185 - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"UGUI\u9650\u5236ui\u5728\u5c4f\u5e55\u5185\u6216\u6307\u5b9a\u533a\u57df\u5185 - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" width=\"953\" height=\"327\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/11\/QQ\u622a\u56fe20191114104801.jpg\" alt=\"\" class=\"wp-image-1967\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/11\/QQ\u622a\u56fe20191114104801.jpg 953w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/11\/QQ\u622a\u56fe20191114104801-300x103.jpg 300w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/11\/QQ\u622a\u56fe20191114104801-768x264.jpg 768w\" sizes=\"(max-width: 953px) 100vw, 953px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u5b9e\u73b0\u5c06ui\u9650\u5236\u5728\u4e00\u4e2a\u8303\u56f4\u5185\uff0c\u53ef\u4ee5\u662f\u5728\u5f53\u524d\u5c4f\u5e55\u5185\uff0c\u4e5f\u53ef\u4ee5\u6307\u5b9a\u533a\u57df\u3002 \u6548\u679c\u56fe &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1964\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[395],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1964"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1964"}],"version-history":[{"count":3,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1964\/revisions"}],"predecessor-version":[{"id":1969,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1964\/revisions\/1969"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}