/// <summary> /// 交换道具-容器 /// </summary> /// <param name="viewItemContainer"></param> public bool ExchangeItemForContainer(UIViewItemContainer viewItemContainer) { UIViewItem tempViewItem = viewItemContainer.GetViewItem(); if (tempViewItem == null) { //检测容器是否能放置当前物品 bool canSetItem = viewItemContainer.CheckCanSetItem(itemsInfo.GetItemsType()); //如果能放置 if (canSetItem) { //如果容器里没有道具 则直接设置容器的道具为当前道具 //首先清空原容器里的数据 originalParent.ClearViewItem(); //设置新的容器 viewItemContainer.SetViewItem(this); AnimForPositionChange(timeForMove, null); } else { //如果没有设置成功(不能放置该类型),则返回原容器 HandleForBackOriginalContainer(); } return(true); } else { //如果容器里有道具 则返回原容器 HandleForBackOriginalContainer(); return(true); } }
/// <summary> /// 增加道具 /// </summary> /// <param name="uiViewItem"></param> public bool AddItems(UIViewItem uiViewItem) { //首先直接在显示的list中搜索空位 List <GameObject> listCellObj = ui_ItemList.GetAllCellObj(); for (int i = 0; i < listCellObj.Count; i++) { GameObject itemObj = listCellObj[i]; UIViewItemContainer itemContainer = itemObj.GetComponent <UIViewItemContainer>(); //如果有容器VIEW 并且里面没有东西 if (itemContainer != null && itemContainer.GetViewItem() == null) { uiViewItem.ExchangeItemForContainer(itemContainer); return(true); } } //如果不成功则直接查询整个listBackpack for (int i = 0; i < listBackpack.Length; i++) { ItemsBean itemData = listBackpack[i]; if (itemData == null || itemData.itemId == 0) { itemData.itemId = uiViewItem.itemId; itemData.number = uiViewItem.itemNumber; itemData.meta = uiViewItem.meta; Destroy(uiViewItem.gameObject); return(true); } } return(false); }
/// <summary> /// 单个数据回调 /// </summary> /// <param name="itemCell"></param> public void OnCellForItem(ScrollGridCell itemCell) { UIViewItemContainer viewItemContainer = itemCell.GetComponent <UIViewItemContainer>(); ItemsBean itemsData = blockBoxData.items[itemCell.index]; viewItemContainer.SetData(UIViewItemContainer.ContainerType.Box, itemsData, itemCell.index); }
/// <summary> /// 单个数据回调 /// </summary> /// <param name="itemCell"></param> public void OnCellForItem(ScrollGridCell itemCell) { UserDataBean userData = GameDataHandler.Instance.manager.GetUserData(); UIViewItemContainer viewItemContainer = itemCell.GetComponent <UIViewItemContainer>(); ItemsBean itemsData = userData.GetItemsFromBackpack(itemCell.index); viewItemContainer.SetData(UIViewItemContainer.ContainerType.Backpack, itemsData, itemCell.index); }
/// <summary> /// 道具修改回调 /// </summary> /// <param name="itemContainer"></param> /// <param name="itemId"></param> public void CallBackForItemChange(UIViewItemContainer itemContainer, long itemId) { if (itemContainer.containerType != UIViewItemContainer.ContainerType.Box) { return; } //保存数据 SaveBoxBlockData(); }
/// <summary> /// 单个数据回调 /// </summary> /// <param name="itemCell"></param> public void OnCellForItem(ScrollGridCell itemCell) { UIViewItemContainer viewItemContainer = itemCell.GetComponent <UIViewItemContainer>(); ItemsInfoBean itemsInfo = listItemsInfo[itemCell.index]; ItemsBean itemsData = new ItemsBean(); itemsData.itemId = itemsInfo.id; itemsData.number = int.MaxValue; viewItemContainer.SetData(UIViewItemContainer.ContainerType.God, itemsData, itemCell.index); }
/// <summary> /// 开始拖拽 /// </summary> /// <param name="eventData"></param> public void OnBeginDrag(PointerEventData eventData) { if (inputActionClick.ReadValue <float>() != 1 || isAnim) { isBeginDrag = false; return; } isBeginDrag = true; originalParent = transform.parent.GetComponent <UIViewItemContainer>(); //如果是无限物品格 则在原位置实例化一个新的 if (itemNumber == int.MaxValue) { CopyItemInOriginal(); } else { } RefreshUI(); isRaycastLocationValid = false;//设置射线忽略自身 }
/// <summary> /// 设置装备回调 /// </summary> /// <param name="changeContainer"></param> /// <param name="itemId"></param> public void CallBackForSetEquip(UIViewItemContainer changeContainer, long itemId) { foreach (var itemContainer in dicEquip) { if (changeContainer == itemContainer.Value) { //更换装备 Player player = GameHandler.Instance.manager.player; CreatureCptCharacter character = player.GetCharacter(); character.characterEquip.ChangeEquip(itemContainer.Key, itemId); //设置渲染摄像头 Action <GameObject> callBack = (objModel) => { showCharacter.SetLayerAllChild(LayerInfo.RenderCamera); }; //UI显示也修改 showCharacter.characterEquip.ChangeEquip(itemContainer.Key, itemId, callBack); } } }
/// <summary> /// 结束拖拽 /// </summary> /// <param name="eventData"></param> public void OnEndDrag(PointerEventData eventData) { if (isAnim || !isBeginDrag) { return; } isBeginDrag = false; GameObject objTarget = eventData.pointerCurrentRaycast.gameObject; if (objTarget != null) { UIViewItemContainer viewItemContainer = objTarget.GetComponent <UIViewItemContainer>(); //如果检测到容器 说明只有容器 里面没有道具 if (viewItemContainer != null) { ExchangeItemForContainer(viewItemContainer); return; } UIViewItem viewItem = objTarget.GetComponent <UIViewItem>(); //如果检测到道具 说明容器里有道具 if (viewItem != null) { ExchangeItemForItem(viewItem); return; } else { HandleForBackOriginalContainer(); return; } } else { //如果什么都没有检测到,说明是把物体丢到场景中 DropItem(); return; } }
/// <summary> /// 初始化状态栏 /// </summary> public void InitShortcuts() { UserDataBean userData = GameDataHandler.Instance.manager.GetUserData(); for (int i = 0; i < listShortcut.Count; i++) { listShortcut[i].SetData(UIViewItemContainer.ContainerType.Shortcuts, userData.GetItemsFromShortcut(i), i); } for (int i = 0; i < listShortcut.Count; i++) { UIViewItemContainer uiViewItem = listShortcut[i]; if (uiViewItem.viewIndex == userData.indexForShortcuts) { //如果选中的是当前状态栏 uiViewItem.SetSelectState(true); } else { //如果未选中当前状态栏 uiViewItem.SetSelectState(false); } } }
/// <summary> /// 道具和道具交换 /// </summary> /// <param name="viewItem"></param> /// <returns></returns> protected bool ExchangeItemForItem(UIViewItem viewItem) { //如果目标是同一物品 if (viewItem.itemsInfo.id == itemsInfo.id) { //如果目标是无限物品 则删除现有物品 if (viewItem.itemNumber == int.MaxValue) { transform.SetParent(viewItem.transform.parent); transform.localScale = Vector3.one; AnimForPositionChange(timeForMove, () => { DestroyImmediate(gameObject); }); return(true); } //如果目标不是无限物品,则先将目标叠加到最大,自己再返回原位置 else { //目标数量叠加 viewItem.itemNumber += itemNumber; if (viewItem.itemNumber > viewItem.itemsInfo.max_number) { //如果目标数量超出最大了 itemNumber = viewItem.itemNumber - viewItem.itemsInfo.max_number; viewItem.itemNumber = viewItem.itemsInfo.max_number; } else { //如果没有 则自己的数量为0 itemNumber = 0; } //刷新一下UI viewItem.RefreshUI(); RefreshUI(); if (itemNumber == 0) { //如果自己没有数量了,则删除 DestroyImmediate(gameObject); return(true); } else { //如果自己还有数量,则返回原容器 HandleForBackOriginalContainer(); return(true); } } } //如果目标不是同一类物品 else { //如果目标是无限物品 则回到原来位置 if (viewItem.itemNumber == int.MaxValue) { HandleForBackOriginalContainer(); return(true); } //如果是目标不是无限物品,则交换物品位置 else { //检测容器是否能放置当前物品 bool canSetItem = viewItem.originalParent.CheckCanSetItem(itemsInfo.GetItemsType()); if (canSetItem) { //交换位置 UIViewItemContainer dargContainer = this.originalParent; UIViewItemContainer targetContainer = viewItem.originalParent; //交换父级 if (dargContainer.GetViewItem() != null && dargContainer.GetViewItem().itemNumber == int.MaxValue) { //如果原父级有东西 则把目标容器里的物品丢出来 viewItem.DropItem(); } else { //如果原父级没有东西 则交换父级 dargContainer.SetViewItem(viewItem); //设置位置 viewItem.rectTransform.anchoredPosition = Vector2.zero; viewItem.transform.localScale = Vector3.one; } targetContainer.SetViewItem(this); //设置位置 transform.localScale = Vector3.one; AnimForPositionChange(timeForMove, () => { }); return(true); } else { //如果不能设置该物品(容器不能装该类型) 则返回 HandleForBackOriginalContainer(); return(true); } } } }
/// <summary> /// 点击 /// </summary> /// <param name="eventData"></param> public void OnPointerClick(PointerEventData eventData) { float isFastClick = inputActionShiftClick.ReadValue <float>(); //LogUtil.Log($"OnPointerClick dragging:{eventData.dragging} pointerDrag:{eventData.pointerDrag.name} eligibleForClick:{eventData.eligibleForClick} isFastClick:{isFastClick}"); //如果是快速选择 if (isFastClick == 1) { BaseUIComponent currentUI = UIHandler.Instance.GetOpenUI(); UIViewBoxList boxList; UIViewBackpackList backpackUI; UIViewShortcuts shortcutsUI; switch (originalParent.containerType) { //如果是快捷栏 case UIViewItemContainer.ContainerType.Shortcuts: //首先检测是否有箱子 优先放进箱子 boxList = currentUI.GetComponentInChildren <UIViewBoxList>(); if (boxList != null) { if (boxList.AddItems(this)) { return; } } backpackUI = currentUI.GetComponentInChildren <UIViewBackpackList>(); if (backpackUI != null) { if (backpackUI.AddItems(this)) { return; } } break; //如果是背包或者上帝模式 case UIViewItemContainer.ContainerType.Backpack: case UIViewItemContainer.ContainerType.God: //首先检测是否有箱子 优先放进箱子 boxList = currentUI.GetComponentInChildren <UIViewBoxList>(); if (boxList != null) { if (boxList.AddItems(this)) { return; } } //获取快捷栏 shortcutsUI = currentUI.GetComponentInChildren <UIViewShortcuts>(); if (shortcutsUI != null) { for (int i = 0; i < shortcutsUI.listShortcut.Count; i++) { UIViewItemContainer itemContainer = shortcutsUI.listShortcut[i]; //如果有容器VIEW 并且里面没有东西 if (itemContainer != null && itemContainer.GetViewItem() == null) { //如果是上帝模式则需要在原位置复制一个 if (originalParent.containerType == UIViewItemContainer.ContainerType.God) { CopyItemInOriginal(); } ExchangeItemForContainer(itemContainer); return; } } } break; //如果是箱子里的东西 case UIViewItemContainer.ContainerType.Box: //首先放进快捷栏 shortcutsUI = currentUI.GetComponentInChildren <UIViewShortcuts>(); if (shortcutsUI != null) { for (int i = 0; i < shortcutsUI.listShortcut.Count; i++) { UIViewItemContainer itemContainer = shortcutsUI.listShortcut[i]; //如果有容器VIEW 并且里面没有东西 if (itemContainer != null && itemContainer.GetViewItem() == null) { ExchangeItemForContainer(itemContainer); return; } } } //如果没有成功再放进背包 backpackUI = currentUI.GetComponentInChildren <UIViewBackpackList>(); if (backpackUI != null) { if (backpackUI.AddItems(this)) { return; } } break; } } }