예제 #1
0
 public void AddAssetBundleToDic(AssetBundlePackage bundle)
 {
     if (!allAssetBundleDic.ContainsKey(bundle.name))
     {
         allAssetBundleDic.Add(bundle.name, bundle);
     }
 }
예제 #2
0
        private IEnumerator m_LoadAssetBundleFromFileAsync(string assetBundleName)
        {
            string[] list = Instance.GetAssetBundleDependencies(assetBundleName);
            if (list.Length != 0)
            {
                for (int i = 0; i < list.Length; i++)
                {
                    yield return(StartCoroutine(m_LoadAssetBundleFromFileAsync(list[i])));
                }
            }

            if (!allAssetBundleDic.ContainsKey(assetBundleName))
            {
                AssetBundleCreateRequest request = AssetBundle.LoadFromFileAsync(AutoGetResourcePath(assetBundleName, false));
                //HFLog.L("异步加载AssetBundle   " + assetBundleName);
                yield return(request);

                if (!allAssetBundleDic.ContainsKey(assetBundleName))
                {
                    AssetBundle        bundle         = request.assetBundle;
                    AssetBundlePackage tmpAssetBundle = new AssetBundlePackage(bundle, assetBundleName);
                    AddAssetBundleToDic(tmpAssetBundle);
                }
            }
            else
            {
                AssetBundlePackage ab = allAssetBundleDic[assetBundleName];
                ab.Retain();
                //HFLog.L("异步 通过缓存加载");
            }
        }
예제 #3
0
        /// <summary>
        ///  一般来说,尽可能使用AssetBundle.LoadFromFile。该API在速度,磁盘使用率和运行时内存使用方面是最有效的
        /// </summary>
        public AssetBundlePackage LoadAssetBundleFromFile(string assetBundleName)
        {
            assetBundleName = assetBundleName.ToLower();
            string[] list = Instance.GetAssetBundleDependencies(assetBundleName);
            if (list.Length != 0)
            {
                for (int i = 0; i < list.Length; i++)
                {
                    LoadAssetBundleFromFile(list[i]);
                }
            }

            if (!allAssetBundleDic.ContainsKey(assetBundleName))
            {
                AssetBundle        bundle         = AssetBundle.LoadFromFile(AutoGetResourcePath(assetBundleName, false));
                AssetBundlePackage tmpAssetBundle = new AssetBundlePackage(bundle, assetBundleName);
                AddAssetBundleToDic(tmpAssetBundle);
                //HFLog.L("同步加载AssetBundle   " + assetBundleName);
                return(tmpAssetBundle);
            }
            else
            {
                AssetBundlePackage ab = allAssetBundleDic[assetBundleName];
                ab.Retain();
                return(ab);
            }
        }
예제 #4
0
        /// <summary>
        ///  使用这个方法  如果在编辑器首先需要在 EditorSetting 里把 SpritePacker 设置成Always Enabled 在运行之前build一下图集 才能显示
        /// </summary>
        /// <param name="packageName">assetbundle名字</param>
        /// <param name="atlasName">图集名字</param>
        /// <param name="spriteName">图片名字</param>
        /// <returns></returns>
        public Sprite GetSpriteByAtlas(string packageName, string atlasName, string spriteName)
        {
            AssetBundlePackage ab    = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            SpriteAtlas        atlas = ab.LoadAssetWithCache <SpriteAtlas>(atlasName);

            return(atlas.GetSprite(spriteName));
        }
예제 #5
0
        /// <summary>
        ///  获取资源 在callback 之后自动卸载assetbundle 并且销毁对应的prefab
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="assetName"></param>
        /// <param name="autoKillPrefab"></param>
        /// <param name="callBack"></param>
        public void LoadPrefabWithAutoKill(string packageName, string assetName, Action <GameObject> callback)
        {
            AssetBundlePackage ab     = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            GameObject         prefab = ab.assetBundle.LoadAsset <GameObject>(assetName);

            callback(prefab);
            UnloadAssetBundle(ab, false);
        }
