Unity3D仿星露谷物语开辟13之角色感知道具

打印 上一主题 下一主题

主题 976|帖子 976|积分 2928

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1、目的

在Scene中创建道具,角色靠近道具可以或许自动获取道具的信息。
ps:unity核心用法:


  • SerializeField:序列化某一个字段
  • Create -> Prefab Variant得到衍生预制体。
  • SingletonMonobehaviour:单例模式类,使用xx.Instance可获取实例
  • Dictionary.TryGetValue()方法:try and getvalue,乐成返回true,否则为false

2、创建Item底子预制体

在Scripts -> Item下创建Item.cs脚本
  1. using UnityEngine;
  2. public class Item : MonoBehaviour
  3. {
  4.     [SerializeField]
  5.     private int _itemCode;
  6.     private SpriteRenderer spriteRenderer;
  7.     public int ItemCode { get { return _itemCode; } set { _itemCode = value; } }
  8.     private void Awake()
  9.     {
  10.         spriteRenderer = GetComponentInChildren<SpriteRenderer>();  // SpriteRenderer组件在子对象中
  11.     }
  12.     private void Start()
  13.     {
  14.         if(ItemCode != 0)
  15.         {
  16.             Init(ItemCode);
  17.         }
  18.     }
  19.     public void Init(int ItemCodeParam)
  20.     {
  21.     }
  22. }
复制代码
在Hierarchy -> Scene1_Farm中创建Items空物体,在此物体下再创建Item空物体。
添加Box Collider 2D组件和Item脚本,属性如下:

在Hierarchy -> Scene1_Farm -> Items -> Item下再创建ItemSprite空物体,挂载Sprite Renderer组件信息如下:

在Assets -> Prefabs下新建Item目录。
然后将Hierarchy中的Item对象拖到Assets -> Prefabs -> Item目录下得到预制体。该预制体为最底子的预制体,后续会根据该底子预制体生成其他道具预制体。
此时,可以删除Hierarchy中的Item对象。
3、生成道具预制体

在Prefabs下新建Commodity目录,用于放置corn等道具预制体。
右击Prefabs -> Item下的Item预制体,Create -> Prefab Variant得到衍生预制体,命名为Corn。

并将该Corn预制体放到Commodity目录下。
接着给Corn添加Sprite:双击Corn后,在Hierarchy中点击ItemSprite,在Sprite Renderer中选择Sweetcorn图片。

然后再点击Hierarchy中的Corn,在Box Collider 2D中点击Edit Collider,调解碰撞检测的区域。

点击Assets -> Prefabs -> Commodity -> Corn,在Item中修改Item Code为10002。

以同样的方式在Commodity中添加Parsnip预制体,其Item Code为10007。
再以同样的方式在Commodity中添加Pumpkin预制体,其Item Code为10001。
现在,Commodity就有了3个道具的预制体。

4、Scene中放置道具

拖到道具预制体到Scene界面中,并将对象都同一移到Items目录下。

5、创建Inventory管理器

在Assets -> Scripts 下创建Inventory目录,并在其下创建InventoryManager.cs脚本。
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class InventoryManager : SingletonMonobehaviour<InventoryManager>
  5. {
  6.     private Dictionary<int, ItemDetails> itemDetailsDictionary;
  7.     [SerializeField] private SO_ItemList itemList = null;
  8.     private void Start()
  9.     {
  10.         CreateItemDetailsDictionary();
  11.     }
  12.     /// <summary>
  13.     /// Populates the itemDetailsDictionary from the scriptable object items list
  14.     /// </summary>
  15.     private void CreateItemDetailsDictionary()
  16.     {
  17.         itemDetailsDictionary = new Dictionary<int, ItemDetails>();
  18.         foreach (ItemDetails itemDetails in itemList.itemDetails)
  19.         {
  20.             itemDetailsDictionary.Add(itemDetails.itemCode, itemDetails);
  21.         }
  22.     }
  23.     /// <summary>
  24.     /// Returns the itemDetails (from the SO_ItemList) for the itemCode, or null if the item doesn't exist
  25.     /// </summary>
  26.     /// <param name="itemCode"></param>
  27.     /// <returns></returns>
  28.     public ItemDetails GetItemDetails(int itemCode)
  29.     {
  30.         ItemDetails itemDetails;
  31.         if(itemDetailsDictionary.TryGetValue(itemCode, out itemDetails))
  32.         {
  33.             return itemDetails;
  34.         }
  35.         else
  36.         {
  37.             return null;
  38.         }
  39.     }
  40. }
复制代码
在PersistentScene中创建InventoryManager物体,同时将Assets -> ScriptableObjectAssets中的so_ItemList拖入ItemList的变量。

6、给Player添加感知脚本

在Assets -> Scripts -> Player下创建脚本ItemPickup.cs,其代码如下:
  1. using UnityEngine;
  2. public class ItemPickup : MonoBehaviour
  3. {
  4.     private void OnTriggerEnter2D(Collider2D collision)
  5.     {
  6.         Item item = collision.GetComponent<Item>();
  7.         if(item != null)
  8.         {
  9.             // Get item details
  10.             ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(item.ItemCode);
  11.             // Print item description to console
  12.             Debug.Log(itemDetails.itemDescription);
  13.         }
  14.     }
  15. }
复制代码
然后,将该脚本挂到Player对象下。
运行程序,当Player在道具中穿逾期,Console会显示对应道具的信息:



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

徐锦洪

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表