Unity编辑器下,通过资源名加载资源的一种方案

接上篇:https://www.jianshu.com/p/88fde844efb7

在编辑器下,通过资源名来加载资源

限制:资源名在项目中不能有重复的
using System.Collections.Generic;
using System.IO;
using UnityEditor;

public class AssetMgrEditor : Singleton<AssetMgrEditor>
{
    Dictionary<string, string> asset_name_path = new Dictionary<string, string>();

    public void Init()
    {
        List<AssetBundleBuild> AssetBundleLists = AssetBundleCollectEditor.GetAssetBundleBuilds_UI();
        for (int i = 0; i < AssetBundleLists.Count; i++)
        {
            AssetBundleBuild assetBundle = AssetBundleLists[i];
            for (int s = 0; s < assetBundle.assetNames.Length; s++)
            {
                string path = assetBundle.assetNames[s];
                string name = Path.GetFileName(path);
                asset_name_path.Add(name, path);
            }
        }
    }
    /// <summary>
    /// 通过资源名获取资源路径
    /// </summary>
    /// <param name="assetName"></param>
    /// <returns></returns>
    public string GetAssetPath(string assetName)
    {
        string path = "";
        if (asset_name_path.TryGetValue(assetName, out path))
        {
            return path;
        }
        return null;
    }
}

用法
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public class LoadUIScript : MonoBehaviour
{
    public Transform Parent;

    public Image showImage1;
    public Image showImage2;
    public Image showImage3;

    void Start()
    {


        //string assetPath = @"E:\UnityProject\AssetBundles";

        //////先加载 Manifest 方便获取依赖关系,一般正式项目会自己保存ab包的依赖关系Singleton
        //AssetBundle manifestAB = AssetBundle.LoadFromFile(Path.Combine(assetPath, "AssetBundles"));
        //AssetBundleManifest assetBundleManifest = manifestAB.LoadAsset<AssetBundleManifest>("AssetBundleManifest");

        ////通过打包时的assetBundleName来获取这个ab包的依赖
        //string[] strs = assetBundleManifest.GetAllDependencies("assets/ui/modules/bag/ui_testpanel1.unity3d");
        //foreach (string name in strs)
        //{
        //    //加载依赖,实际需要把这些加载的依赖都保存起来
        //    AssetBundle dep = AssetBundle.LoadFromFile(Path.Combine(assetPath, name));
        //    Debug.Log("依赖加载:" + name);
        //}
        ////加载ab包
        //AssetBundle panelAB = AssetBundle.LoadFromFile(assetPath + "/assets/ui/modules/bag/ui_testpanel1.unity3d");
        ////加载ab包中的预制体
        //GameObject wallPrefab = panelAB.LoadAsset<GameObject>("UI_testPanel1");
        //var obj = Instantiate(wallPrefab);
        //obj.transform.SetParent(Parent);
        //obj.transform.localScale = Vector3.one;
        //obj.transform.localPosition = Vector3.zero;



        //加载图集并获取图集中的图片
        //一下两种效果一样

        //AssetBundle atlas_ab = AssetBundle.LoadFromFile(Path.Combine(assetPath, "assets/ui/modules/bag/atlas_bag_bg.unity3d"));
        //Sprite sprite1 = atlas_ab.LoadAsset<Sprite>("bg_blue");
        //showImage1.sprite = sprite1;
        //Sprite sprite2 = atlas_ab.LoadAsset<Sprite>("bg_white");
        //showImage2.sprite = sprite2;
        //Sprite sprite3 = atlas_ab.LoadAsset<Sprite>("img_check");
        //showImage3.sprite = sprite3;


        //如果把图集也打进AB包中,可以这样加载
        //SpriteAtlas spriteAtlas = atlas_ab.LoadAsset<SpriteAtlas>("Atlas_Bag_bg");
        //Sprite sprite1 = spriteAtlas.GetSprite("bg_blue");
        //showImage1.sprite = sprite1;
        //Sprite sprite2 = spriteAtlas.GetSprite("bg_white");
        //showImage2.sprite = sprite2;
        //Sprite sprite3 = spriteAtlas.GetSprite("img_check");
        //showImage3.sprite = sprite3;



        AssetMgrEditor.instance.Init();

        GameObject go = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(AssetMgrEditor.instance.GetAssetPath("UI_testPanel1.prefab"));
        var obj = Instantiate(go);
        obj.transform.SetParent(Parent);
        obj.transform.localScale = Vector3.one;
        obj.transform.localPosition = Vector3.zero;

        Sprite sprite = UnityEditor.AssetDatabase.LoadAssetAtPath<Sprite>(AssetMgrEditor.instance.GetAssetPath("bg_blue.png"));
        showImage1.sprite = sprite;
    }


}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容