private void Start() { Instance = this; layout.Init(); layout.HeatToggle.onValueChanged.AddListener(state => { isHeating = state; GuessRecipe(); }); }
public UIItem(Layer owner, UILayout layout, int depth, bool multiclick = false) { Layer = owner; this.multiClick = multiclick; this._Depth = depth; this.Layout = layout; UIRenderer.Add(owner, this); Layout.Init(this); }
private void Start() { #if UNITY_EDITOR foreach (var storage in inventoryStorageList) { storage.inventory = Instantiate(storage.inventory); } #endif layout.Init(); layout.dropInvBtn.onClick.AddListener(DropCurrentInv); SwapManager.OnHighlightedItem += (InventoryIconUI invUI) => { if (invUI.m_iconType == InventoryIconType.Inv) { toDropSlotID = invUI.m_slotID; UpdateCurrentSelectText(invUI.m_slotID); } }; //选择 Inventory ControlEvents.OnClickInventoryByID += id => { if (!Util.isCrafting) { SelectInventoryByID(id); } }; ControlEvents.OnClickScreen += pos => { //使用 Inventory (PC 右键 移动端点击屏幕) var isPlaceable = inventoryStorageList.Find(val => val.slotID == currentSelectID)?.inventory is IPlaceable; if (isPlaceable) { PlaceCurrentInventory(pos); } }; ControlEvents.OnBeginPressScreen += () => { interactTime = 0; }; ControlEvents.OnPressingScreen += pos => { //与方块交互 InteractBlock(pos); }; ControlEvents.OnEndPressScreen += () => { interactTime = 0; layout.digProgressBar.SetActive(false); }; //分配物体 SwapManager.OnAllocateItem += (a, b, type) => { if (type == SwapType.CraftToCraft) { var itemA = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == a); var itemB = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == b); if (itemA == null) { return; } if (itemB != null) { return; } itemA.count -= 1; CraftSystem.Instance.craftInventoryList.Add(new InventoryStorage() { count = 1, inventory = itemA.inventory, slotID = b }); } else if (type == SwapType.InvToCraft) { var itemA = inventoryStorageList.Find(val => val.slotID == a); var itemB = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == b); if (itemA == null) { return; } if (itemB != null) { return; } itemA.count -= 1; CraftSystem.Instance.craftInventoryList.Add(new InventoryStorage() { count = 1, inventory = itemA.inventory, slotID = b }); } CleanUpInventory(); UpdateInvetoryUI(); CraftSystem.Instance.UpdateInvetoryUI(); }; //交换物体 SwapManager.OnSwapItem += (a, b, type) => { //Inventory 中交换物品 if (type == SwapType.InvToInv) { var itemA = inventoryStorageList.Find(val => val.slotID == a); var itemB = inventoryStorageList.Find(val => val.slotID == b); var isAppendCount = false; //数量叠加 if (itemA != null && itemB != null) { if (itemA.inventory.inventoryName == itemB.inventory.inventoryName) { isAppendCount = true; } } //纯粹交换物品 else { isAppendCount = false; } if (isAppendCount) { var sum = itemA.count + itemB.count; if (sum > 64) { itemA.count = sum - 64; itemB.count = 64; } else { itemB.count = itemA.count + itemB.count; itemA.count = 0; } CleanUpInventory(); } else { if (itemA != null) { itemA.slotID = b; } if (itemB != null) { itemB.slotID = a; } } UpdateInvetoryUI(); } //Inventory Craft 交换物品 else if (type == SwapType.InvToCraft) { var itemA = inventoryStorageList.Find(val => val.slotID == a); var itemB = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == b); var isAppendCount = false; //数量叠加 if (itemA != null && itemB != null) { if (itemA.inventory.inventoryName == itemB.inventory.inventoryName) { isAppendCount = true; } } //纯粹交换物品 else { isAppendCount = false; } if (isAppendCount) { var sum = itemA.count + itemB.count; if (sum > 64) { itemA.count = sum - 64; itemB.count = 64; } else { itemB.count = itemA.count + itemB.count; itemA.count = 0; } CleanUpInventory(); } else { if (itemA != null) { itemA.slotID = b; inventoryStorageList.Remove(itemA); CraftSystem.Instance.craftInventoryList.Add(itemA); } if (itemB != null) { itemB.slotID = a; inventoryStorageList.Add(itemB); CraftSystem.Instance.craftInventoryList.Remove(itemB); } } UpdateInvetoryUI(); CraftSystem.Instance.UpdateInvetoryUI(); } //Craft Inventory 交换物品 else if (type == SwapType.CraftToInv) { var itemA = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == a); var itemB = inventoryStorageList.Find(val => val.slotID == b); var isAppendCount = false; //数量叠加 if (itemA != null && itemB != null) { if (itemA.inventory.inventoryName == itemB.inventory.inventoryName) { isAppendCount = true; } } //纯粹交换物品 else { isAppendCount = false; } if (isAppendCount) { var sum = itemA.count + itemB.count; if (sum > 64) { itemA.count = sum - 64; itemB.count = 64; } else { itemB.count = itemA.count + itemB.count; itemA.count = 0; } CleanUpInventory(); } else { if (itemA != null) { itemA.slotID = b; inventoryStorageList.Add(itemA); CraftSystem.Instance.craftInventoryList.Remove(itemA); } if (itemB != null) { itemB.slotID = a; inventoryStorageList.Remove(itemB); CraftSystem.Instance.craftInventoryList.Add(itemB); } } UpdateInvetoryUI(); CraftSystem.Instance.UpdateInvetoryUI(); } //Craft 中交换物品 else if (type == SwapType.CraftToCraft) { var itemA = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == a); var itemB = CraftSystem.Instance.craftInventoryList.Find(val => val.slotID == b); var isAppendCount = false; //数量叠加 if (itemA != null && itemB != null) { if (itemA.inventory.inventoryName == itemB.inventory.inventoryName) { isAppendCount = true; } } //纯粹交换物品 else { isAppendCount = false; } if (isAppendCount) { var sum = itemA.count + itemB.count; if (sum > 64) { itemA.count = sum - 64; itemB.count = 64; } else { itemB.count = itemA.count + itemB.count; itemA.count = 0; } CleanUpInventory(); } else { if (itemA != null) { itemA.slotID = b; } if (itemB != null) { itemB.slotID = a; } } CraftSystem.Instance.UpdateInvetoryUI(); } //Craft 生成物体 else if (type == SwapType.CraftedToInv) { //当前没有合成的物体 if (CraftSystem.Instance.craftedInventory == null) { return; } var targetInv = inventoryStorageList.Find(val => val.slotID == b); //深复制防止多引用 var craftedInventory = CraftSystem.Instance.craftedInventory.Clone(); var isCrafted = false; //目标插槽空 if (targetInv == null) { isCrafted = true; craftedInventory.slotID = b; inventoryStorageList.Add(craftedInventory); } else if (targetInv.inventory.inventoryName == craftedInventory.inventory.inventoryName && targetInv.count + craftedInventory.count <= 64) { isCrafted = true; craftedInventory.slotID = b; targetInv.count += craftedInventory.count; } //满足Craft 条件后,销毁原材料 if (isCrafted) { foreach (var usedItem in CraftSystem.Instance.craftInventoryList) { usedItem.count -= 1; } CleanUpInventory(); UpdateInvetoryUI(); CraftSystem.Instance.UpdateInvetoryUI(); } } }; UpdateInvetoryUI(); SelectInventoryByID(currentSelectID); }