• LoadAssetAtPath 与 Load 的区别


    一、官方的文档 

      

    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
    

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

    二、试验代码

     1 public enum UpDecorationEnum
     2 {
     3     Flower_Category1,
     4     Olive_Category2,
     5     Man_Category3,
     6     Woman_Category4
     7 }
     8 
     9 void UnitTest()
    10 {
    11     Random_Sprite_SpecificEnum(UpDecorationEnum.Flower_Category1);
    12 }
    13 
    14 public static void Random_Sprite_SpecificEnum(UpDecorationEnum specific)
    15 {
    16     string DataPath = Application.dataPath
    17     + "/Resources/Environment/Line_Up/Up/" + specific.ToString();
    18 
    19     DirectoryInfo directoryinfo = new DirectoryInfo(DataPath);
    20 
    21     FileInfo[] fileinfo = directoryinfo.GetFiles("*.png");
    22 
    23     List<Sprite> list = new List<Sprite>();
    24     foreach (var item in fileinfo)
    25     {
    26         //Window Editor
    27         // Flower@sprite.png
    28         //Debug.Log(item.Name);
    29         
    30         // E:Unity3D ProjectMenSa_Academy_2ClientMenSa_Academy
    31         // AssetsResourcesEnvironmentLine_UpUpFlower_Category1Flower@sprite.png
    32         //Debug.Log(item.FullName);
    33         
    34         // AssetsResourcesEnvironmentLine_UpUpFlower_Category1Flower@sprite.png
    35         //Debug.Log(DataPathToAssetPath(item.FullName));
    36         
    37         
    38         // Environment/Line_Up/Up/Flower_Category1/Flower@sprite
    39         // Debug.Log(DataPathToResourcesPath(item.FullName));
    40         
    41 
    42         /// 这个在android 下 是 不行的 使用 DataPathToAssetPath 下面两种都是可以的
    43         //  AssetsResourcesEnvironmentLine_UpUpFlower_Category1Flower@sprite.png
    44         //  Assets/Resources/Environment/Line_Up/Up/Flower_Category1/Flower@sprite.png
    45         //Sprite tmp = Resources.LoadAssetAtPath<Sprite>(DataPathToAssetPath(item.FullName));
    46 
    47         /// 全平台适用 只能使用 DataPathToResourcesPath1路径
    48         Sprite tmp = Resources.Load<Sprite>(DataPathToResourcesPath(item.FullName)); 
    49         
    50         if (tmp == null) Debug.Log("Null");
    51         else list.Add(tmp);
    52     }
    53     Debug.Log("Random_Sprite_SpecificEnum" + list.Count);
    54 }
    55 
    56 public static string DataPathToAssetPath(string path)
    57 {
    58     if (Application.platform == RuntimePlatform.WindowsEditor)
    59         return path.Substring(path.IndexOf("Assets\"));
    60     else
    61         return path.Substring(path.IndexOf("Assets/"));
    62 }
    63 
    64 public static string DataPathToResourcesPath(string path)
    65 {
    66     // E:Unity3D ProjectMenSa_Academy_2ClientMenSa_Academy
    67     // AssetsResourcesEnvironmentLine_UpUpFlower_Category1Flower@sprite.png
    68     if (Application.platform == RuntimePlatform.WindowsEditor){
    69         // EnvironmentLine_UpUpFlower_Category1Flower@sprite.png
    70         string result =  path.Substring(path.IndexOf("Environment\"));
    71         
    72         // Environment/Line_Up/Up/Flower_Category1/Flower@sprite.png
    73         string result1 =  result.Replace("\","/");
    74 
    75         // Environment/Line_Up/Up/Flower_Category1/Flower@sprite
    76         int index = result1.LastIndexOf('.');
    77         string result2 = result1.Substring(0, index);
    78         
    79         return result2;
    80 
    81     }
    82         
    83     else
    84         return path.Substring(path.IndexOf("Environment/"));
    85 }
  • 相关阅读:
    【缺少kubernetes权限】 namespaces "xxx" is forbidden: User "xxx" cannot xxx resource "xxx" in API group "xxx" in the namespace "xxx"
    【kubernetes secret 和 aws ecr helper】kubernetes从docker拉取image,kubernetes docker私服认证(argo docker私服认证),no basic auth credentials错误解决
    win10老提示系统错误,要注销
    win10无法访问局域网共享文件?解决如此简单。。。。。
    filezilla server老提示connect server
    代理ARP
    win10用filezilla server搭建ftp服务器一直无法访问
    华为路由器GRE配置
    spring笔记--使用springAPI以及自定义类 实现AOP的一个例子
    spring笔记--依赖注入之针对不同类型变量的几种注入方式
  • 原文地址:https://www.cnblogs.com/chongxin/p/4098055.html
Copyright © 2020-2023  润新知