马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
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脚本
- using UnityEngine;
- public class Item : MonoBehaviour
- {
- [SerializeField]
- private int _itemCode;
- private SpriteRenderer spriteRenderer;
- public int ItemCode { get { return _itemCode; } set { _itemCode = value; } }
- private void Awake()
- {
- spriteRenderer = GetComponentInChildren<SpriteRenderer>(); // SpriteRenderer组件在子对象中
- }
- private void Start()
- {
- if(ItemCode != 0)
- {
- Init(ItemCode);
- }
- }
- public void Init(int ItemCodeParam)
- {
- }
- }
复制代码 在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脚本。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class InventoryManager : SingletonMonobehaviour<InventoryManager>
- {
- private Dictionary<int, ItemDetails> itemDetailsDictionary;
- [SerializeField] private SO_ItemList itemList = null;
- private void Start()
- {
- CreateItemDetailsDictionary();
- }
- /// <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>
- /// 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;
- }
- }
- }
复制代码 在PersistentScene中创建InventoryManager物体,同时将Assets -> ScriptableObjectAssets中的so_ItemList拖入ItemList的变量。

6、给Player添加感知脚本
在Assets -> Scripts -> Player下创建脚本ItemPickup.cs,其代码如下:
- using UnityEngine;
- public class ItemPickup : MonoBehaviour
- {
- private void OnTriggerEnter2D(Collider2D collision)
- {
- Item item = collision.GetComponent<Item>();
- if(item != null)
- {
- // Get item details
- ItemDetails itemDetails = InventoryManager.Instance.GetItemDetails(item.ItemCode);
- // Print item description to console
- Debug.Log(itemDetails.itemDescription);
- }
- }
- }
复制代码 然后,将该脚本挂到Player对象下。
运行程序,当Player在道具中穿逾期,Console会显示对应道具的信息:
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |