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 { } } } |
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!" ); } |
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!" );} |
1 | Texture2D tempTex = new Texture2D(TERRAIN_MAP_DI, TERRAIN_MAP_DI); tempTex.LoadImage(item.Value); |
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); } |
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 ); } |