예제 #6
0
        /// <summary>
        ///  获取资源 在callback 之后自动卸载assetbundle
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="packageName"></param>
        /// <param name="assetName"></param>
        /// <param name="callBack"></param>
        public void LoadAssetWithAutoKill <T>(string packageName, string assetName, Action <T> callback) where T : UnityEngine.Object
        {
            AssetBundlePackage ab = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            T ta = ab.assetBundle.LoadAsset <T>(assetName);

            callback(ta);
            UnloadAssetBundle(ab, false);
        }
예제 #7
0
 public void UnloadAssetBundle(AssetBundlePackage bundle, bool b = true)
 {
     if (bundle != null && !string.IsNullOrEmpty(bundle.name))
     {
         //HFLog.L("卸载Assetbundle  " + bundle.name);
         allAssetBundleDic.Remove(bundle.name);
         bundle.Unload(b);
     }
 }
예제 #8
0
        /// <summary>
        ///  通用的以同步方式获取一个资源  封装了编辑器 和 ab 两种加载方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="packageName"></param>
        /// <param name="assetName"></param>
        /// <returns></returns>
        public T GetAsset <T>(string packageName, string assetName) where T : UnityEngine.Object
        {
#if UNITY_EDITOR
            return(EditorLoadAsset <T>(packageName, assetName));
#else
            AssetBundlePackage ab = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            return(ab.LoadAssetWithCache <T>(assetName));
#endif
        }
예제 #9
0
        /// <summary>
        /// 获取 Sprite 通过缓存 不会自动卸载
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public Sprite GetSprite(string packageName, string assetName)
        {
#if UNITY_EDITOR
            return(EditorLoadAsset <Sprite>(packageName, assetName));
#else
            AssetBundlePackage ab = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            Sprite             sp = ab.LoadAssetWithCache <Sprite>(assetName);
            return(sp);
#endif
        }
예제 #10
0
        /// <summary>
        /// 获取 GameObject 通过缓存 不会自动销毁
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public GameObject GetPrefab(string packageName, string assetName)
        {
#if UNITY_EDITOR
            return(EditorLoadAsset <GameObject>(packageName, assetName));
#else
            AssetBundlePackage ab = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            GameObject         g  = ab.LoadAssetWithCache <GameObject>(assetName);
            return(g);
#endif
        }
예제 #11
0
        public void AnalysisConfig()
        {
            AssetBundlePackage pp = HFResourceManager.Instance.LoadAssetBundleFromFile("Common_Config");
            TextAsset          tt = pp.LoadAssetWithCache <TextAsset>("SpineExchangeConfig");
            SpineEquipment     ex = JsonMapper.ToObject <SpineEquipment>(tt.text);

            for (int i = 0; i < ex.Equipment.Count; i++)
            {
                EquipmentItem item = ex.Equipment[i];
                equipmentInfoDic.Add(item.ID, item);
            }
        }
예제 #12
0
        private IEnumerator m_LoadAssetBundleFromFileAsync(string assetBundleName, Action <AssetBundlePackage> finishCallback)
        {
            assetBundleName = assetBundleName.ToLower();
            yield return(StartCoroutine(m_LoadAssetBundleFromFileAsync(assetBundleName)));

            if (finishCallback != null)
            {
                AssetBundlePackage ab = allAssetBundleDic[assetBundleName];
                ab.Retain();
                finishCallback(ab);
            }
        }
예제 #13
0
        /// <summary>
        ///  卸载某一个 assetbundle 通过名字
        /// </summary>
        /// <param name="name"></param>
        /// <param name="b">是否卸载压出来的的东西</param>
        public void UnloadAssetBundle(string name, bool b = true)
        {
            name = name.ToLower();
            AssetBundlePackage bundle = GetAssetBundle(name);

            if (bundle != null)
            {
                //HFLog.L("卸载Assetbundle  " + bundle.name);
                RecursionReleaseAssetBundle(bundle.name);
                bundle.Unload(b);
                allAssetBundleDic.Remove(name);
            }
        }
예제 #14
0
        /// <summary>
        /// 获取shader
        /// </summary>
        /// <param name="packageName"></param>
        /// <param name="abName"></param>
        /// <returns></returns>
        public Shader GetShader(string packageName, string assetName)
        {
#if UNITY_EDITOR
            Shader shader = EditorLoadAsset <Shader>(packageName, assetName);
            Shader.WarmupAllShaders();
            return(shader);
#else
            AssetBundlePackage ab     = HFResourceManager.Instance.LoadAssetBundleFromFile(packageName);
            Shader             shader = ab.LoadAssetWithCache <Shader>(assetName);
            Shader.WarmupAllShaders();
            return(shader);
#endif
        }
예제 #15
0
        /// <summary>
        /// 递归 Release AssetBundle
        /// </summary>
        /// <param name="name"></param>
        public void RecursionReleaseAssetBundle(string name)
        {
            AssetBundlePackage bundle = GetAssetBundle(name);

            if (bundle != null)
            {
                bundle.Release();
            }
            string[] list = Instance.GetAssetBundleDependencies(name);
            for (int i = 0; i < list.Length; i++)
            {
                RecursionReleaseAssetBundle(list[i]);
            }
        }
예제 #16
0
        void Awake()
        {
            Image image = GetComponent <Image>();

            Image[] images = GetComponentsInChildren <Image>();
            imageList = new List <Image>(images);
            if (image != null)
            {
                imageList.Add(image);
            }

            Button button = GetComponent <Button>();

            Button[] buttons = GetComponentsInChildren <Button>();
            buttonList = new List <Button>(buttons);
            if (button != null)
            {
                buttonList.Add(button);
            }

            Outline outline = GetComponent <Outline>();

            Outline[] outlines = GetComponentsInChildren <Outline>();
            outLineList = new List <Outline>(outlines);
            if (outline != null)
            {
                outLineList.Add(outline);
            }

            Text text = GetComponent <Text>();

            Text[] texts = GetComponentsInChildren <Text>();
            textList = new List <Text>(texts);
            if (text != null)
            {
                textList.Add(text);
            }

            foreach (var item in textList)
            {
                textColorList.Add(item.color);
            }

            AssetBundlePackage ab = HFResourceManager.Instance.LoadAssetBundleFromFile(assetBundleName);

            grayMaterial = ab.LoadAssetWithCache <Material>(assetName);
        }
예제 #17
0
 public void LoadHotFixAssembly(string assetbundleName, string dllName, Action <byte[]> callback)
 {
     if (GameEnvironment.Instance.RuntimeEnvironment == GameEnvironmentType.Develop)
     {
         StartCoroutine(m_EditorLoadHotFixAssembly(assetbundleName, dllName, callback));
     }
     else
     {
         AssetBundlePackage ab   = HFResourceManager.Instance.LoadAssetBundleFromFile(assetbundleName);
         TextAsset          text = ab.LoadAssetWithCache <TextAsset>(dllName + ".dll");
         if (callback != null)
         {
             callback(text.bytes);
         }
         UnloadAssetBundle(ab, true);
     }
 }
예제 #18
0
        public void ExchangeEquipmentAndMergeUI(SkeletonGraphic skeletonAnimation, List <EquipmentItem> infos)
        {
            for (int i = 0; i < infos.Count; i++)
            {
                EquipmentItem      info               = infos[i];
                string             regionName         = info.equipmentImageName;
                string             defaultSkinName    = info.defaultSkinName;
                string             spineEquipmentType = info.spineEquipmentTypeName;
                AssetBundlePackage assetbundle        = HFResourceManager.Instance.LoadAssetBundleFromFile(info.equipmentAssetbundleName);
                AtlasAsset         atlasAsset         = assetbundle.LoadAssetWithCache <AtlasAsset>(info.equipmentAtlasAssetName);
                float       scale              = skeletonAnimation.skeletonDataAsset.scale;
                Atlas       atlas              = atlasAsset.GetAtlas();
                AtlasRegion region             = atlas.FindRegion(regionName);
                Slot        slot               = skeletonAnimation.Skeleton.FindSlot(info.slotName);
                Attachment  originalAttachment = slot.Attachment;
                if (region == null)
                {
                    HFLog.C("没有找到图集里的 : 图片  " + regionName);
                    slot.Attachment = null;
                }
                else if (originalAttachment != null)
                {
                    slot.Attachment = originalAttachment.GetRemappedClone(region, true, true, 1);
                }
                else
                {
                    var newRegionAttachment = region.ToRegionAttachment(region.name, scale);
                    slot.Attachment = newRegionAttachment;
                }
                slot.Skeleton.Skin.SetAttachment(slot.Data.Index, info.slotPlaceholderName, slot.Attachment);
            }

            Skin repackedSkin = new Skin(RepackConst);

            repackedSkin.Append(skeletonAnimation.Skeleton.Data.DefaultSkin); // Include the "default" skin. (everything outside of skin placeholders)
            repackedSkin.Append(skeletonAnimation.Skeleton.Skin);             // Include your new custom skin.
            Texture2D runtimeAtlas    = null;
            Material  runtimeMaterial = null;

            repackedSkin = repackedSkin.GetRepackedSkin(RepackConst, skeletonAnimation.SkeletonDataAsset.atlasAssets[0].materials[0], out runtimeMaterial, out runtimeAtlas); // Pack all the items in the skin.
            skeletonAnimation.Skeleton.SetSkin(repackedSkin);                                                                                                                 // Assign the repacked skin to your Skeleton.
            skeletonAnimation.Skeleton.SetSlotsToSetupPose();                                                                                                                 // Use the pose from setup pose.
            skeletonAnimation.Update(0);                                                                                                                                      // Use the pose in the currently active animation.
            skeletonAnimation.OverrideTexture = runtimeAtlas;
        }
예제 #19
0
        /// <summary>
        ///  加载一系列 的assetbundle
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List <AssetBundlePackage> LoadAssetsBundlesFromFile(string[] list, Action <float> progressCallback)
        {
            List <AssetBundlePackage> bundles = new List <AssetBundlePackage>();

            for (int i = 0; i < list.Length; i++)
            {
                AssetBundlePackage bundle = LoadAssetBundleFromFile(list[i]);
                if (bundle != null)
                {
                    bundles.Add(bundle);
                }
                if (progressCallback != null)
                {
                    progressCallback((bundles.Count + 0.0f) / list.Length);
                }
            }
            return(bundles);
        }
예제 #20
0
        private IEnumerator m_LoadAssetsBundlesFromFileAsync(string[] list, Action <float> progressCallback)
        {
            List <AssetBundlePackage> bundles = new List <AssetBundlePackage>();

            for (int i = 0; i < list.Length; i++)
            {
                AssetBundlePackage bundle = LoadAssetBundleFromFile(list[i]);
                if (bundle != null)
                {
                    bundles.Add(bundle);
                }
                if (progressCallback != null)
                {
                    progressCallback((bundles.Count + 0.0f) / list.Length);
                }
                yield return(null);
            }
        }
예제 #21
0
        /// <summary>
        ///  替换spine部件 但是不合并材质球
        /// </summary>
        /// <param name="skeletonAnimation"></param>
        /// <param name="item"></param>
        public void ExchangeEquipment(SkeletonAnimation skeletonAnimation, EquipmentItem item)
        {
            AssetBundlePackage pg = HFResourceManager.Instance.LoadAssetBundleFromFile(item.equipmentAssetbundleName);
            Texture2D          equipmentTexture = pg.LoadAssetWithCache <Texture2D>(item.equipmentImageName);
            // 找到具体的一个 插槽
            Slot slot = skeletonAnimation.skeleton.FindSlot(item.slotName);
            // 通过插槽和一张图片创建对应插槽下的附件
            Attachment attachment = null;

            if (item.attachmentType == (int)AttachmentType.Region)
            {
                attachment = CreateRegionAttachmentByTexture(slot, equipmentTexture);
            }
            else if (item.attachmentType == (int)AttachmentType.Mesh)
            {
                attachment = CreateMeshAttachmentByTexture(slot, equipmentTexture);
            }

            // 设置新的附件到插槽
            slot.Skeleton.Skin.SetAttachment(slot.Data.index, item.slotPlaceholderName, attachment);
        }