在编辑器下,通过资源名来加载资源
限制:资源名在项目中不能有重复的
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;
}
}