{"id":1908,"date":"2019-01-25T13:30:06","date_gmt":"2019-01-25T05:30:06","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1908"},"modified":"2019-05-02T13:48:08","modified_gmt":"2019-05-02T05:48:08","slug":"unity%e5%b8%a7%e7%8e%87%e6%8f%90%e5%8d%87%e6%9c%80%e6%9c%89%e6%95%88%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1908","title":{"rendered":"Unity\u5e27\u7387\u63d0\u5347\u6700\u6709\u6548\u6280\u5de7"},"content":{"rendered":"<p>\u6027\u80fd\u4f18\u5316\u7ec8\u6781\u6280\u5de7\uff1a\u964d\u4f4e\u5206\u8fa8\u7387<\/p>\n<p>\u5c31\u662f\u4f7f\u7528Unity\u51fd\u6570<\/p>\n<pre class=\"lang:c# decode:true\">Screen.SetResolution(240, 320, true);<\/pre>\n<p>\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8ba9\u4f60\u5e27\u7387\u98d9\u5347\uff01<\/p>\n<p>10+\u5e27\u7387\u77ac\u95f4\u6ee160\u5e27\uff01<\/p>\n<p>\u8a00\u5f52\u6b63\u4f20\uff0c\u672c\u6587\u662f\u505a\u4e00\u4e2a\u6839\u636e\u5e27\u7387\u52a8\u6001\u964d\u4f4e\u5206\u8fa8\u7387\uff0c\u4ee5\u727a\u7272\u753b\u8d28\uff0c\u4fdd\u8bc1\u5e27\u7387\u7a33\u5b9a\u7684\u903b\u8f91<\/p>\n<p>\u76f4\u63a5\u6302\u5728\u5373\u53ef\uff0c\u53ef\u4ee5\u5728\u6bd4\u8f83\u5361\u5e27\u7684\u573a\u666f\u4e2d\u6253\u5305\u81ea\u6d4b<\/p>\n<pre class=\"lang:c# decode:true \">using System.Collections;\r\nusing System.Collections.Generic;\r\nusing UnityEngine;\r\n\r\npublic class DynamicResolution : MonoBehaviour {\r\n\r\n    public float showTime = 1f;\r\n    private int count = 0;\r\n    private float deltaTime = 0f;\r\n\r\n    List&lt;float&gt; fpsList = new List&lt;float&gt;();\r\n    int[] resolutionWidths = new int[3];\r\n    private int lowestFps = 50;\r\n\r\n    private void Start()\r\n    {\r\n        InitSolutions();\r\n        SetResolution(resolutionWidths[0]);\r\n    }\r\n\r\n    void Update()\r\n    {\r\n        count++;\r\n        deltaTime += Time.deltaTime;\r\n        if (deltaTime &gt;= showTime)\r\n        {\r\n            float fps = count \/ deltaTime;\r\n            float milliSecond = deltaTime * 1000 \/ count;\r\n            count = 0;\r\n            deltaTime = 0f;\r\n\r\n            SetResolutionReduce(fps);\r\n        }\r\n    }\r\n\r\n\r\n    void InitSolutions()\r\n    {\r\n        int currentWidth = Screen.currentResolution.width;\r\n        for (int i = 0; i &lt; resolutionWidths.Length; i++)\r\n        {\r\n            resolutionWidths[i] = currentWidth - (int)(currentWidth * (i + 1) * 0.1f);\r\n        }\r\n    }\r\n\r\n    void SetResolution(int width)\r\n    {\r\n        int currentWidth = Screen.currentResolution.width;\r\n        int currentHeight = Screen.currentResolution.height;\r\n        int height = currentHeight * width \/ currentWidth;\r\n        Screen.SetResolution(width, height, true);\r\n        Debuger.Log(\"\u5f3a\u5236\u964d\u4f4e\u5206\u8fa8\u7387 \u76ee\u6807\u5206\u8fa8\u7387\u4e3a\uff1a\" + width + \" \" + height + \"  \u5f53\u524d\u5206\u8fa8\u7387\uff1a\" + currentWidth + \" \" + currentHeight);\r\n    }\r\n\r\n    void SetResolutionReduce(float fps)\r\n    {\r\n        if (fps &lt; lowestFps)\r\n        {\r\n            fpsList.Add(fps);\r\n        }\r\n        else\r\n        {\r\n            fpsList.Clear();\r\n        }\r\n        if (fpsList.Count &lt; 5) return;\r\n        fpsList.RemoveRange(0, 3);\r\n\r\n        int currentWidth = Screen.currentResolution.width;\r\n        int currentHeight= Screen.currentResolution.height;\r\n        if(currentWidth &lt; resolutionWidths[resolutionWidths.Length - 1])\r\n        {\r\n            return;\r\n        }\r\n\r\n        foreach (int width in resolutionWidths)\r\n        {\r\n            if(width &lt; currentWidth)\r\n            {\r\n                int height = currentHeight * width \/ currentWidth;\r\n                Screen.SetResolution(width, height, true);\r\n                Debuger.Log(\"\u7531\u4e8e\u5e27\u7387\u592a\u4f4e\uff0c\u5f3a\u5236\u964d\u4f4e\u5206\u8fa8\u7387 \u76ee\u6807\u5206\u8fa8\u7387\u4e3a\uff1a\" + width + \" \" + height + \"  \u5f53\u524d\u5206\u8fa8\u7387\uff1a\" + currentWidth + \" \" + currentHeight);\r\n                break;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6027\u80fd\u4f18\u5316\u7ec8\u6781\u6280\u5de7\uff1a\u964d\u4f4e\u5206\u8fa8\u7387 \u5c31\u662f\u4f7f\u7528Unity\u51fd\u6570 Screen.SetRes &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1908\">\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":[252],"tags":[250,388],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1908"}],"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=1908"}],"version-history":[{"count":2,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1908\/revisions"}],"predecessor-version":[{"id":1925,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1908\/revisions\/1925"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1908"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1908"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1908"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}