Unity插件NGUI实现背包系统
2016-10-01 10:30
507 查看
三十 背包系统的制作
1 创建一个Sprite作为背景图片
2 创建一个Sprite作为格子并保存为Prefab
3 在格子下创建Sprite作为物品
4 在物品下创建Label用来显示物品个数
5 把物品做成Prefab
7 给物品添加Box Collider
8 创建脚本继承UIDragDropItem指定给物品
using UnityEngine;
using System.Collections;
public class KnapsackItem : UIDragDropItem {
protected override void OnDragDropRelease(GameObject surface)
{
base.OnDragDropRelease(surface);
Debug.Log(surface.transform.childCount);//显示格子下面子物体有多少个
//if (surface.transform.childCount > 0)//当物品大于零时
if(surface.tag=="Ceil")//当格子上有物体时,surface将会改变,故用此来判断是否已经有物品
{//没有物品时
this.transform.parent = surface.transform;//将物品移到格子下边
this.transform.localPosition = Vector3.zero;//设置局部坐标为0,此时可以居中
}
else if(surface.tag=="KnapsackItem")
{//有物品时,进行交换
Transform parent = surface.transform.parent;//过度,将格子上的当前物品位置保存
surface.transform.parent = this.transform.parent;//将已经在格子上的物品移动到要交换的物品的格子
surface.transform.localPosition = Vector3.zero;//设置局部坐标为0\
this.transform.parent = parent;//把要交换的格子移过去
this.transform.localPosition = Vector3.zero;
}
}
}
9 创建脚本指定给格子
代码设计:
using UnityEngine;
using System.Collections;
public class KnapSack : MonoBehaviour {
public GameObject[] Ceils=new GameObject[16];//用来存放物品的格子数组
}
10 给格子进行编号并将格子复制给Ceils数组
11 设置Tag并指定给相应对象
1 创建一个Sprite作为背景图片
2 创建一个Sprite作为格子并保存为Prefab
3 在格子下创建Sprite作为物品
4 在物品下创建Label用来显示物品个数
5 把物品做成Prefab
7 给物品添加Box Collider
8 创建脚本继承UIDragDropItem指定给物品
using UnityEngine;
using System.Collections;
public class KnapsackItem : UIDragDropItem {
protected override void OnDragDropRelease(GameObject surface)
{
base.OnDragDropRelease(surface);
Debug.Log(surface.transform.childCount);//显示格子下面子物体有多少个
//if (surface.transform.childCount > 0)//当物品大于零时
if(surface.tag=="Ceil")//当格子上有物体时,surface将会改变,故用此来判断是否已经有物品
{//没有物品时
this.transform.parent = surface.transform;//将物品移到格子下边
this.transform.localPosition = Vector3.zero;//设置局部坐标为0,此时可以居中
}
else if(surface.tag=="KnapsackItem")
{//有物品时,进行交换
Transform parent = surface.transform.parent;//过度,将格子上的当前物品位置保存
surface.transform.parent = this.transform.parent;//将已经在格子上的物品移动到要交换的物品的格子
surface.transform.localPosition = Vector3.zero;//设置局部坐标为0\
this.transform.parent = parent;//把要交换的格子移过去
this.transform.localPosition = Vector3.zero;
}
}
}
9 创建脚本指定给格子
代码设计:
using UnityEngine;
using System.Collections;
public class KnapSack : MonoBehaviour {
public GameObject[] Ceils=new GameObject[16];//用来存放物品的格子数组
}
10 给格子进行编号并将格子复制给Ceils数组
11 设置Tag并指定给相应对象
相关文章推荐
- 【Unity】NGUI背包系统——背包整理按钮的实现
- RPG黑暗之光(5)NGUI实现背包系统初步
- unity背包系统中物体拖拽效果实现(初学者)
- NGUI简单背包系统的实现
- 关于Unity中NGUI的背包实现之Scrollview(基于Camera)
- NGUI实现背包系统
- 【Unity&NGUI】我的背包系统相关文章总览
- 【Unity】NGUI背包系统——背包整理按钮的改进
- 【Unity】基于MVC模式的背包系统 UGUI实现
- [unity]NGUI实现背包拖拽+使用EventDelegate回调
- 【Unity&NGUI】背包系统显示隐藏问题
- 【Unity&NGUI】背包系统物品的累加
- [Unity插件]背包系统插件
- [Unity&NGUI]背包系统崩溃的解决办法
- [插件资源] Inventory Pro 2.3.1 - Unity 5专用的背包系统
- [Unity]背包效果-使用NGUI实现物品的拖拽效果Drag
- 【unity】ngui事件系统实现参考
- NGUI简单背包系统的实现
- 利用Unity来实现插件开发
- .NET实现之(自己动手写高内聚插件系统)