欢迎来到天天文库
浏览记录
ID:39891194
大小:1.17 MB
页数:6页
时间:2019-07-14
《unity3d游戏开发之背包系统(2)》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、背包系统(二)7、拖入两个物品预设体到格子里面,并为其添加数量8、修改物品脚本,实现两个物品可以交换 //重写OnDragDropRelease方法 protectedoverridevoidOnDragDropRelease(GameObjectsurface) { base.OnDragDropRelease(surface);//调用父类的OnDragDropRelease(surface)方法 if(surface.tag=="Cell") { this.transform.parent=surface.transform;//把背包放入格
2、子里面 this.transform.localPosition=Vector3.zero;//把背包居中 } elseif(surface.tag=="Knapsack") { //第一个背包:拖动的背包 //第二个背包:将要被交换的背包 Transformparent=surface.transform.parent;//得到第二个格子Transform对象 surface.transform.parent=this.transform.parent;//把第二个背包放入第一个格子里面 surfac
3、e.transform.localPosition=Vector3.zero;//把第二个背包居中 this.transform.parent=parent;//把第一个背包放入第二个格子里面 this.transform.localPosition=Vector3.zero;//把第一个背包居中 } }9、修改格子脚本,实现按下F键,随机产生物品【狗刨学习网】publicGameObject[]cells;//9个格子 publicstring[]knapsacksName;//三个物品的名称 publicGameObjectitem;//任意一
4、个物品 voidUpdate(){ if(Input.GetKeyDown(KeyCode.F)){//按下F键 PickUp();//调用PickUp() } } voidPickUp(){ intindex=Random.Range(0,knapsacksName.Length);//随机生成0,1,2三个数其中一个 stringname=knapsacksName[index];//得到随机的物品名称 for(inti=0;i5、childCount==0)//当前格子里面没有物品 { GameObjectgo=NGUITools.AddChild(cells[i],item);//把新生成的物品放入格子里面 go.GetComponent().spriteName=name;//为新生成的物品添加名称 go.transform.localPosition=Vector3.zero;//把物品居中 break; } }}这篇文章来自狗刨学习网
5、childCount==0)//当前格子里面没有物品 { GameObjectgo=NGUITools.AddChild(cells[i],item);//把新生成的物品放入格子里面 go.GetComponent().spriteName=name;//为新生成的物品添加名称 go.transform.localPosition=Vector3.zero;//把物品居中 break; } }}这篇文章来自狗刨学习网
此文档下载收益归作者所有