Unity | 发布Android的那些事儿

1.使用UnityWebRequest获取StreamingAssets中的json文件

(1)直接根据不同平台指定url路径

    IEnumerator AITalPredZhanHui()
    {
        string url;
        string fileName = "girl.json";
#if UNITY_EDITOR || UNITY_STANDALONE
        url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
#elif UNITY_IPHONE
        url = "file://" + Application.dataPath + "/Raw/"+ fileName;
#elif UNITY_ANDROID
        url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
#endif
        Debug.Log(url);
        using (UnityWebRequest w = UnityWebRequest.Get(url))
        {
            yield return w.SendWebRequest();
            if (w.isNetworkError || w.isHttpError)
            {
                Debug.Log("加载失败");
            }
            else
            {
                sdkMsg = JsonConvert.DeserializeObject<SDKData>(w.downloadHandler.text);
                //...
            }
        }
    }

(2)运用System.Uri类

System.Uri这个类,这个类可以帮助我们更具不同平台更好的构造uri,特别是在使用本地路径得时候,结合Path.Combine能更好的得出Uri路径。因为平台的区别,Uri路径也不同。

var uri = new System.Uri(Path.Combine(Application.streamingAssetsPath, "girl.json"));
Debug.Log(uri);

以上两种方式在Android端得到的路径相同:jar:file:///data/app/com.DefaultCompany.AndroidTest-ZQ5hoVVjvdSWV_n-_xw2lQ==/base.apk!/assets/girl.json

2.帧率设置

官方文档中有下述描述:

-All mobile platforms have a fix cap for their maximum achievable frame rate, that is equal to the refresh rate of the screen (60 Hz = 60 fps, 40 Hz = 40 fps, ...). Screen.currentResolution contains the screen's refresh rate.

- Additionally, all mobile platforms can only display frames on a VBlank. Therefore, you should set the targetFrameRate to either -1, or a value equal to the screen's refresh rate, or the refresh rate divided by an integer. Otherwise, the resulting frame rate is always lower than targetFrameRate. Note: If you set the targetFrameRate to the refresh rate divided by an integer, the integer division leads to the same effective fps as setting QualitySettings.vSyncCount to the same value as that integer.

翻译下来就是:

所有移动平台都有其最大可实现帧速率的固定上限,即等于屏幕的刷新率(Screen.currentResolution)(60 Hz = 60 fps,40 Hz = 40 fps,...)。Screen.currentResolution包含屏幕的刷新率。

此外,所有移动平台只能在VBlank上显示帧。因此,应该将targetFrameRate设置为 -1、或者等于屏幕刷新率的值、或者刷新率除以一个整数得到的值。否则,生成的帧速率始终低targetFrameRate。注意:如果将targetFrameRate设置为刷新率除以整数,则整数除法导致的有效 fps 与将QualitySettings.vSyncCount设置为与该整数相同的值的效果相同。

举个例子:

我们通过下方代码获取到Android设备的屏幕刷新率为60HZ,那么我们通过Application.targetFrameRate只能是60、30、20、15、12、10FPS,如果我们设置为25,则真实的fps为20。

RefreshRate rate = Screen.currentResolution.refreshRateRatio;

猜你喜欢

转载自blog.csdn.net/weixin_39766005/article/details/129737063