Unity3D仿星露谷物语开发34之单击Drop项目

1、目标

当在道具栏中选中一个Item时,点击地面就可以实现Item的drop操作,每点击一次就drop一次,直到道具栏中Item数量不够。

这样的好处:避免每次Drop都从道具栏中拖拉Item,通过点击这种操作可以更加高效。

方法:通过EventHandler脚本创建Event,在Player脚本中处理点击并触发Event,在UIInvenrotySlot脚本中订阅Event进行处理。

2、优化EventHandler脚本

添加如下事件代码:

3、优化Player脚本

添加如下变量:

cs 复制代码
private GridCursor gridCursor;

添加Start方法:

cs 复制代码
private void Start()
{
    gridCursor = FindObjectOfType<GridCursor>();
}

在Update中添加如下代码:

其具体实现如下:

cs 复制代码
private void PlayerClickInput()
{
    if (Input.GetMouseButton(0))
    {
        if (gridCursor.CursorIsEnabled)
        {
            ProcessPlayerClickInput();
        }
    }
}

相关的代码实现如下:

cs 复制代码
 private void ProcessPlayerClickInput()
 {
     ResetMovement();

     // Get Selected item details
     ItemDetails itemDetails = InventoryManager.Instance.GetSelectedInventoryItemDetails(InventoryLocation.player);

     if(itemDetails != null)
     {
         switch (itemDetails.itemType)
         {
             case ItemType.Seed:
                 if (Input.GetMouseButtonDown(0))
                 {
                     ProcessPlayerClickInputSeed(itemDetails);
                 }
                 break;
             case ItemType.Commodity:
                 if (Input.GetMouseButtonDown(0))
                 {
                     ProcessPlayerClickInputCommodity(itemDetails);
                 }
                 break;
             case ItemType.none:
                 break;
             case ItemType.count:
                 break;

             default:
                 break;
         }
     }
 }

 private void ProcessPlayerClickInputSeed(ItemDetails itemDetails)
 {
     if(itemDetails.canBeDropped && gridCursor.CursorPositionIsValid)
     {
         EventHandler.CallDropSelectedItemEvent();
     }
 }

 private void ProcessPlayerClickInputCommodity(ItemDetails itemDetails)
 {
     if(itemDetails.canBeDropped && gridCursor.CursorPositionIsValid)
     {
         EventHandler.CallDropSelectedItemEvent();
     }
 }

4、优化UIInventorySlot脚本

在OnEnable方法中增加如下代码,订阅事件并进行处理。

5、运行程序

相关推荐
开开心心就好17 小时前
游戏安装报错缺环境?运行库整合包补上
java·mysql·游戏·pdf·ocr·音视频·lua
鹿野素材屋20 小时前
游戏开发(三):AI时代,如何快速还原一款竞品游戏
游戏
汉得数字平台1 天前
【汉得飞码】构建接口与UI自动化测试全链路,让验证反馈持续驱动交付
自动化测试·人工智能·ui
淡海水1 天前
02-02-原理篇-Scriban模板引擎原理深度解析
unity·excel·游戏开发·luban
一只大鸽子1 天前
游戏开发笔记19-Godot RPG游戏基础-文本样式
笔记·游戏·godot
兰亭妙微UI设计公司2 天前
兰亭妙微设计洞察:跳出界面视角,深度理解广义的全链路用户体验
ui·交互·ux
禁默2 天前
AI搓童年游戏-用华为云码道 CodeArts(Kimi-K3.1-Pro)从零写一个零依赖俄罗斯方块
人工智能·游戏·华为云
zhongben_keji2 天前
APP 本地数据与持久化体系全解:存储选型、SQLite WAL、数据库迁移与隐私清理
ui·ci/cd·自动化
2601_949695592 天前
显卡驱动版本过低玩不了游戏怎么办?如何检查最新驱动?
游戏·电脑·显卡·显卡驱动·驱动程序
loulanyue_2 天前
AI智能体24小时复刻Photoshop:什么该交给Agent、什么必须留给人——读Brandon Thomas演讲实录
人工智能·ui·rust·ai编程·photoshop