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
|
///
/// 获取当前物体应该读取的地形贴图的文件的路径
///
///
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 +
"/"
;
//最后加上当前文件夹的名称,最后的文件夹名称和当前物体的名称一致
strPath += gameObject.name +
"/"
;
return
strPath;
}
///
/// 获取所有地图贴图文件路径
///
///
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
|
///
/// 根据文件路径加载图片
///
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+
"在材质列表中没有找到对应的材质名!"
);
}
}
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
|
///
/// 根据文件路径读取图片并转换成byte
///
///
///
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;
}
然后,加载整个文件列表里的图片到字典缓存中。
///
/// 根据文件路径加载图片
///
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
|
///
///异步加载地形的贴图
///
///
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
yield
return
assetLoadRequest;
Texture2D tempTex = assetLoadRequest.asset
as
Texture2D;
if
(
null
== tempTex)
{
Debug.Log(gameObject.name +
"Assetbundle里没有对应"
+ item.Key +
"的贴图"
);
}
else
{
item.Value.SetTexture(
"_MainTex"
, tempTex);
}
}
myLoadedAssetBundle.Unload(
false
);
}
|