Unity3D仿星露谷物语开发22之选择道具特效

server/2025/1/14 18:35:45/

1、目标

当在库存栏中选择道具时,Slot会红色高亮,表明一个道具已被选中。再次点击道具红色高亮消失。

2、优化InventoryManager.cs脚本

(1)新建被选中项目的变量

我们允许有多个库存列表,比如属于player的即库存栏中,也有chest(未使用过)的。

库存列表位于一个按库存位置索引的数组中。

新增一个变量表明对应库存位置索引下被选中的物品code。

    private int[] selectedInventoryItem; // the index of the array is the inventory list, and the value is the item code,表明每个位置对应被选中物品的code

其在Awake中的初始化如下:

// Initialize selected inventory item array
selectedInventoryItem = new int[(int)InventoryLocation.count];for(int i = 0; i < selectedInventoryItem.Length; i++)
{selectedInventoryItem[i] = -1;
}

其中:

  • InventoryLocation为枚举类。
  • -1表示没有选中任何项目

(2)添加选中项目的函数

public void SetSelectedInventoryItem(InventoryLocation inventoryLocation, int itemCode)
{selectedInventoryItem[(int)inventoryLocation] = itemCode;  
}

(3)添加删除项目的函数

 public void ClearSelectedInventoryItem(InventoryLocation inventoryLocation){selectedInventoryItem[(int)inventoryLocation] = -1;}

此时InventoryManager.cs中的完整代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class InventoryManager : SingletonMonobehaviour<InventoryManager>
{private Dictionary<int, ItemDetails> itemDetailsDictionary;private int[] selectedInventoryItem; // the index of the array is the inventory list, and the value is the item code,表明每个位置对应被选中物品的codepublic List<InventoryItem>[] inventoryLists; // 每个位置的库存清单// 每个位置的库存数。 The index of the array is the inventory list(from the// InventoryLocation enum), and the value is the capacity of that inventory list[HideInInspector] public int[] inventoryListCapacityIntArray; [SerializeField] private SO_ItemList itemList = null;protected override void Awake(){base.Awake();// Create Inventory listsCreateInventoryLists();// Create item details dictionaryCreateItemDetailsDictionary();// Initialize selected inventory item arrayselectedInventoryItem = new int[(int)InventoryLocation.count];for(int i = 0; i < selectedInventoryItem.Length; i++){selectedInventoryItem[i] = -1;}}private void CreateInventoryLists(){inventoryLists = new List<InventoryItem>[(int)InventoryLocation.count];for (int i = 0; i < (int)InventoryLocation.count; i++){inventoryLists[i] = new List<InventoryItem>();}// initialize inventory list capacity arrayinventoryListCapacityIntArray = new int[(int)InventoryLocation.count];// initialize player inventory list capacityinventoryListCapacityIntArray[(int)InventoryLocation.player] = Settings.playerInitialInventoryCapacity;}/// <summary>/// Populates the itemDetailsDictionary from the scriptable object items list/// </summary>private void CreateItemDetailsDictionary(){itemDetailsDictionary = new Dictionary<int, ItemDetails>();foreach (ItemDetails itemDetails in itemList.itemDetails) {itemDetailsDictionary.Add(itemDetails.itemCode, itemDetails);}}/// <summary>/// Add an item to the inventory list for the inventoryLocation and then destroy the gameObjectToDelete/// 角色拾取到物品后,物品需要消失掉/// </summary>/// <param name="inventoryLocation"></param>/// <param name="item"></param>/// <param name="gameObjectToDelete"></param>public void AddItem(InventoryLocation inventoryLocation, Item item, GameObject gameObjectToDelete){AddItem(inventoryLocation, item);Destroy(gameObjectToDelete);}/// <summary>/// Add an item to the inventory list for the inventoryLocation/// </summary>/// <param name="inventoryLocation"></param>/// <param name="item"></param>public void AddItem(InventoryLocation inventoryLocation, Item item){int itemCode = item.ItemCode;List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];// Check if inventory already contains the itemint itemPosition = FindItemInInventory(inventoryLocation, itemCode);if(itemPosition != -1){AddItemPosition(inventoryList, itemCode, itemPosition);}else{AddItemPosition(inventoryList, itemCode);}// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);  }/// <summary>/// Add item to position in the inventory/// </summary>/// <param name="inventoryList"></param>/// <param name="itemCode"></param>/// <param name="position"></param>/// <exception cref="NotImplementedException"></exception>private void AddItemPosition(List<InventoryItem> inventoryList, int itemCode, int position){InventoryItem inventoryItem = new InventoryItem();int quantity = inventoryList[position].itemQuantity + 1;inventoryItem.itemQuantity = quantity;inventoryItem.itemCode = itemCode;inventoryList[position] = inventoryItem;Debug.ClearDeveloperConsole();//DebugPrintInventoryList(inventoryList);}/// <summary>/// Get the item type description for an item type - returns the item type description as a string for a given ItemType/// </summary>/// <param name="itemType"></param>/// <returns></returns>public string GetItemTypeDescription(ItemType itemType){string itemTypeDescription;switch (itemType){case ItemType.Breaking_tool:itemTypeDescription = Settings.BreakingTool;break;case ItemType.Chopping_tool:itemTypeDescription = Settings.ChoppingTool;break;case ItemType.Hoeing_tool:itemTypeDescription = Settings.HoeingTool;break;case ItemType.Reaping_tool:itemTypeDescription = Settings.ReapingTool;break;case ItemType.Watering_tool:itemTypeDescription = Settings.WateringTool;break;case ItemType.Collecting_tool:itemTypeDescription = Settings.CollectingTool;break;default:itemTypeDescription = itemType.ToString();break;}return itemTypeDescription;}/// <summary>/// Remove an item from the inventory, and create a game object at the position it was dropped/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>public void RemoveItem(InventoryLocation inventoryLocation, int itemCode){List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];// Check if inventory already contains the itemint itemPosition = FindItemInInventory(inventoryLocation, itemCode);if(itemPosition != -1){RemoveItemAtPosition(inventoryList, itemCode, itemPosition);}// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);}private void RemoveItemAtPosition(List<InventoryItem> inventoryList, int itemCode, int position){InventoryItem inventoryItem = new InventoryItem();int quantity = inventoryList[position].itemQuantity - 1;if(quantity > 0){inventoryItem.itemQuantity = quantity;inventoryItem.itemCode = itemCode;inventoryList[position] = inventoryItem;}else{inventoryList.RemoveAt(position);   }}/// <summary>/// Swap item at fromItem index with item at toItem index in inventoryLocation inventory list/// </summary>/// <param name="inventoryLocation"></param>/// <param name="fromItem"></param>/// <param name="toItem"></param>public void SwapInventoryItems(InventoryLocation inventoryLocation, int fromItem, int toItem){// if fromItem index and toItemIndex are within the bounds of the list, not the same, and greater than or equal to zeroif(fromItem < inventoryLists[(int)inventoryLocation].Count && toItem < inventoryLists[(int)inventoryLocation].Count&& fromItem != toItem && fromItem >= 0 && toItem >= 0){InventoryItem fromInventoryItem = inventoryLists[(int)inventoryLocation][fromItem];InventoryItem toInventoryItem = inventoryLists[(int)inventoryLocation][toItem];inventoryLists[(int)inventoryLocation][toItem] = fromInventoryItem;inventoryLists[(int)inventoryLocation][fromItem] = toInventoryItem;// Send event that inventory has been updatedEventHandler.CallInventoryUpdatedEvent(inventoryLocation, inventoryLists[(int)inventoryLocation]);}}private void DebugPrintInventoryList(List<InventoryItem> inventoryList){foreach(InventoryItem inventoryItem in inventoryList){Debug.Log("Item Description:" + InventoryManager.Instance.GetItemDetails(inventoryItem.itemCode).itemDescription + "    Item Quantity:" + inventoryItem.itemQuantity);}Debug.Log("*******************************************************************************");}/// <summary>/// Add item to the end of the inventory /// </summary>/// <param name="inventoryList"></param>/// <param name="itemCode"></param>/// <exception cref="NotImplementedException"></exception>private void AddItemPosition(List<InventoryItem> inventoryList, int itemCode){InventoryItem inventoryItem = new InventoryItem(); inventoryItem.itemCode = itemCode;inventoryItem.itemQuantity = 1;inventoryList.Add(inventoryItem);//DebugPrintInventoryList(inventoryList);}/// <summary>/// Find if an itemCode is already in the inventory. Returns the item position/// in the inventory list, or -1 if the item is not in the inventory/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>/// <returns></returns>/// <exception cref="NotImplementedException"></exception>public int FindItemInInventory(InventoryLocation inventoryLocation, int itemCode){List<InventoryItem> inventoryList = inventoryLists[(int)inventoryLocation];for (int i = 0; i < inventoryList.Count; i++){if(inventoryList[i].itemCode == itemCode){return i;}}return -1;}/// <summary>/// Returns the itemDetails (from the SO_ItemList) for the itemCode, or null if the item doesn't exist/// </summary>/// <param name="itemCode"></param>/// <returns></returns>public ItemDetails GetItemDetails(int itemCode) {ItemDetails itemDetails;if(itemDetailsDictionary.TryGetValue(itemCode, out itemDetails)){return itemDetails;}else{return null;}}/// <summary>/// Set the selected inventory item for inventoryLocation to itemCode/// </summary>/// <param name="inventoryLocation"></param>/// <param name="itemCode"></param>public void SetSelectedInventoryItem(InventoryLocation inventoryLocation, int itemCode){selectedInventoryItem[(int)inventoryLocation] = itemCode;  }/// <summary>/// Clear the selected inventory item for inventoryLocation/// </summary>/// <param name="inventoryLocation"></param>public void ClearSelectedInventoryItem(InventoryLocation inventoryLocation){selectedInventoryItem[(int)inventoryLocation] = -1;}
}

3、优化UIInventoryBar.cs脚本

(1)清除选中高亮特效

添加Bar中清除所有选中的红色高亮框的函数:

/// <summary>
/// Clear all highlight from the inventory bar
/// </summary>
public void ClearHighlightOnInventorySlots()
{if (inventorySlot.Length > 0){// loop through inventory slots and clear highlight spritesfor (int i = 0; i < inventorySlot.Length; i++){if (inventorySlot[i].isSelected){inventorySlot[i].isSelected = false;inventorySlot[i].inventorySlotHighlight.color = new Color(0f, 0f, 0f, 0f);// Update inventory to show item as not selectedInventoryManager.Instance.ClearSelectedInventoryItem(InventoryLocation.player);}}}
}

(2)增加选中高亮特效

/// <summary>
/// Set the selected highlight if set on all inventory item positions
/// </summary>
public void SetHighlightedInventorySlots()
{if(inventorySlot.Length > 0){// loop through inventory slots and set highlight spritesfor(int i = 0; i < inventorySlot.Length; i++){SetHighlightedInventorySlots(i);}}
}/// <summary>
/// Set the selected highlight if set on an inventory item for a given slot item position
/// </summary>
/// <param name="itemPosition"></param>
public void SetHighlightedInventorySlots(int itemPosition)
{if(inventorySlot.Length > 0 && inventorySlot[itemPosition].itemDetails != null){if (inventorySlot[itemPosition].isSelected){inventorySlot[itemPosition].inventorySlotHighlight.color = new Color(1f, 1f, 1f, 1f);// Update inventory to show item as selectedInventoryManager.Instance.SetSelectedInventoryItem(InventoryLocation.player, inventorySlot[itemPosition].itemDetails.itemCode);}}
}

在InventoryUpdated函数中增加SetHighlightedInventorySlots()的调用。

此时UIInventoryBar.cs的完整代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class UIInventoryBar : MonoBehaviour
{[SerializeField] private Sprite blank16x16sprite = null; // 插槽默认图案[SerializeField] private UIInventorySlot[] inventorySlot = null; // 所有插槽集合public GameObject inventoryBarDraggedItem;[HideInInspector] public GameObject inventoryTextBoxGameobject;private RectTransform rectTransform;private bool _isInventoryBarPositionBottom = true;public bool IsInventoryBarPositionBottom { get { return _isInventoryBarPositionBottom;} set { _isInventoryBarPositionBottom = value; } }private void Awake(){rectTransform = GetComponent<RectTransform>();}private void OnDisable(){EventHandler.InventoryUpdatedEvent -= InventoryUpdated;}private void InventoryUpdated(InventoryLocation inventoryLocation, List<InventoryItem> inventoryList){if (inventoryLocation == InventoryLocation.player){ClearInventorySlots();if (inventorySlot.Length > 0 && inventoryList.Count > 0){for (int i = 0; i < inventorySlot.Length; i++){if (i < inventoryList.Count){int itemCode = inventoryList[i].itemCode;ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(itemCode);if (itemDetails != null){// add images and details to inventory item slotinventorySlot[i].inventorySlotImage.sprite = itemDetails.itemSprite;inventorySlot[i].textMeshProUGUI.text = inventoryList[i].itemQuantity.ToString();inventorySlot[i].itemDetails = itemDetails;inventorySlot[i].itemQuantity = inventoryList[i].itemQuantity;SetHighlightedInventorySlots(i);}}else{break;}}}}}private void ClearInventorySlots(){if(inventorySlot.Length > 0){// loop through inventory slots and update with blank spritefor(int i = 0; i < inventorySlot.Length; i++){inventorySlot[i].inventorySlotImage.sprite = blank16x16sprite;inventorySlot[i].textMeshProUGUI.text = "";inventorySlot[i].itemDetails = null;inventorySlot[i].itemQuantity = 0;}}}private void OnEnable(){EventHandler.InventoryUpdatedEvent += InventoryUpdated;}private void Update(){// Switch inventory bar position depending on player positionSwitchInventoryBarPosition();}private void SwitchInventoryBarPosition(){Vector3 playerViewportPosition = Player.Instance.GetPlayerViewportPosition();if (playerViewportPosition.y > 0.3f && IsInventoryBarPositionBottom == false){rectTransform.pivot = new Vector2(0.5f, 0f);rectTransform.anchorMin = new Vector2(0.5f, 0f);rectTransform.anchorMax = new Vector2(0.5f, 0f);rectTransform.anchoredPosition = new Vector2(0f, 2.5f);IsInventoryBarPositionBottom = true;}else if (playerViewportPosition.y <= 0.3f && IsInventoryBarPositionBottom == true) {rectTransform.pivot = new Vector2(0.5f, 1f);rectTransform.anchorMin = new Vector2(0.5f, 1f);rectTransform.anchorMax = new Vector2(0.5f, 1f);rectTransform.anchoredPosition = new Vector2(0f, -2.5f);IsInventoryBarPositionBottom = false;}}/// <summary>/// Clear all highlight from the inventory bar/// </summary>public void ClearHighlightOnInventorySlots(){if (inventorySlot.Length > 0){// loop through inventory slots and clear highlight spritesfor (int i = 0; i < inventorySlot.Length; i++){if (inventorySlot[i].isSelected){inventorySlot[i].isSelected = false;inventorySlot[i].inventorySlotHighlight.color = new Color(0f, 0f, 0f, 0f);// Update inventory to show item as not selectedInventoryManager.Instance.ClearSelectedInventoryItem(InventoryLocation.player);}}}}/// <summary>/// Set the selected highlight if set on all inventory item positions/// </summary>public void SetHighlightedInventorySlots(){if(inventorySlot.Length > 0){// loop through inventory slots and set highlight spritesfor(int i = 0; i < inventorySlot.Length; i++){SetHighlightedInventorySlots(i);}}}/// <summary>/// Set the selected highlight if set on an inventory item for a given slot item position/// </summary>/// <param name="itemPosition"></param>public void SetHighlightedInventorySlots(int itemPosition){if(inventorySlot.Length > 0 && inventorySlot[itemPosition].itemDetails != null){if (inventorySlot[itemPosition].isSelected){inventorySlot[itemPosition].inventorySlotHighlight.color = new Color(1f, 1f, 1f, 1f);// Update inventory to show item as selectedInventoryManager.Instance.SetSelectedInventoryItem(InventoryLocation.player, inventorySlot[itemPosition].itemDetails.itemCode);}}}
}

4、优化UIInventorySlot.cs脚本

(1)新增Slot是否被选中的变量

[HideInInspector] public bool isSelected = false;

(2)添加IPointerClickHandler处理函数

处理在Slot上的鼠标点击事件。

public void OnPointerClick(PointerEventData eventData)
{// if left clickif (eventData.button == PointerEventData.InputButton.Left){// if inventory slot currently selected then deselectif (isSelected == true){ClearSelectedItem();}else  // 未被选中且有东西则显示选中的效果{if(itemQuantity > 0){SetSelectedItem();}}}
}/// <summary>
/// Set this inventory slot item to be selected
/// </summary>
private void SetSelectedItem()
{// Clear currently highlighted itemsinventoryBar.ClearHighlightOnInventorySlots();// Highlight item on inventory barisSelected = true;// Set highlighted inventory slotsinventoryBar.SetHighlightedInventorySlots();// Set item selected in inventoryInventoryManager.Instance.SetSelectedInventoryItem(InventoryLocation.player, itemDetails.itemCode);
}private void ClearSelectedItem()
{// Clear currently highlighted iteminventoryBar.ClearHighlightOnInventorySlots();isSelected = false;// set no item selected in inventoryInventoryManager.Instance.ClearSelectedInventoryItem(InventoryLocation.player);
}

(3)修改DropSelectedItemAtMousePosition方法

private void DropSelectedItemAtMousePosition()
{if(itemDetails != null && isSelected){Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCamera.transform.position.z));// Create item from prefab at mouse positionGameObject itemGameObject = Instantiate(itemPrefab, worldPosition, Quaternion.identity, parentItem);Item item = itemGameObject.GetComponent<Item>();item.ItemCode = itemDetails.itemCode;// Remove item from player's inventoryInventoryManager.Instance.RemoveItem(InventoryLocation.player, item.ItemCode);// If no more of item then clear selectedif(InventoryManager.Instance.FindItemInInventory(InventoryLocation.player, item.ItemCode) == -1){ClearSelectedItem();}}
}

增加isSelected的判断。

如果库存列表中不存在商品,那么增加清除红色高亮。

(4)修改OnBeginDrag函数

增加SetSelectedItem()调用。

开始拖动的时候,就可以显示红色高亮。

(5)修改OnEndDrag函数

增加ClearSelectedItem()调用。

结束拖动的时候,就可以清除红色高亮了。

此时UIInventorySlot.cs的完整代码如下:

using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;public class UIInventorySlot : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{private Camera mainCamera;private Transform parentItem; // 场景中的物体父类private GameObject draggedItem; // 被拖动的物体private Canvas parentCanvas;public Image inventorySlotHighlight;public Image inventorySlotImage;public TextMeshProUGUI textMeshProUGUI;[SerializeField] private UIInventoryBar inventoryBar = null;[SerializeField] private GameObject itemPrefab = null;[SerializeField] private int slotNumber = 0; // 插槽的序列号[SerializeField] private GameObject inventoryTextBoxPrefab = null;[HideInInspector] public ItemDetails itemDetails;[HideInInspector] public int itemQuantity;[HideInInspector] public bool isSelected = false;private void Awake(){parentCanvas = GetComponentInParent<Canvas>();}private void Start(){mainCamera = Camera.main;parentItem = GameObject.FindGameObjectWithTag(Tags.ItemsParentTransform).transform;}public void OnBeginDrag(PointerEventData eventData){if(itemDetails != null) {// Disable keyboard inputPlayer.Instance.DisablePlayerInputAndResetMovement();// Instatiate gameobject as dragged itemdraggedItem = Instantiate(inventoryBar.inventoryBarDraggedItem, inventoryBar.transform);// Get image for dragged itemImage draggedItemImage = draggedItem.GetComponentInChildren<Image>();draggedItemImage.sprite = inventorySlotImage.sprite;SetSelectedItem();}}public void OnDrag(PointerEventData eventData){// move game object as dragged itemif(!draggedItem != null){draggedItem.transform.position = Input.mousePosition;}}public void OnEndDrag(PointerEventData eventData){// Destroy game object as dragged itemif (draggedItem != null) {Destroy(draggedItem);// if drag ends over inventory bar, get item drag is over and swap thenif (eventData.pointerCurrentRaycast.gameObject != null && eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>() != null) {// get the slot number where the drag endedint toSlotNumber = eventData.pointerCurrentRaycast.gameObject.GetComponent<UIInventorySlot>().slotNumber;// Swap inventory items in inventory listInventoryManager.Instance.SwapInventoryItems(InventoryLocation.player, slotNumber, toSlotNumber);// Destroy inventory text boxDestroyInventoryTextBox();// Clear selected itemClearSelectedItem();}else{// else attemp to drop the item if it can be droppedif (itemDetails.canBeDropped){DropSelectedItemAtMousePosition();}}// Enable player inputPlayer.Instance.EnablePlayerInput();}}/// <summary>/// Drops the item(if selected) at the current mouse position. called by the DropItem event/// </summary>private void DropSelectedItemAtMousePosition(){if(itemDetails != null && isSelected){Vector3 worldPosition = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, -mainCamera.transform.position.z));// Create item from prefab at mouse positionGameObject itemGameObject = Instantiate(itemPrefab, worldPosition, Quaternion.identity, parentItem);Item item = itemGameObject.GetComponent<Item>();item.ItemCode = itemDetails.itemCode;// Remove item from player's inventoryInventoryManager.Instance.RemoveItem(InventoryLocation.player, item.ItemCode);// If no more of item then clear selectedif(InventoryManager.Instance.FindItemInInventory(InventoryLocation.player, item.ItemCode) == -1){ClearSelectedItem();}}}public void OnPointerEnter(PointerEventData eventData){// Populate text box with item detailsif(itemQuantity != 0){// Instantiate inventory text boxinventoryBar.inventoryTextBoxGameobject = Instantiate(inventoryTextBoxPrefab, transform.position, Quaternion.identity);inventoryBar.inventoryTextBoxGameobject.transform.SetParent(parentCanvas.transform, false);UIInventoryTextBox inventoryTextBox = inventoryBar.inventoryTextBoxGameobject.GetComponent<UIInventoryTextBox>();// Set item type descriptionstring itemTypeDescription = InventoryManager.Instance.GetItemTypeDescription(itemDetails.itemType);// Populate text boxinventoryTextBox.SetTextboxText(itemDetails.itemDescription, itemTypeDescription, "", itemDetails.itemLongDescription, "", "");// Set text box position according to inventory bar positionif (inventoryBar.IsInventoryBarPositionBottom){inventoryBar.inventoryTextBoxGameobject.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 0f);inventoryBar.inventoryTextBoxGameobject.transform.position = new Vector3(transform.position.x, transform.position.y + 50f, transform.position.z);}else{inventoryBar.inventoryTextBoxGameobject.GetComponent<RectTransform>().pivot = new Vector2(0.5f, 1f);inventoryBar.inventoryTextBoxGameobject.transform.position = new Vector3(transform.position.x, transform.position.y - 50f, transform.position.z);}}}public void OnPointerExit(PointerEventData eventData){DestroyInventoryTextBox();}private void DestroyInventoryTextBox(){if (inventoryBar.inventoryTextBoxGameobject != null) {Destroy(inventoryBar.inventoryTextBoxGameobject);}}public void OnPointerClick(PointerEventData eventData){// if left clickif (eventData.button == PointerEventData.InputButton.Left){// if inventory slot currently selected then deselectif (isSelected == true){ClearSelectedItem();}else  // 未被选中且有东西则显示选中的效果{if(itemQuantity > 0){SetSelectedItem();}}}}/// <summary>/// Set this inventory slot item to be selected/// </summary>private void SetSelectedItem(){// Clear currently highlighted itemsinventoryBar.ClearHighlightOnInventorySlots();// Highlight item on inventory barisSelected = true;// Set highlighted inventory slotsinventoryBar.SetHighlightedInventorySlots();// Set item selected in inventoryInventoryManager.Instance.SetSelectedInventoryItem(InventoryLocation.player, itemDetails.itemCode);}private void ClearSelectedItem(){// Clear currently highlighted iteminventoryBar.ClearHighlightOnInventorySlots();isSelected = false;// set no item selected in inventoryInventoryManager.Instance.ClearSelectedInventoryItem(InventoryLocation.player);}
}

运行程序效果如下:


http://www.ppmy.cn/server/158354.html

相关文章

【AI游戏】基于OpenAI打造自动生成剧情的 Python 游戏

引言 你是否曾经梦想过成为一名游戏设计师&#xff0c;创造出引人入胜的冒险故事&#xff1f;今天&#xff0c;我将带你使用 OpenAI 的 GPT 模型和 Python 编写一个简单的自动生成剧情游戏。通过这个项目&#xff0c;你可以体验到人工智能在创意写作中的强大能力&#xff0c;并…

Autodl安装tensorflow2.10.0记录

首先租用新实例&#xff08;我选的是3080*2卡&#xff09;&#xff0c;由于基础镜像中没有2.10.0版本&#xff0c;选miniconda3的基础环境 创建虚拟环境&#xff1a;conda create --name xxx python3.8&#xff08;环境名&#xff09;激活虚拟环境&#xff1a;conda activate x…

【ArcGIS技巧】如何给CAD里的面注记导入GIS属性表中

前面分享了GIS怎么给田块加密高程点&#xff0c;但是没有分享每块田的高程对应的是哪块田&#xff0c;今天结合土地整理软件GLAND做一期田块的属性怎么放入GIS属性表当中。 1、GLAND数据 杭州阵列软件&#xff08;GLand&#xff09;是比较专业的土地整理软件&#xff0c;下载之…

如何让 LLM 使用外部函数 or 工具?Llama-3-Groq-8B-Tool-Use 模型使用详解

2024年7月份&#xff0c;Groq 团队在huggingface上发布了基于Meta llama3两个大小&#xff08;8b和70b&#xff09;的开源模型进行微调&#xff08;官网介绍&#xff09;的模型&#xff08;Groq/Llama-3-Groq-8B-Tool-Use 和 Groq/Llama-3-Groq-70B-Tool-Use&#xff09;&#…

Chromium 132 编译指南 Windows 篇 - 配置核心环境变量 (三)

1. 引言 在之前的 Chromium 编译指南系列文章中&#xff0c;我们已经完成了编译前的准备工作以及 depot_tools 工具的安装与配置。本篇我们将聚焦于 Chromium 编译过程中至关重要的环境变量设置&#xff0c;这些配置是您顺利进行 Chromium 构建的基石。 2. 启用本地编译&…

Jupyter notebook入门教程

一、优点&#xff1a; 1、代码分成小块逐块运行&#xff0c;方便查看中间结果&#xff0c;调试和修改 2、文档和代码结合&#xff0c;比普通的注释好看&#xff0c;使代码的可读性大大提高 3、可以生成多种格式的报告&#xff0c;适合演示使用 二、如何打开 命令行下载jupy…

如何更轻松的对React refs 的理解?都有哪些应用场景?

React refs 的理解与应用 refs 是 React 提供的一种机制&#xff0c;用于直接访问 DOM 元素或 React 组件实例。在 React 中&#xff0c;refs 主要用于获取对 DOM 元素的引用&#xff0c;或访问类组件中的实例方法。在许多情况下&#xff0c;refs 是避免使用传统的 JavaScript…

后端:Spring(IOC、AOP)

文章目录 1. Spring2. IOC 控制反转2-1. 通过配置文件定义Bean2-1-1. 通过set方法来注入Bean2-1-2. 通过构造方法来注入Bean2-1-3. 自动装配2-1-4. 集合注入2-1-5. 数据源对象管理(第三方Bean)2-1-6. 在xml配置文件中加载properties文件的数据(context命名空间)2-1-7. 加载容器…