{"id":4694,"date":"2023-03-07T20:46:51","date_gmt":"2023-03-07T12:46:51","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=4694"},"modified":"2023-03-07T21:03:38","modified_gmt":"2023-03-07T13:03:38","slug":"%e6%b5%ae%e5%8a%a8tips%e7%9a%84%e4%b8%80%e7%a7%8d%e5%88%b6%e4%bd%9c%e6%80%9d%e8%b7%af","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=4694","title":{"rendered":"\u6d6e\u52a8Tips\u7684\u4e00\u79cd\u5236\u4f5c\u601d\u8def"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>\u6e38\u620f\u5185\u6d6e\u52a8Tips\u7684\u5236\u4f5c\u8981\u6c42\uff1a\n\u70b9\u51fb\u67d0\u611f\u53f9\u53f7\uff0c\u6216\u8005\u9053\u5177\uff0c\u4f1a\u5f39\u51fa\u4e00\u4e2a\u5c0fTips\n\u70b9\u51fb\u5176\u5b83\u4efb\u4f55\u5730\u65b9\uff0c\u4f1a\u76f4\u63a5\u5173\u95ed\u6b64Tips\n\u4f46\u70b9\u51fb\u6b64Tips\u5185\u7684\u5143\u7d20\uff0c\u4e0d\u4f1a\u5173\u95ed\u6b64Tips\n\n\n\u5236\u4f5c\u601d\u8def\uff1a\n\u5982\u679c\u70b9\u51fb\u547d\u4e2dTips\u9762\u677f\u7684\u201c\u5b50\u5143\u7d20\u201d\uff0c\u5219\u4e0d\u5904\u7406\uff08\u76f8\u5f53\u4e8e\u70b9\u4e86\u9762\u677f\u5185\u7684\u5143\u7d20\uff09\uff1b\n\u5982\u679c\u70b9\u51fb\u5728Tips\u9762\u677f\u4e4b\u5916\uff0c\u5219\u628aTips\u9762\u677f\u5173\u95ed\uff1b\n\n1\u3001\u4f7f\u7528RectTransformUtility.RectangleContainsScreenPoint\u51fd\u6570\u8fdb\u884c\u68c0\u6d4b\n2\u3001\u68c0\u6d4b\u65f6\uff0c\u8981\u6ce8\u610fUI\u76f8\u673a\uff0c\u5982\u679c\u4e0d\u662fOverlay\u76f8\u673a\uff0c\u5219\u9700\u8981\u67e5\u627e\u5f53\u524d\u7684UI\u76f8\u673a\n\npublic class FloatTipsBackground : MonoBehaviour\n{\n    private Camera mCamera;\n    private bool mCameraInit= false;\n\n    private Camera GetUICamera()\n    {\n        if (mCameraInit)\n        {\n            return mCamera;\n        }\n\n        mCamera = FindParentUICamera();\n        mCameraInit = true;\n        return mCamera;\n    }\n\n    private Camera FindParentUICamera()\n    {\n        var parentObj = this.transform.parent;\n        for(int i=0; i&lt;50 &amp;&amp; parentObj != null; ++i)\n        {\n            var canvas = parentObj.GetComponent&lt;Canvas>();\n            if (canvas != null &amp;&amp; canvas.renderMode == RenderMode.ScreenSpaceCamera)\n            {\n                return canvas.worldCamera;\n            }\n            parentObj = parentObj.parent;\n        }\n        return null;\n    }\n\n    public void Update()\n    {\n        Vector3 touchPosition = Input.mousePosition;\n\n#if UNITY_EDITOR || UNITY_STANDALONE\n        if (Input.GetMouseButtonDown(0))\n        {\n            touchPosition = Input.mousePosition;\n            HandlerClick(touchPosition);\n        }\n#else\n        if (Input.touchCount > 0 &amp;&amp; Input.touches[0].phase == TouchPhase.Began )  \n        {\n            touchPosition = Input.touches[0].position;\n            HandlerClick(touchPosition);\n        }\n#endif\n    }\n\n    public bool PointHitChilds(GameObject parentObject, Vector2 p)\n    {\n        var camera = GetUICamera();\n        var childs = parentObject.GetComponentsInChildren&lt;UnityEngine.UI.Graphic>(false);\n        foreach(var child in childs)\n        {\n            if (child.gameObject == parentObject)\n            {\n                continue;\n            }\n            if (RectTransformUtility.RectangleContainsScreenPoint(child.rectTransform, p, camera))\n            {\n                return true;\n            }\n        }\n        return false;\n    }\n\n    private void HandlerClick(Vector3 touchPosition)\n    {\n        Vector2 p = new Vector2(touchPosition.x, touchPosition.y);\n        if (!PointHitChilds(this.gameObject, p))\n        {\n            this.gameObject.SetActive(false);\n        }\n    }\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4694"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4694"}],"version-history":[{"count":5,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4694\/revisions"}],"predecessor-version":[{"id":4699,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/4694\/revisions\/4699"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}