您的位置:首页 > 编程语言 > PHP开发

LoadAssetAtPath 与 Load 的区别

2014-11-14 19:55 155 查看
一、官方的文档

  

Resources.LoadAssetAtPath

Returns a resource at an asset path (Editor Only).
This function always return null in the standalone player or web player.
This is useful for quickly accessing an asset for use in the editor only

Resources.Load

Loads an asset stored at path in a Resources folder.
Returns the asset at path if it can be found otherwise returns null.
Only objects of type will be returned if this parameter is supplied.
The path is relative to any Resources folder inside the Assets folder of your project,
extensions must be omitted


上面已经解释的很清楚了,接下来我做个实验来验证一下。

二、试验代码

public enum UpDecorationEnum
{
Flower_Category1,
Olive_Category2,
Man_Category3,
Woman_Category4
}

void UnitTest()
{
Random_Sprite_SpecificEnum(UpDecorationEnum.Flower_Category1);
}

public static void Random_Sprite_SpecificEnum(UpDecorationEnum specific)
{
string DataPath = Application.dataPath
+ "/Resources/Environment/Line_Up/Up/" + specific.ToString();

DirectoryInfo directoryinfo = new DirectoryInfo(DataPath);

FileInfo[] fileinfo = directoryinfo.GetFiles("*.png");

List<Sprite> list = new List<Sprite>();
foreach (var item in fileinfo)
{
//Window Editor
// Flower@sprite.png
//Debug.Log(item.Name);

// E:\Unity3D Project\MenSa_Academy_2\Client\MenSa_Academy
// \Assets\Resources\Environment\Line_Up\Up\Flower_Category1\Flower@sprite.png
//Debug.Log(item.FullName);

// Assets\Resources\Environment\Line_Up\Up\Flower_Category1\Flower@sprite.png
//Debug.Log(DataPathToAssetPath(item.FullName));

// Environment/Line_Up/Up/Flower_Category1/Flower@sprite
// Debug.Log(DataPathToResourcesPath(item.FullName));

/// 这个在android 下 是 不行的 使用 DataPathToAssetPath 下面两种都是可以的
//  Assets\Resources\Environment\Line_Up\Up\Flower_Category1\Flower@sprite.png
//  Assets/Resources/Environment/Line_Up/Up/Flower_Category1/Flower@sprite.png
//Sprite tmp = Resources.LoadAssetAtPath<Sprite>(DataPathToAssetPath(item.FullName));

/// 全平台适用 只能使用 DataPathToResourcesPath1路径
Sprite tmp = Resources.Load<Sprite>(DataPathToResourcesPath(item.FullName));

if (tmp == null) Debug.Log("Null");
else list.Add(tmp);
}
Debug.Log("Random_Sprite_SpecificEnum" + list.Count);
}

public static string DataPathToAssetPath(string path)
{
if (Application.platform == RuntimePlatform.WindowsEditor)
return path.Substring(path.IndexOf("Assets\\"));
else
return path.Substring(path.IndexOf("Assets/"));
}

public static string DataPathToResourcesPath(string path)
{
// E:\Unity3D Project\MenSa_Academy_2\Client\MenSa_Academy
// \Assets\Resources\Environment\Line_Up\Up\Flower_Category1\Flower@sprite.png
if (Application.platform == RuntimePlatform.WindowsEditor){
// Environment\Line_Up\Up\Flower_Category1\Flower@sprite.png
string result =  path.Substring(path.IndexOf("Environment\\"));

// Environment/Line_Up/Up/Flower_Category1/Flower@sprite.png
string result1 =  result.Replace("\\","/");

// Environment/Line_Up/Up/Flower_Category1/Flower@sprite
int index = result1.LastIndexOf('.');
string result2 = result1.Substring(0, index);

return result2;

}

else
return path.Substring(path.IndexOf("Environment/"));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: