仓库物品与装备物品位置更换

news2024/9/9 5:03:31
一、装备物品与选中的仓库物品位置交换

1、准备工作

2、Inventory Items

3、给Warehouse添加Grid Layout Group组件

4、复制Inventory Items,设置Grid Layout Group组件

5、创建文本ItemName和ItemDescription

6、设置物品数据

(1) 创建 ItemData.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "ItemData",menuName ="Inventory/ItemData",order = 1)]
public class ItemData : ScriptableObject
{
    public string description;//描述特征
    public Sprite thumbnail;//图标
    public GameObject gameModel;//游戏模型
}

fileName = "ItemData" :
当通过Unity编辑器创建此资产的新实例时,文件的默认名称将是 "ItemData"。

menuName ="Inventory/ItemData" :

在Unity编辑器的“Project”窗口的“Create”菜单中,会看到一个新的子菜单“Inventory”

并在子菜单“Inventory”中可看到“ItemData”选项

点击“ItemData”选项可以创建一个新的“ItemData”资产

order = 1 :

在“Create”菜单中被创建的选项的显示顺序。数字越小,显示的位置越靠前。

(2) 创建Scriptable Object:在项目视图中右键点击 -> Create -> Inventory -> ItemData。在Inspector中给该ItemData对象添加描述和缩略图。

7、显示物品缩略图

(1) 在Inventory Item预制体上添加ItemHover.cs组件,设置物品栏中的某一个物品的缩略图

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    ItemData itemToDisplay;//引用ItemData对象(包含物品栏中的一个物品的信息)
    public Image itemToDisplayImage;

    //显示物品缩略图
    public void Display(ItemData item)
    {
        if (item != null && item.thumbnail !=null)
        {
            //获取的ItemData类的物品信息item传递给this.itemToDisplay
            this.itemToDisplay = item;
            itemToDisplayImage.sprite = itemToDisplay.thumbnail;

            itemToDisplayImage.gameObject.SetActive(true);
        }
        else
        {
            itemToDisplayImage.gameObject.SetActive(false);
            this.itemToDisplay = null;
        }
    }
}

 public void Display(ItemData item){}

ItemData类传递过来的物品信息 item,通过this.itemToDisplay = item;这行代码,将传入的item对象赋值给了类的成员变量itemToDisplay

(2) 在Inventory Item预制体中赋值并保存

(3) Create Empty,命名为Manager,添加InventoryManager.cs组件,定义存储物品信息的数组

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : MonoBehaviour
{ 
    //单例
    public static InventoryManager Instance;
    //单例
    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    //单例
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }

    public ItemData[] inventoryItems = new ItemData[9]; // 存储物品信息的数组
    public ItemData equippedItem = null;
}

(4) 赋值

(5) 给Manager添加UIManager.cs组件,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    //单例
    public static UIManager Instance { get; private set; }
    //单例
    private void Awake()
    {
        if (Instance != null && Instance !=this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    //单例
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }

    //由物品栏中的物品组成的数组,这些物品在执行 Display()方法时显示缩略图
    public ItemHover[] itemToDisplays;

    private void Start()
    {
        RenderInventory();
    }

    public void RenderInventory()
    {
        //储存所有物品信息的数组,这一数组由InventoryManager定义,它会向RenderInventoryImg()方法传递一个参数
        ItemData[] inventoryItemToDisplays = InventoryManager.Instance.inventoryItems;

        RenderInventoryImg(inventoryItemToDisplays, itemToDisplays);
    }
    //显示物品对应的缩略图,参数为物品信息(由InventoryManager定义)和物品对应的缩略图(由ItemHover定义)
    //显示ItemData类的一个对象数组所包含的缩略图
    public void RenderInventoryImg(ItemData[] slots, ItemHover[] UiSlots)
    {
        for (int i = 0; i < UiSlots.Length; i++)
        {
            UiSlots[i].Display(slots[i]);
        }
    }
}

(6) 赋值

小结

ItemHover.cs 确定某一个物品的缩略图

InventoryManager.cs 定义物品信息(ItemData)组,并为组内元素分配对应物品(ItemData)

UIManager.cs 定义附有ItemHover.cs的游戏物体同时传递参数,关联游戏物体与ItemData信息

效果图(每一个物品位都有一个设定物品缩略图显示)

8、显示物品名称和特征

(1) 打开UIManager.cs,设置显示物品文本信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    //单例
    public static UIManager Instance { get; private set; }
    //单例
    private void Awake()
    {
        if (Instance != null && Instance !=this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    //单例
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }

    //由物品栏中的物品组成的数组,这些物品在执行 Display()方法时显示缩略图
    public ItemHover[] itemToDisplays;

    //********设置物品文本信息*******//
    public Text ItemNameText;
    public Text ItemDescriptionText;

    private void Start()
    {
        RenderInventory();
    }

    public void RenderInventory()
    {
        //储存所有物品信息的数组,这一数组由InventoryManager定义,它会向RenderInventoryImg()方法传递一个参数
        ItemData[] inventoryItemToDisplays = InventoryManager.Instance.inventoryItems;

        RenderInventoryImg(inventoryItemToDisplays, itemToDisplays);
    }
    //显示物品对应的缩略图,参数为物品信息(由InventoryManager定义)和物品对应的缩略图(由ItemHover定义)
    //显示ItemData类的一个对象数组所包含的缩略图
    public void RenderInventoryImg(ItemData[] slots, ItemHover[] UiSlots)
    {
        for (int i = 0; i < UiSlots.Length; i++)
        {
            UiSlots[i].Display(slots[i]);
        }
    }

    //*******显示物品文本信息*********//
    public void DisplayItemText(ItemData data)
    {
        if(data == null)
        {
            ItemNameText.text = "";
            ItemDescriptionText.text = "";
            return;
        }
        else
        {
            ItemNameText.text = data.name;
            ItemDescriptionText.text = data.description;
        }
    }
}

(2) 赋值

(3) 打开ItemHover.cs,设置鼠标悬停效果(执行UIManager中的显示文本)

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    ItemData itemToDisplay;//引用ItemData对象(包含物品栏中的一个物品的信息)
    public Image itemUIImage;

    //显示物品缩略图
    public void Display(ItemData item)
    {
        if (item != null && item.thumbnail !=null)
        {
            this.itemToDisplay = item;
            itemUIImage.sprite = itemToDisplay.thumbnail;

            itemUIImage.gameObject.SetActive(true);
        }
        else
        {
            itemUIImage.gameObject.SetActive(false);
            this.itemToDisplay = null;
        }
    }

    //*******设置鼠标悬停时执行UIManager类的DisplayItemText方法显示和隐藏文本********//
    public void OnPointerEnter(PointerEventData eventData)
    {
        UIManager.Instance.DisplayItemText(itemToDisplay);
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        UIManager.Instance.DisplayItemText(null);
    }
}

9、设置装备物品的图片

(1) 打开InventoryManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : MonoBehaviour
{
    public static InventoryManager Instance;
    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }
    public ItemData[] inventoryItems = new ItemData[9]; 
    //已装备物品(默认不存在,不用赋值)
    public ItemData equippedItem = null;
}

(2) 打开UIManager.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    //单例
    public static UIManager Instance { get; private set; }
    //单例
    private void Awake()
    {
        if (Instance != null && Instance !=this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    //单例
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }

    //由物品栏中的物品组成的数组,这些物品在执行 Display()方法时显示缩略图
    public ItemHover[] itemToDisplays;

    //222***设置装备物品********222//
    public Image EquippedItemUIImage;

    //********设置物品文本信息*******//
    public Text ItemNameText;
    public Text ItemDescriptionText;

    private void Start()
    {
        RenderInventory();
    }

    public void RenderInventory()
    {
        //储存所有物品信息的数组,这一数组由InventoryManager定义,它会向RenderInventoryImg()方法传递一个参数
        ItemData[] inventoryItemToDisplays = InventoryManager.Instance.inventoryItems;

        RenderInventoryImg(inventoryItemToDisplays, itemToDisplays);

        //22222******引用InventoryManager方法中定义的变量equippedItem
        ItemData equippedItem = InventoryManager.Instance.equippedItem;
        //222***设置装备物品的缩略图********222//
        if (equippedItem != null && equippedItem != this)
        {
            EquippedItemUIImage.sprite = equippedItem.thumbnail;
            EquippedItemUIImage.gameObject.SetActive(true);
        }
        else
        {
            EquippedItemUIImage.gameObject.SetActive(false);
        }
    }

    //显示物品对应的缩略图,参数为物品信息(由InventoryManager定义)和物品对应的缩略图(由ItemHover定义)
    //显示ItemData类的一个对象数组所包含的缩略图
    public void RenderInventoryImg(ItemData[] slots, ItemHover[] UiSlots)
    {
        for (int i = 0; i < UiSlots.Length; i++)
        {
            UiSlots[i].Display(slots[i]);
        }
    }

    //*******显示物品文本信息*********//
    public void DisplayItemText(ItemData data)
    {
        if(data == null)
        {
            ItemNameText.text = "";
            ItemDescriptionText.text = "";
            return;
        }
        else
        {
            ItemNameText.text = data.name;
            ItemDescriptionText.text = data.description;
        }
    }
}

(3) 赋值及效果图

10、设置玩家装备物品

(1) 打开InventoryManager.cs 创建装备物品与物品栏物品互换的方法

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : MonoBehaviour
{
    public static InventoryManager Instance;
    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }

    public ItemData[] inventoryItems = new ItemData[9]; 
    //已装备物品
    public ItemData equippedItem = null;

    //3333333333***********装备物品与物品栏物品的替换
    public void InventoryToHand()
    {
        
    }
    public void HandToInventory()
    {
        
    }
}

(2) 打开ItemHover.cs,设置点击事件

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

//2222222222*******增加点击********
public class ItemHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler,IPointerClickHandler
{
    ItemData itemToDisplay;//引用ItemData对象(包含物品栏中的一个物品的信息)
    public Image itemUIImage;

    //显示物品缩略图
    public void Display(ItemData item)
    {
        if (item != null && item.thumbnail != null)
        {
            this.itemToDisplay = item;
            itemUIImage.sprite = itemToDisplay.thumbnail;

            itemUIImage.gameObject.SetActive(true);
        }
        else
        {
            itemUIImage.gameObject.SetActive(false);
            this.itemToDisplay = null;
        }
    }
    //22222*********设置点击事件********//
    public virtual void OnPointerClick(PointerEventData eventData)
    {
        InventoryManager.Instance.InventoryToHand();
    }

    //*******设置鼠标悬停时执行UIManager类的DisplayItemText方法显示和隐藏文本********//
    public void OnPointerEnter(PointerEventData eventData)
    {
        UIManager.Instance.DisplayItemText(itemToDisplay);
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        UIManager.Instance.DisplayItemText(null);
    }
}

(3) 在UI文件夹新建 HandItemHover.cs 作为ItemHover.cs的子类,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

//继承ItemHover
public class HandItemHover : ItemHover
{
    //点击时将手中物品放入背包
    public override void OnPointerClick(PointerEventData eventData)
    {
        InventoryManager.Instance.HandToInventory();
    }
}

当使用HandItemHover类的实例作为点击目标时,它将执行自己的OnPointerClick方法,而不是父类的。

解析

ItemHover 类:当点击一个物品(可能是在背包或其他UI元素中)时,该物品会被取出并放到手上

HandItemHover 类:当手上已经有物品,并且你点击了某个可以放置物品的地方(可能是背包的UI或其他)时,手上的物品会被放回背包

(4) 打开ItemHover.cs,接受被选中的物品的索引值(使被选中的物品有一个索引值)

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

//2222222222*******增加点击********
public class ItemHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler,IPointerClickHandler
{
    ItemData itemToDisplay;//引用ItemData对象(包含物品栏中的一个物品的信息)
    public Image itemUIImage;

    //33333333***********被选中的物体所处的索引***********
    int itemSelectedIndex;

    //显示物品缩略图
    public void Display(ItemData item)
    {
        if (item != null && item.thumbnail != null)
        {
            this.itemToDisplay = item;
            itemUIImage.sprite = itemToDisplay.thumbnail;

            itemUIImage.gameObject.SetActive(true);
        }
        else
        {
            itemUIImage.gameObject.SetActive(false);
            this.itemToDisplay = null;
        }
    }
    //22222*********设置点击事件********//
    public virtual void OnPointerClick(PointerEventData eventData)
    {
        InventoryManager.Instance.InventoryToHand();
    }

    //333333333*******接收一个索引值,这个索引值在调用AssignIndex方法时被分配。
    //接收(物品栏中)被选中的物品的索引值*************

    public void AssignIndex(int itemSelectedIndex)
    {
        this.itemSelectedIndex = itemSelectedIndex;
    }

    //*******设置鼠标悬停时执行UIManager类的DisplayItemText方法显示和隐藏文本********//
    public void OnPointerEnter(PointerEventData eventData)
    {
        UIManager.Instance.DisplayItemText(itemToDisplay);
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        UIManager.Instance.DisplayItemText(null);
    }
}

(5) 打开InventoryManager,实现装备物品与物品栏中的物品交换

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryManager : MonoBehaviour
{
    public static InventoryManager Instance;
    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
        }
    }
    
    private void OnDestroy()
    {
        if (Instance == this)
        {
            Instance = null;
        }
    }

    public ItemData[] inventoryItems = new ItemData[9]; 
    //已装备物品
    public ItemData equippedItem = null;

    //3333333333***********装备物品与物品栏物品的交换
    public void InventoryToHand(int itemSelectedIndex)
    {
        //取出物品栏中的物品(位置A)缓存到itemToHand这个临时变量(临时位置)中
        ItemData itemToHand = inventoryItems[itemSelectedIndex];
        //把已装备的物品转移到移出的物品的位置(位置A)上
        inventoryItems[itemSelectedIndex] = equippedItem;
        //把临时变量中的物品从临时位置转移到装备物品的位置上
        equippedItem = itemToHand;
        //更新缩略图
        UIManager.Instance.RenderInventory();
    }
    public void HandToInventory()
    {
        Debug.Log("放回物品");
    }
}

(6) 设置装备物品

UIManager.cs

在Canvas下,我有一个含有9个物品的物品栏(Warehouse),每个物品(Inventory Items预制体)是由父物体Inventory Items(空白背景图片)和子物体ItemUI(物体缩略图)组成。
在物品栏外我还设置了一个已被装备的物品位置Equipped Item。
含有9个物品的物品栏是通过给Warehouse添加Grid Layout Group组件后,由预制体(Inventory Items)复制生成的。
已被装备的物品的位置是将预制体Inventory Items拖进Canvas实现的
我还设置了一个物品名称文本ItemName和一个物品描述文本用来承载物品名称和物品特征的描述

现在我想实现以下功能,具体应该怎么操作?
功能1、每个物品都有一个Item说明。这个我通过创建ItemData.cs,在Unity中创建ItemData物体已经实现,并且给每一个物体都添加了description描述说明,添加了缩略图thumbnail图标

功能2、显示物品栏时,物品栏中的每个物品都显示对应的缩略图
功能3、当鼠标滑进物品栏中的物品时,在ItemName和ItemDescription上分别显示对应物体的名称和物品特征描述,鼠标滑出时,对应文本上的文字消失
功能4、单击物品栏中的物品时(玩家选中物品并装备物品)在Equipped Item中的ItemUI显示该物品的图标,再次单击该物品,Equipped Item中的ItemUI不显示任何图标
功能5、当玩家已装备物品时,单击物品栏中的另外物品(玩家选中另外物品),Equipped Item中的ItemUI显示当前被选中物体,原已装备物品回到物品栏

AI答案2

您的描述很清楚,以下是每个功能的具体操作:

功能1:为每个物品添加Item说明

  • 创建一个名为ItemData.cs的脚本,将其附加到每个物品的父对象上(Inventory Items预制体)。
  • 在ItemData.cs脚本中添加一个public string类型的变量description。
  • 在Unity编辑器中,为每个物品的ItemData脚本的description变量分别赋值,填写对应的物品说明。

功能2:显示物品栏中的物品缩略图

  • 在物品栏的Inventory Items预制体上,创建一个子对象ItemUI,并将其命名为Thumbnail。
  • 在ItemUI上添加一个Image组件,并将其Sprite属性设置为对应物品的缩略图。
  • 确保ItemUI的RectTransform组件的大小适合缩略图。

功能3:鼠标滑进/滑出物品栏物品时显示/隐藏物品名称和特征描述

  • 创建一个名为ItemHover.cs的脚本,并将其附加到每个物品的父对象上(Inventory Items预制体)。
  • 在ItemHover.cs脚本中,使用Unity的事件触发器(Event Trigger)来监听鼠标进入和退出事件。
  • 当鼠标进入时,通过代码将ItemName和ItemDescription的文本内容设置为对应物品的名称和特征描述。
  • 当鼠标退出时,通过代码将ItemName和ItemDescription的文本内容设置为空字符串。

功能4:单击物品栏中的物品时,在Equipped Item中显示/隐藏物品图标

  • 创建一个名为ItemEquip.cs的脚本,并将其附加到每个物品(Inventory Items预制体)的父对象上。
  • 在ItemEquip.cs脚本中,使用Unity的事件触发器(Event Trigger)来监听鼠标点击事件。
  • 当鼠标单击时,切换Equipped Item中ItemUI的图标显示状态。如果ItemUI已显示图标,则隐藏它;如果ItemUI未显示图标,则显示对应物品的图标。

功能5:单击物品栏中的其他物品时,在Equipped Item中显示被选中物体,并将原装备物品放回物品栏

  • 修改ItemEquip.cs脚本,在鼠标点击事件的处理代码中,添加以下逻辑:
    • 如果Equipped Item中的ItemUI已显示图标,则将其隐藏,并将当前物品放回物品栏。
    • 如果Equipped Item中的ItemUI未显示图标,则将其显示,并将当前物品移动到Equipped Item的位置。

希望以上步骤能帮助您实现所需的功能。如果有任何进一步的问题,请随时提问。

AI答案3

当然,请参考以下详细的操作和代码:

功能1:为每个物品添加Item说明

  1. 在Unity中创建一个名为ItemData.cs的脚本,并将其附加到每个物品的父对象上(Inventory Items预制体)。
  2. 在ItemData.cs脚本中添加一个public string类型的变量description。
  3. 在Unity编辑器中,为每个物品的ItemData脚本的description变量分别赋值,填写对应的物品说明。
// ItemData.cs

using UnityEngine;

public class ItemData : MonoBehaviour
{
    public string description;
}

功能2:显示物品栏中的物品缩略图

  1. 在物品栏的Inventory Items预制体上,创建一个子对象ItemUI,并将其命名为Thumbnail。
  2. 在ItemUI上添加一个Image组件,并将其Sprite属性设置为对应物品的缩略图。
  3. 确保ItemUI的RectTransform组件的大小适合缩略图。

功能3:鼠标滑进/滑出物品栏物品时显示/隐藏物品名称和特征描述

  1. 创建一个名为ItemHover.cs的脚本,并将其附加到每个物品的父对象上(Inventory Items预制体)。
  2. 在ItemHover.cs脚本中添加以下代码:
// ItemHover.cs

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public Text itemNameText;
    public Text itemDescriptionText;
    
    private ItemData itemData;
    
    private void Awake()
    {
        itemData = GetComponentInChildren<ItemData>();
    }
    
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (itemData != null)
        {
            itemNameText.text = itemData.name;
            itemDescriptionText.text = itemData.description;
        }
    }
    
    public void OnPointerExit(PointerEventData eventData)
    {
        itemNameText.text = "";
        itemDescriptionText.text = "";
    }
}

功能4:单击物品栏中的物品时,在Equipped Item中显示/隐藏物品图标

  1. 创建一个名为ItemEquip.cs的脚本,并将其附加到每个物品(Inventory Items预制体)的父对象上。
  2. 在ItemEquip.cs脚本中添加以下代码:
// ItemEquip.cs

using UnityEngine;
using UnityEngine.EventSystems;

public class ItemEquip : MonoBehaviour, IPointerClickHandler
{
    public GameObject equippedItem;
    public GameObject inventoryItem;
    
    private bool isEquipped = false;
    
    public void OnPointerClick(PointerEventData eventData)
    {
        if (isEquipped)
        {
            equippedItem.SetActive(false);
            isEquipped = false;
        }
        else
        {
            equippedItem.SetActive(true);
            isEquipped = true;
        }
    }
}

功能5:单击物品栏中的其他物品时,在Equipped Item中显示被选中物体,并将原装备物品放回物品栏

  1. 修改ItemEquip.cs脚本,在OnPointerClick方法的处理代码中添加以下逻辑:
// ItemEquip.cs

using UnityEngine;
using UnityEngine.EventSystems;

public class ItemEquip : MonoBehaviour, IPointerClickHandler
{
    public GameObject equippedItem;
    public GameObject inventoryItem;
    
    private bool isEquipped = false;
    
    public void OnPointerClick(PointerEventData eventData)
    {
        if (isEquipped)
        {
            equippedItem.SetActive(false);
            inventoryItem.SetActive(true); // 将原装备物品放回物品栏
            isEquipped = false;
        }
        else
        {
            if (equippedItem.activeSelf)
            {
                equippedItem.SetActive(false);
                inventoryItem.SetActive(true); // 将原装备物品放回物品栏
            }
            
            inventoryItem.SetActive(false);
            equippedItem.SetActive(true);
            isEquipped = true;
        }
    }
}

希望以上代码和操作对您有所帮助。如果还有任何问题,请随时提问。

AI答案1

功能1:创建ItemData脚本并添加description和thumbnail变量,为每个物品添加对应的描述和缩略图。

  1. 在Unity中创建一个C#脚本,命名为ItemData,并在脚本中添加以下代码:
using UnityEngine;

[CreateAssetMenu(fileName = "ItemData", menuName = "Inventory/ItemData", order = 1)]
public class ItemData : ScriptableObject
{
    public string description;
    public Sprite thumbnail;
}

  1. 创建一个新的Scriptable Object,在项目视图中右键点击 -> Create -> Inventory -> ItemData。在Inspector中给该ItemData对象添加描述和缩略图。

  2. 将ItemData对象拖放到相应的Inventory Item预制体上,确保每个物品都有对应的ItemData。

功能2:鼠标滑入物品栏中的物品时,在ItemName和ItemDescription上显示对应物体的名称和物品特征描述,鼠标滑出时,对应文本上的文字消失。

  1. 在Inventory Item预制体上添加一个脚本,命名为ItemHover。在脚本中添加以下代码:
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class ItemHover : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public Text itemNameText;
    public Text itemDescriptionText;

    public void OnPointerEnter(PointerEventData eventData)
    {
        ItemData itemData = GetComponent<ItemData>();
        itemNameText.text = itemData.name;
        itemDescriptionText.text = itemData.description;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        itemNameText.text = "";
        itemDescriptionText.text = "";
    }
}

  1. 在预制体的ItemUI子物体上添加一个Text组件,并将其分别拖放到ItemHover脚本中的itemNameText和itemDescriptionText字段上。

功能3:单击物品栏中的物品时,在Equipped Item中的ItemUI显示该物品的图标,再次单击该物品,Equipped Item中的ItemUI不显示任何图标。

  1. 在Inventory Item预制体上添加一个脚本,命名为ItemEquipToggle。在脚本中添加以下代码:
using UnityEngine;
using UnityEngine.EventSystems;

public class ItemEquipToggle : MonoBehaviour, IPointerClickHandler
{
    public GameObject itemUI;
    public GameObject equippedItemUI;

    private bool isEquipped = false;

    public void OnPointerClick(PointerEventData eventData)
    {
        if (isEquipped)
        {
            equippedItemUI.SetActive(false);
        }
        else
        {
            equippedItemUI.SetActive(true);
        }
        isEquipped = !isEquipped;
    }
}

  1. 在预制体中的空白背景图片上添加一个Image组件,并将其拖放到ItemEquipToggle脚本中的itemUI字段上。

  2. 在Equipped Item中的ItemUI物体上添加一个Image组件,并将其拖放到ItemEquipToggle脚本中的equippedItemUI字段上。

功能4:当玩家已装备物品时,单击物品栏中的另外物品,Equipped Item中的ItemUI显示当前被选中物体,原已装备物品回到物品栏。

  1. 在Inventory Item预制体上添加一个脚本,命名为ItemSelectToggle。在脚本中添加以下代码:
using UnityEngine;
using UnityEngine.EventSystems;

public class ItemSelectToggle : MonoBehaviour, IPointerClickHandler
{
    public GameObject itemUI;
    public GameObject equippedItemUI;

    private bool isSelected = false;

    public void OnPointerClick(PointerEventData eventData)
    {
        itemUI.SetActive(true);
        equippedItemUI.SetActive(false);
    }
}

  1. 在预制体中的空白背景图片上添加一个Image组件,并将其拖放到ItemSelectToggle脚本中的itemUI字段上。

  2. 在Equipped Item中的ItemUI物体上添加一个Image组件,并将其拖放到ItemSelectToggle脚本中的equippedItemUI字段上。

现在,当你在物品栏中测试点击和鼠标滑入操作时,应该能够实现所描述的功能。如有问题,请及时与我联系。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1961478.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Spring boot tomcat 读写超时时间设置

yaml配置 connection-timeout: 20000 server:port: 9898servlet:context-path: /testtomcat:connection-timeout: 20000max-connections: 250accept-count: 300 spring源码设置自定义tomcat参数 customizeConnector(connector); Overridepublic WebServer getWebServer(Serv…

【MySQL】表的约束{ 常见约束 空属性 默认值 列描述comment zerofill 主键 复合主键 自增长 唯一键 外键 }

文章目录 常见约束空属性默认值列描述commentzerofill主键复合主键自增长唯一键外键 2.总结 真正约束字段的是数据类型&#xff0c;但是数据类型约束很单一&#xff0c;需要有一些额外的约束&#xff0c;更好的保证数据的合法性&#xff0c;从业务逻辑角度保证数据的正确性。比…

MySQL基础练习题12-使用唯一标识码替换员工ID

题目&#xff1a;展示每位用户的 唯一标识码&#xff08;unique ID &#xff09;&#xff1b;如果某位员工没有唯一标识码&#xff0c;使用 null 填充即可。 准备数据 分析数据 题目&#xff1a;展示每位用户的 唯一标识码&#xff08;unique ID &#xff09;&#xff1b;如果…

一, 创建工程,引入依赖

一&#xff0c; 创建工程&#xff0c;引入依赖 文章目录 一&#xff0c; 创建工程&#xff0c;引入依赖创建工程工程间的关系的建立配置各个工程当中的 pow 配置信息&#xff0c;相关的依赖父工程(也就是总项目工程)的 pow 配置demo-module06-generate 模块中pow 配置&#xff…

基于IEC61499标准的在线工业编程平台open61499

基于IEC61499标准的在线工业编程平台open61499是一个专为工业自动化领域设计的编程环境&#xff0c;它遵循IEC 61499标准&#xff0c;为开发者提供了一种高效、灵活的方式来创建、配置和管理分布式控制系统&#xff08;DCS&#xff09;的应用程序。以下是对open61499的详细解析…

LeetCode热题 翻转二叉树、二叉树最大深度、二叉树中序遍历

目录 一、翻转二叉树 1.1 题目链接 1.2 题目描述 1.3 解题思路 二、二叉树最大深度 2.1 题目链接 2.2 题目描述 2.3 解题思路 三、二叉树中序遍历 3.1 题目链接 3.2 题目描述 3.3 解题思路 一、翻转二叉树 1.1 题目链接 翻转二叉树 1.2 题目描述 1.3 解题思路 根…

【多模态大模型】 BLIP in ICML 2022

一、引言 论文&#xff1a; BLIP: Bootstrapping Language-Image Pre-training for Unified Vision-Language Understanding and Generation 作者&#xff1a; Salesforce Research 代码&#xff1a; BLIP 特点&#xff1a; 该方法分别使用ViT和BERT进行图像和文本特征提取&am…

【changchain-community安装失败】‘EntryPoints‘ object has no attribute ‘get‘报错解决

在安装changchain-community时报错信息如下&#xff1a; WARNING: Keyring is skipped due to an exception: EntryPoints object has no attribute get ERROR: Could not find a version that satisfies the requirement changchain-community ERROR: No matching distributio…

进程间通信与线程间通信的方法汇总

目录 一、进程间通信机制 管道(pipe)&#xff1a; 命名管道(FIFO)&#xff1a; 消息队列(MQ)&#xff1a; 信号量(semaphore)&#xff1a; 共享内存(shared memory)&#xff1a; 信号(signal)&#xff1a; 内存映射(mapped memory)&#xff1a; 内存映射和共享内存的区…

华杉研发九学习日记20 LinkedHashMap TreeMap Arrays 函数式接口 方法引用

华杉研发九学习日记20 一&#xff0c;LinkedHashMap 与HashMap相比&#xff0c;key是有序的 Map<Integer,String> map new LinkedHashMap<Integer,String>(); map.put(1, "one"); map.put(2, "two"); map.put(3, "three"); map.…

GitHub Desktop commit文件到repository

1. Clone a repository到本地 2. 在本地仓库修改/添加需要提交的文件或者文档 3. 添加comments并commit 4. 提交完成&#xff0c;点击Push origin提交代码到Github远程仓库 上传成功后&#xff0c;刷新Github网站页面就会出现上传的项目

鸿蒙应用框架开发【自绘编辑框】 输入法框架

自绘编辑框 介绍 本示例通过输入法框架实现自会编辑框&#xff0c;可以绑定输入法应用&#xff0c;从输入法应用输入内容&#xff0c;显示和隐藏输入法。 效果预览 使用说明 1.点击编辑框可以绑定并拉起输入法&#xff0c;可以从输入法键盘输入内容到编辑框。 2.可以点击a…

SSM老人服务管理系统小程序-计算机毕业设计源码91022

摘 要 21世纪的今天&#xff0c;随着社会的不断发展与进步&#xff0c;人们对于信息科学化的认识&#xff0c;已由低层次向高层次发展&#xff0c;由原来的感性认识向理性认识提高&#xff0c;管理工作的重要性已逐渐被人们所认识&#xff0c;科学化的管理&#xff0c;使信息存…

跨网段 IP 地址通信故障分析

现如今计算机网络的规模和复杂性不断增加&#xff0c;跨网段通信成为网络运行中的常见需求。但如果设备处于不同网段且路由设置出现偏差时就会导致通信故障&#xff0c;严重影响网络的正常运行和数据传输。 1.跨网段通信的基本原理 跨网段通信依赖于路由器的路由功能。路由器根…

vue3.0 入门基础知识汇总【1】 全面 精简 推荐

这篇博文主要对一些刚入门vue框架的同学&#xff0c;以及对vue基本知识进行巩固的&#xff0c;最后就是精简一下基本知识&#xff0c;以方便自己查看&#xff0c;感谢参考&#xff0c;有问题评论区交流&#xff0c;谢谢。 目录 1.component组件的基本结构和使用 2.method方法…

全网最适合入门的面向对象编程教程:28 类和对象的Python实现-Python编程原则、哲学和规范大汇总

全网最适合入门的面向对象编程教程&#xff1a;28 类和对象的 Python 实现-Python 编程原则、哲学和规范大汇总 摘要&#xff1a; 本文主要介绍了在使用 Python 进行面向对象编程时&#xff0c;Python 异常处理的原则-“请求谅解&#xff0c;而非许可”&#xff0c;以及软件设…

什么是安全编程?

安全编程&#xff08;Secure Programming&#xff09;是一种编程方法论&#xff0c;旨在通过编写安全可靠的代码来保护计算机系统和数据的安全性。它涵盖了软件设计、开发、测试和维护的整个生命周期&#xff0c;旨在最大程度地降低软件漏洞和安全缺陷的风险。以下是对安全编程…

【前端 20】Element-UI快速入门

探索Element UI组件库&#xff1a;快速搭建Vue应用的必备工具 在现代Web开发中&#xff0c;Vue.js以其轻量级和灵活性赢得了广泛的关注。而Element UI&#xff0c;作为Vue.js的一个UI组件库&#xff0c;更是为开发者们提供了丰富、易用的前端组件&#xff0c;极大地加速了开发过…

Spring源码(八)--Spring实例化的策略

Spring实例化的策略有几种 &#xff0c;可以看一下 InstantiationStrategy 相关的类。 UML 结构图 InstantiationStrategy的实现类有 SimpleInstantiationStrategy。 CglibSubclassingInstantiationStrategy 又继承了SimpleInstantiationStrategy。 InstantiationStrategy I…

Java----反射

什么是反射&#xff1f; 反射就是允许对成员变量、成员方法和构造方法的信息进行编程访问。换句话来讲&#xff0c;就是通过反射&#xff0c;我们可以在不需要创建其对象的情况下就可以获取其定义的各种属性值以及方法。常见的应用就是IDEA中的提示功能&#xff0c;当我…