Unity3D 背包系统
作业:
UI 效果制作(你仅需要实现以下效果之一)
- 进入 NGUI 官方网站,使用 UGUI 实现以下效果
实现效果
参考博客:
背包系统制作: https://blog.kinpzz.com/2016/05/21/unity3d-ugui-Inventory/
人物模型改变:https://blog.uwa4d.com/archives/avartar.html
这次作业,我融合和两篇博客的精髓,实现了官方样例的功能。
通过拖曳装备,可以改变人物身上的模型。完成度十分高。
背包系统的制作过程,师兄的博客里已经十分详细,不再赘述。
接下来介绍一下人物模型的改变。
在人物模型的改变中,将人物模型生成的类Director.cs设置为单例模式。
在Director.cs中:改变武器,只需要按照下面的例子,引用函数即可(身体的其他部位雷同)
1 2 3 4 5 6 7 8 9 10 11 12 13 | public void ChangeWeapon( int num) { if (!weapon_list[num]) { for ( int j = 0; j < weapon_list.Length; j++) { weapon_list[j] = false ; } weapon_list[num] = true ; character.ChangeWeapon( "ch_we_one_hou_" + index[num]); } } |
在DragItem.cs中,拖动位置的识别,是通过对应位置的名称改变的。所以Wear,和collection中Image名称一定要对应!不可以改变。Bag中的名称无所谓。
在DragItem.cs中,例如Weapon1只能拖动到Weapon位置,或者Weapon0和Weapon1位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | if (curEnter == null ) { myTransform.position = originalPosition; } else if (curEnter.GetComponent<DragItem>().inWear || inWear) { string Pre_thisName = this .name.Substring(0, this .name.Length - 1); string Pre_curEnterName = curEnter.name.Substring(0, this .name.Length - 1); //移动至物品格子上 if (curEnter.name == Pre_thisName) { UnityEngine.Debug.Log( "移动至物品格子上" ); UnityEngine.Debug.Log( this .name); UnityEngine.Debug.Log( this .transform.position); UnityEngine.Debug.Log(curEnter.name); UnityEngine.Debug.Log(curEnter.transform.position); if ( this .transform.position.x < 0) { string name = this .name; string num_s = name.Substring(name.Length - 1, 1); int num = Convert.ToInt32(num_s); changeBody(num, Pre_thisName, Pre_curEnterName); } myTransform.position = curEnter.transform.position; originalPosition = myTransform.position; curEnter.GetComponent<Image>().color = lastEnterNormalColor; //当前格子恢复正常颜色 curEnter.GetComponent<DragItem>().inWear = !curEnter.GetComponent<DragItem>().inWear; inWear = !inWear; } else if (Pre_curEnterName == Pre_thisName) { //移动至包裹中的其它物品上 UnityEngine.Debug.Log( "移动至包裹中的其它物品上" ); /*curEnter.name == eventData.pointerDrag.name &&*/ if (curEnter != eventData.pointerDrag) { UnityEngine.Debug.Log( "交换:this.name:" + this .name); UnityEngine.Debug.Log( this .transform.position); UnityEngine.Debug.Log( "交换:curEnter:" + curEnter.name); UnityEngine.Debug.Log(curEnter.transform.position); if ( this .transform.position.x < 0) { string name = this .name; string num_s = name.Substring(name.Length - 1, 1); int num = Convert.ToInt32(num_s); changeBody(num, Pre_thisName, Pre_curEnterName); } else if ( this .transform.position.x > 0) { string name = curEnter.name; string num_s = name.Substring(name.Length - 1, 1); int num = Convert.ToInt32(num_s); changeBody(num, Pre_thisName, Pre_curEnterName); } Vector3 targetPostion = curEnter.transform.position; curEnter.transform.position = originalPosition; myTransform.position = targetPostion; originalPosition = myTransform.position; curEnter.GetComponent<DragItem>().inWear = !curEnter.GetComponent<DragItem>().inWear; inWear = !inWear; } else //拖拽至其它对象上面(包裹上的其它区域) { myTransform.position = originalPosition; } } else { myTransform.position = originalPosition; } } else { myTransform.position = originalPosition; } |
具体代码可以参考我的gitee:
https://gitee.com/woodx9/unity-3D-homework/tree/master/PackSystem
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」