/**
     * \brief 封装过的加载道具用的接口,配合slot的概念和item这个struct控制多个不同类型的道具的加载卸载
     * \param item 要加载的道具的item,封装过的道具信息集合,方便业务逻辑,详见itemconfig
     * \param slotid  道具要加载的位置(slot),默认值为0,即slot数组的第0位
     * \param cb  加载道具完毕后会自动执行的回调,可以为空
     * \return 无
     */
    public IEnumerator LoadItem(Item item, int slotid = 0, LoadItemCallback cb = null)
    {
        if (LoadingItem == false && item.fullname != null && item.fullname.Length != 0 && slotid >= 0 && slotid < SLOTLENGTH)
        {
            LoadingItem = true;
            int tempslot = GetSlotIDbyName(item.name);
            if (tempslot < 0)   //如果尚未载入道具数据
            {
                var bundledata = Resources.LoadAsync <TextAsset>(item.fullname);
                yield return(bundledata);

                var    data         = bundledata.asset as TextAsset;
                byte[] bundle_bytes = data != null ? data.bytes : null;
                Debug.LogFormat("bundledata name:{0}, size:{1}", item.name, bundle_bytes.Length);
                GCHandle hObject = GCHandle.Alloc(bundle_bytes, GCHandleType.Pinned);
                IntPtr   pObject = hObject.AddrOfPinnedObject();
                var      itemid  = FaceunityWorker.fu_CreateItemFromPackage(pObject, bundle_bytes.Length);
                hObject.Free();

                UnLoadItem(slotid); //卸载上一个在这个slot槽内的道具

                itemid_tosdk[slotid]    = itemid;
                slot_items[slotid].id   = itemid;
                slot_items[slotid].name = item.name;
                slot_items[slotid].item = item;

                FaceunityWorker.fu_setItemIds(p_itemsid, SLOTLENGTH, IntPtr.Zero);
                Debug.Log("载入Item:" + item.name + " @slotid=" + slotid);
            }
            else if (tempslot != slotid)    //道具已载入,但是不在请求的slot槽内
            {
                UnLoadItem(slotid);

                itemid_tosdk[slotid] = slot_items[tempslot].id;
                slot_items[slotid]   = slot_items[tempslot];

                itemid_tosdk[tempslot] = 0;
                slot_items[tempslot].Reset();

                FaceunityWorker.fu_setItemIds(p_itemsid, SLOTLENGTH, IntPtr.Zero);
                Debug.Log("移动Item:" + item.name + " from tempslot=" + tempslot + " to slotid=" + slotid);
            }
            else    //tempslot == slotid 即重复载入同一个道具进同一个slot槽,直接跳过
            {
                Debug.Log("重复载入Item:" + item.name + "  slotid=" + slotid);
            }

            SetItemMirror(slotid);

            if (cb != null)
            {
                cb(item);//触发载入道具完成事件
            }
            LoadingItem = false;
        }
    }
 /// <summary>
 /// 封装过的加载道具用的接口,配合slot的概念和item这个struct控制多个不同类型的道具的加载卸载
 /// </summary>
 /// <param name="item">要加载的道具的item,封装过的道具信息集合,方便业务逻辑,详见itemconfig</param>
 /// <param name="slotid">道具要加载的位置(slot),默认值为0,即slot数组的第0位</param>
 /// <param name="cb">加载道具完毕后会自动执行的回调,可以为空</param>
 /// <returns>无</returns>
 public IEnumerator LoadItem(Item item, int slotid = 0, LoadItemCallback cb = null)
 {
     if (FaceunityWorker.fuIsLibraryInit() == 0)
     {
         yield break;
     }
     if (LoadingItem == false && item.fullname != null && item.fullname.Length != 0 && slotid >= 0 && slotid < slot_length)
     {
         LoadingItem = true;
         int tempslot = GetSlotIDbyName(item.name);
         if (tempslot < 0)                                                     //如果尚未载入道具数据
         {
             string bundle = Util.GetStreamingAssetsPath() + FuConst.AITYPE_PATH_ROOT + item.fullname + ".bundle";
             yield return(LoadItem(item, bundle, slotid));
         }
         else if (tempslot != slotid)                                          //道具已载入,但是不在请求的slot槽内
         {
             UnLoadItem(slotid);
             itemid_tosdk[slotid]   = slot_items[tempslot].id;
             slot_items[slotid]     = slot_items[tempslot];
             itemid_tosdk[tempslot] = 0;
             slot_items[tempslot].Reset();
             FaceunityWorker.fu_SetItemIds(itemid_tosdk, itemid_tosdk.Length, null);
         }
         else                                                                  //tempslot == slotid 即重复载入同一个道具进同一个slot槽,直接跳过
         {
             Debug.Log("重复载入Item:" + item.name + "  slotid=" + slotid);
         }
         SelfAdJustItemMirror(slotid);
         if (cb != null)
         {
             cb(item);                                                         //触发载入道具完成事件
         }
         LoadingItem = false;
     }
 }
Exemple #3
0
    public IEnumerator LoadItem(Item item, LoadItemCallback cb = null)
    {
        if (LoadingItem == false && item.fullname != null && item.fullname.Length != 0)
        {
            LoadingItem = true;
            if (!string.Equals(currentItem, item.name) && !string.Equals(beautyitem, item.name))
            {
                Debug.Log("载入Item:" + item.name + "    当前Item:" + currentItem);
                var bundledata = Resources.LoadAsync <TextAsset>(item.fullname);
                yield return(bundledata);

                var    data         = bundledata.asset as TextAsset;
                byte[] bundle_bytes = data != null ? data.bytes : null;
                Debug.LogFormat("bundledata name:{0}, size:{1}", item.name, bundle_bytes.Length);
                GCHandle hObject = GCHandle.Alloc(bundle_bytes, GCHandleType.Pinned);
                IntPtr   pObject = hObject.AddrOfPinnedObject();
                yield return(FaceunityWorker.fu_CreateItemFromPackage(pObject, bundle_bytes.Length));

                hObject.Free();

                int itemid = FaceunityWorker.fu_getItemIdxFromPackage();

                int itemnum = 0;
                if (beautyitemID >= 0 && itemid >= 0)
                {
                    itemid_tosdk[0] = beautyitemID;
                    itemid_tosdk[1] = itemid;
                    itemnum         = 2;
                }
                else if (beautyitemID >= 0)
                {
                    itemid_tosdk[0] = beautyitemID;
                    itemnum         = 1;
                }
                else if (itemid >= 0)
                {
                    itemid_tosdk[0] = itemid;
                    itemnum         = 1;
                }
                FaceunityWorker.fu_setItemIds(p_itemsid, itemnum, IntPtr.Zero);
                UnLoadItem(currentItem);

                if (string.Equals(item.name, ItemConfig.beautySkin[0].name))
                {
                    beautyitemID = itemid;
                    beautyitem   = item.name;
                    Debug.LogFormat("fu_CreateItemFromPackage beautyitem id:{0}", beautyitemID);
                }
                else
                {
                    currentItemID = itemid;
                    currentItem   = item.name;
                    Debug.LogFormat("fu_CreateItemFromPackage currentItem id:{0}", currentItemID);
                }
            }
            if (item.type == 1)
            {
                flipmark = false;
            }
            else
            {
                flipmark = true;
            }
            if (cb != null)
            {
                cb(item.name);//触发载入道具完成事件
            }
            SetItemMirror();
            LoadingItem = false;
        }
    }