Exemple #1
0
        // 为了模拟Resources.Load不输入扩展名,折腾下
        public static void Load(string name, Type type, Action <UnityEngine.Object> cb = null)
        {
#if UNITY_EDITOR
            Dictionary <string, bool> spriteDic = EditLoadCheck.GetSpriteDic();


            // 拆分路径名和文件名
            string file = Path.GetFileNameWithoutExtension(name);
            string path = Path.GetDirectoryName(name);
            // 添加资源路径
            string updata_path = "Assets/res_updata/" + path;
            path = "Assets/res/" + path;

            // 去掉路径最后的路径分隔符
            path = path.TrimEnd(new char[2] {
                '/', '\\'
            });
            updata_path = updata_path.TrimEnd(new char[2] {
                '/', '\\'
            });
            UnityEngine.Object obj = null;

            string[] assets = UnityEditor.AssetDatabase.FindAssets(file);

            for (int i = 0; i < assets.Length; ++i)
            {
                string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(assets[i]);

                path        = path.Replace('/', '\\');
                updata_path = updata_path.Replace('/', '\\');

                if (Path.GetFileNameWithoutExtension(assetPath) != file || (path != Path.GetDirectoryName(assetPath) && updata_path != Path.GetDirectoryName(assetPath)))
                {
                    continue;
                }

                obj = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, type);
                if (obj != null)
                {
                    UnityEditor.AssetImporter ai = UnityEditor.AssetImporter.GetAtPath(assetPath);
                    if (ai != null && !spriteDic.ContainsKey(ai.assetBundleName) && obj.GetType() == typeof(Sprite))
                    {
                        Debug.LogErrorFormat("Fond Sprite[{0}] but not set ab name[{1}]", assetPath, ai.assetBundleName);
                        obj = null;
                        break;
                    }
                    break;
                }
            }
            cb(obj);
            if (obj == null)
            {
                Debug.LogError("No " + name);
            }
#else
            Debug.LogWarning("Flz.Core.Core.Load only in Editor mode");
            return;
#endif
        }
        private static List <AssetBundleBuild> get_Sprite_list()
        {
            string[] sprites = GetFileListByType("Sprite", new string[1] {
                "Assets/res"
            });
            List <AssetBundleBuild> abbList = new List <AssetBundleBuild>();
            // 找到指定为load的图片
            Dictionary <string, List <string> > spriteDic = new Dictionary <string, List <string> >();
            //spriteDic.Add("load", new List<string>());


            Dictionary <string, bool> dic = EditLoadCheck.GetSpriteDic();

            foreach (var k in dic.Keys)
            {
                spriteDic.Add(k, new List <string>());
            }

            foreach (var sf in sprites)
            {
                AssetImporter ai = AssetImporter.GetAtPath(sf);
                if (spriteDic.ContainsKey(ai.assetBundleName))
                {
                    spriteDic[ai.assetBundleName].Add(sf);
                }
            }

            string[] updata_sprites = GetFileListByType("Sprite", new string[1] {
                "Assets/res_updata"
            });
            foreach (var sf in updata_sprites)
            {
                AssetImporter ai = AssetImporter.GetAtPath(sf);
                string        assetBundleName = "updata_" + ai.assetBundleName;
                if (spriteDic.ContainsKey(assetBundleName))
                {
                    spriteDic[assetBundleName].Add(sf);
                }
                else
                {
                    if (spriteDic.ContainsKey(ai.assetBundleName))
                    {
                        spriteDic.Add(assetBundleName, new List <string>());
                        spriteDic[assetBundleName].Add(sf);
                    }
                }
            }

            foreach (var item in spriteDic)
            {
                Console.WriteLine(item.Key + item.Value);
                Debug.LogFormat("{0} need dynamic load Count {1}", item.Key, item.Value.Count);
                if (item.Value.Count > 0)
                {
                    AssetBundleBuild abbLoadSprite = new AssetBundleBuild();
                    if (item.Key == "load")
                    {
                        abbLoadSprite.assetBundleName = "sprite_load.ab";
                    }
                    else
                    {
                        abbLoadSprite.assetBundleName = item.Key + ".ab";
                    }
                    abbLoadSprite.assetNames = item.Value.ToArray();
                    abbList.Add(abbLoadSprite);
                }
            }

            return(abbList);
        }