免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
【新提醒】【Unity實(shí)用小工具或腳本
一、前言
       項(xiàng)目上需要加載多個地圖,每個地圖的貼圖將近200M,所有地圖加起來貼圖將近2G。因此,想著能不能將貼圖放到工程外加載,逐研究了一下,得出了三種加載方式,分別是WWW加載、C#原生的IO加載在轉(zhuǎn)換成Texture2DAssetbundle打包和加載。
       前面兩種方式都會存在卡頓現(xiàn)象,因?yàn)樽钕男阅艿挠?jì)算方式最終都還是放在主線程里。盡管第二種方式可以使用另外一個線程加載圖片成Bytes數(shù)組,但是將字節(jié)數(shù)組轉(zhuǎn)成成Texture2D還是在主線程里,而這個過程在圖片5M的時候還是很卡頓,何況我的地形貼圖每張有20M左右。對于前面兩種方式?jīng)]有找到任何其他好的優(yōu)化方式來解決。第三種方式是我用到最理想的,在加載的過程中不會有卡頓。
二、WWW加載
首先,獲取當(dāng)前工程同目錄下的“MapImages”文件夾路徑,然后獲取每張圖片的全部路徑,并將路徑存到列表中。
[C#] 純文本查看 復(fù)制代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  /// <summary>
    /// 獲取當(dāng)前物體應(yīng)該讀取的地形貼圖的文件的路徑
    /// </summary>
    /// <returns></returns>
    private string GetFilePath()
    {
        string[] strTempPath = Application.dataPath.Split('/');
        string strPath = string.Empty;
        //去掉后面兩個,獲取跟工程相同目錄的路徑,如“E:/ZXB/MyProject/Asset/",去掉后面兩個就得到和工程同級的目錄為:“E:/ZXB”
        for (int i = 0; i < strTempPath.Length-2; i++)
        {
            strPath += strTempPath[i] + "/";
        }
        //加上整個地形貼圖的文件命
        strPath += TERRAINMAP_FILENAME + "/";
        //最后加上當(dāng)前文件夾的名稱,最后的文件夾名稱和當(dāng)前物體的名稱一致
        strPath += gameObject.name + "/";
        return strPath;
}
    /// <summary>
    /// 獲取所有地圖貼圖文件路徑
    /// </summary>
    /// <param name="info"></param>
    private void GetAllFile(FileSystemInfo info)
    {
        if (!info.Exists)
        {
            Debug.Log("該路徑不存在!");
            return;
        }
        DirectoryInfo dir = info as DirectoryInfo;
        if(null==dir)
        {
            Debug.Log("該目錄不存在!");
            return;
        }
        FileSystemInfo[] si = dir.GetFileSystemInfos();
        for (int i = 0; i < si.Length; i++)
        {
            FileInfo fi = si[i] as FileInfo;
            if(null!=fi&&IsImage(fi.Extension))
            {
                listStrFileName.Add(fi.FullName);
            }
            else
            {
            }
        }
    }

然后根據(jù)路徑列表使用WWW逐個加載圖片
[C#] 純文本查看 復(fù)制代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/// <summary>
   /// 根據(jù)文件路徑加載圖片
   /// </summary>
   private IEnumerator GetAllTexture()
   {
       curFilePath = GetFilePath();
       DirectoryInfo tempInfo = new DirectoryInfo(curFilePath);
       GetAllFile(tempInfo);
       foreach (string item in listStrFileName)
       {
           WWW www = new WWW("file://" + item);
           yield return www;
           //先得到最后的圖片文件名
           string[] tempAllFileName = item.Split(Path.DirectorySeparatorChar);
           //去掉后綴
           string tempStrKey = tempAllFileName[tempAllFileName.Length - 1];
           tempStrKey = tempStrKey.Substring(0, tempStrKey.Length - 4).Trim();
           if(null!=tempStrKey&&!dicTextures.ContainsKey(tempStrKey))
           {
               dicTextures.Add(tempStrKey, www.texture);
           }
           else
           {
               Debug.LogError("圖片文件名為空或者有相同的文件名!");
               continue;
           }
           if(dicSubTerrainMat.ContainsKey(tempStrKey))
           {
               dicSubTerrainMat[tempStrKey].SetTexture("_MainTex", www.texture);
           }
           else
           {
               Debug.LogError("文件名"+tempStrKey+"在材質(zhì)列表中沒有找到對應(yīng)的材質(zhì)名!");
           }
       }
       isLoadAllTexture = true;
       Debug.Log("Load All Terrain Texture!");
   }

效果圖如圖所示:一開始顯示的是默認(rèn)模糊處理的貼圖,在加載的過程中幀率非常低,造成卡頓。
二、IO文件流加載

       首先,同樣的是加載文件路徑。然后開啟另外一個線程將圖片的字節(jié)流加載到字典中。
[C#] 純文本查看 復(fù)制代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  /// <summary>
    /// 根據(jù)文件路徑讀取圖片并轉(zhuǎn)換成byte
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static byte[] ReadPictureByPath(string path)
    {
        FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
        fileStream.Seek(0, SeekOrigin.Begin);
        byte[] binary = new byte[fileStream.Length];
        fileStream.Read(binary, 0, (int)fileStream.Length);
        fileStream.Close();
        fileStream.Dispose();
        fileStream = null;
        return binary;
}
然后,加載整個文件列表里的圖片到字典緩存中。
    /// <summary>
    /// 根據(jù)文件路徑加載圖片
    /// </summary>
    private void GetAllTerrainTex()
    {
        DirectoryInfo tempInfo = new DirectoryInfo(curFilePath);
        GetAllFile(tempInfo);
        foreach (string item in listStrFileName)
        {
            byte[] tempImageBuffer = ReadPictureByPath(item);
            if(null==tempImageBuffer)
            {
                Debug.Log("讀取路徑"+item+"圖片失??!");
            }
            string[] tempAllFileName = item.Split(Path.DirectorySeparatorChar);
            //去掉后綴
            string tempStrKey = tempAllFileName[tempAllFileName.Length - 1];
            tempStrKey = tempStrKey.Substring(0, tempStrKey.Length - 4).Trim();
            if (null != tempStrKey && !dicImageBuffer.ContainsKey(tempStrKey))
            {
                dicImageBuffer.Add(tempStrKey, tempImageBuffer);
            }
            else
            {
                Debug.LogError("圖片文件名為空或者有相同的文件名!");
                continue;
            }
        }
        isLoadAllTexture = true;
        Debug.Log("Load All Terrain Texture!");}

最終的加載效果圖如圖所示:同樣會導(dǎo)致幀率很低造成卡頓,但是這種方式有一個好處是可以先將圖片使用其他線程加載到緩存中,造成卡頓是因?yàn)閷⒆止?jié)流轉(zhuǎn)成Texture2D

[C#] 純文本查看 復(fù)制代碼
1
Texture2D tempTex = new Texture2D(TERRAIN_MAP_DI, TERRAIN_MAP_DI);
             tempTex.LoadImage(item.Value);

這里的地形貼圖都很大,就算單張圖片壓縮到4M左右,使用LoadImage方法還是會卡頓。而這個方法又只能在主線程里使用,也即不能在新開的加載線程里使用。Unity對這類的類都限制了,只能在主線程使用。
三、Assetbundle打包再加載
   首先,要將貼圖導(dǎo)入到Unity工程里面,然后對一個文件夾里的貼圖進(jìn)行打包。.0之后的打包方式比較簡單,如圖所示:

先選中要打包的貼圖,在Inspectors面板下有個AssetBundle,這里新建你要打包成集合的名字,我以文件夾的名字新建了一個標(biāo)簽“40_73.4_36.69237_77.61875”。選中文件夾里其他圖片,將它們都設(shè)置成這個標(biāo)簽。這樣就可以將整個文件夾里的圖片打包成一個集合,方便一次性加載。

       打包的代碼和簡單,可以在Editor文件下,創(chuàng)建一個編輯腳本,代碼如下:
[C#] 純文本查看 復(fù)制代碼
1
2
3
4
5
  [MenuItem("Example/Build Asset Bundles")]
    static void BuildABs()
    {
        // Put the bundles in a folder called "ABs" within the Assets folder.
        BuildPipeline.BuildAssetBundles("Assets/Assetbundle", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows);
}

對比以前的方式,新的打包方法確實(shí)簡化方便了很多。接下來將打包好的Assetbundle文件夾放到工程的同級目錄下,加載Assetbuudle的代碼如下:
[C#] 純文本查看 復(fù)制代碼
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/// <summary>
    ///異步加載地形的貼圖
    /// </summary>
    /// <returns></returns>
    private IEnumerator SetAllTexAsy()
    {
        yield return null;
        var bundleLoadRequest = AssetBundle.LoadFromFileAsync(curFilePath);
        yield return bundleLoadRequest;
        var myLoadedAssetBundle = bundleLoadRequest.assetBundle;
        if (myLoadedAssetBundle == null)
        {
            Debug.Log("Failed to load AssetBundle!");
            yield break;
        }
        foreach (var item in dicSubTerrainMat)
        {
            var assetLoadRequest = myLoadedAssetBundle.LoadAssetAsync<Texture2D>(item.Key);
            yield return assetLoadRequest;
            Texture2D tempTex = assetLoadRequest.asset as Texture2D;
            if (null == tempTex)
            {
                Debug.Log(gameObject.name + "Assetbundle里沒有對應(yīng)" + item.Key + "的貼圖");
            }
            else
            {
                item.Value.SetTexture("_MainTex", tempTex);
            }
        }
        myLoadedAssetBundle.Unload(false);
}

加載得到集合包,然后根據(jù)每個圖片的文件名得到具體的圖片。最終的效果圖如圖所示:
可以看到幀率幾乎不受任何影響,但是會在加載到顯示的過程中停留2秒時間左右,對于程序的卡頓,這個時間的停滯還是可以接受的。
四、總結(jié)

雖然,第三種方式在加載的時候比較流暢,不會影響到主線程的執(zhí)行。但是,打包的過程也是一個很繁瑣,且需要細(xì)致認(rèn)真的工作??偟膩碚f第三種方式最好的,前面兩種方式應(yīng)該用在其他方面,幾天的研究算是作為一個記錄,或許以后就能用的上。最后附上三種方式的工程文件和貼圖,歡迎留言交流,工作一直很忙,對沒有看到留言并及時回復(fù)的童鞋表示抱歉。
haotian_01,如果您要查看本帖隱藏內(nèi)容請回復(fù)


本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Unity 3D加載外部資源
Unity WWW網(wǎng)絡(luò)動態(tài)加載和儲存在本地
[UE4]C++實(shí)現(xiàn)動態(tài)加載UObject:StaticLoadObject();以Texture和Material為例
Unity3d熱更新(三):更新資源流程
unity3d lightmap的assetbundle和動態(tài)載入
【厚積薄發(fā)】技術(shù)分享連載(二十一)|依賴打包 |拆分Alpha通道
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服