如何用ngui背包实现无限循环拖动背包

Unity3D NGUI从背包中拖出并在场景中生成物体 - CSDN博客
Unity3D NGUI从背包中拖出并在场景中生成物体
  由于游戏需要从背包中拖出武器并在场景中相应的位置生成出来,所以研究了一下这个。
  一般来说,在Unity3D开发中如果使用NGUI为游戏做UI,我们的场景和UI并不是使用一个相机进行渲染的,所以从背包中拖出物体并在场景相应的位置生成物体,就会涉及到UICamera和场景摄像机的转换。我在做这个时主要通过打射线来实现坐标转换。
  如何创建一个可拖动的UI组件可以参考NGUI中的Example 11&
  我们这里只需要重写DragDropItem这个类的OnDragDropRelease方法就可以了,下面是简化的关键代码
& & &&&protected override void OnDragDropRelease(GameObject surface)
    base.OnDragDropRelease(surface);
    //首先将拖动按钮时,按钮的位置通过UICamera转换为屏幕坐标
& & & & & Vector3&screenPoint = UICamera.WorldToScreenPoint(this.transform.position);
& & & & & /*然后我们需要在场景中建一个EmptyGameObject,并为其命名为InstantiatePos,为其挂上collider,
& & & & & *这里我们将InstantiatePos的layer设为Pos(我们自己创建的层,第9层),然后我们就能通过主摄像机在刚刚
    * 得到的屏幕坐标的位置打出一条射线,在这条射线有InstantiatePos发生碰撞的位置就是我们武器生成的位置,
    * 就可以在此处生成从UI生成的物体。
    */   
& & & & &int layerMask = 1 && 9;
& & & & &RayH
    float distance = 1000.0f;
   &if (!Physics.Raycast(mainCamera.ScreenPointToRay(screenPoint), out hit, distance, layerMask))
     &
& & & & & //生成武器的位置
& & & & & weapon.positon = hit.
本文已收录于以下专栏:
相关文章推荐
之前一篇博文介绍了Psd 2 Unity uGUI Pro这个插件(【Unity3d】将PSD直接导出成UGUI界面(一))。经过更长时间的使用之后发现一些需要注意的地方,也是这个插件可以更加完善的地...
将做好的unity项目发布成web版本,打开后发现出现的是自定义的unity的loading界面,如果修改成我们自己的的logo图案和加载进度条。
操作步骤:                    ...
游戏中,对象与对象之间需要交流,实现的方法多种,例如:可定义静态变量,其他脚本直接调用, 也可用到: SendMessage
今天主要学习SendMessage的用法。
1、创建两个脚本:“...
猴子原创,欢迎转载。转载请注明: 转载自Cocos2Der-CSDN,谢谢!原文地址: http://blog.csdn.net/cocos2der/article/details/
在《【Unity3D】公告栏与开始界面的布置》(点击打开链接)我曾经简单地提到UGUI是怎么布置与使用的,但这根本不完整,就像单身狗缺个另一半永远只能被喂狗粮似的,组件也因为事件的存在才能够精彩。因此...
实际上就是MOMO贴出来的方法其实是有问题的,用过的才知道。至于别的几乎不用找,根本一个抄一个,抄来抄去都是一样,没自己的东西。
用过MOMO那个方法的都应该会发现了,当进度条开始显示的时候,基本...
Unity3d以场景为单位,UI场景之间的背景音乐要一直播放,而场景切换之间的loading界面,需要在前一个场景执行某个按键进行击发,并在下个场景开始的时候消失。
虽然二者同时公共的需求,但具体还...
我在用Unity3D做一个回合制的战斗游戏,有两个场景(小镇的场景和战斗的场景),每当角色遇到怪兽,游戏就切换到战斗场景,角色就在这里战斗,直到打败怪兽或者被击败。
问题是怎...
单例是场景间切换时传递数据的最常见的方式之一,在unity中,很多方法被封装,有时候感觉不知道从何处看起,只能从官方文档上去根据功能找类了。个人感觉u3d可视化太过强烈,许多自定义的一些功能似乎让人无...
他的最新文章
讲师:吴岸城
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)NGUI简单背包系统的实现 - CSDN博客
NGUI简单背包系统的实现
/zhangbaochong/p/4820160.html
一、利用txt文件存储游戏物品信息
  首先在asset下创建一个txt文件,这里我们命名为objectsInfoList.txt,并将其拖放到unity Project视图中。
  其中txt中我们先存放一些物品信息,每行存储一种物品信息,分别为编号、名称、物品对应的图片名、种类、回血值、回蓝值、出售价和购买价。
  其中物品图片要先用NGUI做成图集,种类中的Drug为药品类,以后在代码中我们会定义一个枚举用于存储物品种类。
  接下来我们创建一个空物体叫做GameSetting,上面挂一个脚本ObjectsInfo,我们把txt文件读取到一个string中,根据'\n'及','分割字符串,取得对应的物品信息,然后存储到Dictionary中,以后需要物品信息时便可以从dictionary中取出来了。
  代码如下,就不做具体解释了。。。虽然注释少点,但还是挺简单的
1 using UnityE
2 using System.C
3 using System.Collections.G
5 public class ObjectsInfo : MonoBehaviour
public static ObjectsInfo _
public TextAsset objectsInfoListT
private Dictionary&int, ObjectInfo& objectInfoDictionary =
new Dictionary&int, ObjectInfo&();
void Awake()
_instance = this;
ReadInfo();
public ObjectInfo GetObjectInfoById(int key)
ObjectInfo info = new ObjectInfo();
objectInfoDictionary.TryGetValue(key, out info);
private void ReadInfo()
string text = objectsInfoListText.
string[] strArray = text.Split('\n');
foreach (string str in strArray)
string[] proArray = str.Split(',');
ObjectInfo info = new ObjectInfo();
int id = int.Parse(proArray[0]);
string name = proArray[1];
string iconName = proArray[2];
string typeStr = proArray[3];
info.name=
info.iconName=iconN
ObjectType type = ObjectType.D
switch (typeStr)
case &Drug&:
type = ObjectType.D
case &Equip&:
type = ObjectType.E
case &Mat&:
type = ObjectType.M
info.type=
if (type == ObjectType.Drug)
int hp = int.Parse(proArray[4]);
int mp = int.Parse(proArray[5]);
int priceSell = int.Parse(proArray[6]);
int priceBuy = int.Parse(proArray[7]);
info.priceBuy = priceB
info.priceSell = priceS
//添加到字典上,id为key,方便根据id查找
objectInfoDictionary.Add(id, info);
77 public enum ObjectType
84 //id,名称,icon名称,类型,加血值,加蓝值,卖出价,买入价
85 public class ObjectInfo
public int
public string
public string iconN
public ObjectT
public int
public int
public int priceS
public int priceB
  二、背包系统
  1、设计背包系统的UI界面
  主界面背景:新建一个sprite,选择相应图片,命名为Inventory
  格子:在Inventory下创建sprite,并在下面创建一个label用来显示物品数量,命名为NumLabel,然后做成prefab并复制多个
  其他:整理背包按钮、金钱图标及显示金币数量的label
  做出来的界面如下
  2、控制背包物品的管理
  &我们给Inventory添加一个脚本命名为Inventory,给格子添加一个脚本命名为InventoryItemGrid方便对格子的管理
  &在Inventory脚本中,定义一个List&InventoryItemGrid& itemGridList用于存放背包的所有格子,并在unity界面中将所有格子拖过去赋值(注意要按照格子的顺序赋值,所以格子创建完后,最好给格子分别命名一下如gide00,grid01.....,也方便以后的测试),并定义好金钱数目等变量;我们给背包添加一个tween动画,控制背包的显示与隐藏,并在脚本中提供相应方法,具体代码如下
1 using UnityE
2 using System.C
3 using System.Collections.G
5 public class Inventory : MonoBehaviour {
public static Inventory _
private TweenPosition tweenP
private int coinCount = 1000;
public UILabel coinCountL
public List&InventoryItemGrid& itemGridList = new List&InventoryItemGrid&();
// Use this for initialization
void Awake () {
_instance = this;
tweenPosition = GetComponent&TweenPosition&();
private bool isShow = false;
private void Show()
isShow = true;
tweenPosition.PlayForward();
private void Hide()
isShow = false;
tweenPosition.PlayReverse();
public void TransformState()
if (!isShow)
isShow = true;
isShow = false;
  3、背包方格物品的prefab制作
  在格子下创建一个sprite,可以随便选一张图片,调整大小使之适应格子大小,将其做成prefab,添加一个脚本命名为InventoryItem&
  由于我们的物品应该是可以拖动的,所以应attach一个Box Collider,脚本InventoryItem让其继承于UIDragDropItem 便可以实现拖拽功能了。定义一个int id用于设置要显示物品的id,并提供一个SetId方法控制对应图片的显示。代码如下: 
using UnityE
using System.C
public class InventoryItem : UIDragDropItem {
private UIS
private int
void Awake()
base.Awake();
sprite = this.gameObject.GetComponent&UISprite&();
public void SetId(int id)
ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);
sprite.spriteName = info.iconN
  4、控制方格对下面物品的管理
  &InventoryItemGrid脚本中定义变量id为物品的编号,num为物品的数量,以及UILabel控制物品数量的显示,并获取格子下面物品上的脚本InventoryItem调用上面的SetId方法,设置物品相应的图片。代码很简单。。。
1 using UnityE
2 using System.C
4 public class InventoryItemGrid : MonoBehaviour {
public int id = 0;
public int num = 0;
private ObjectInfo info = null;
private UILabel numL
void Start()
numLabel = this.GetComponentInChildren&UILabel&();
public void SetId(int id, int num = 1)
info = ObjectsInfo._instance.GetObjectInfoById(id);
InventoryItem item = this.GetComponentInChildren&InventoryItem&();
this.num =
numLabel.text = this.num.ToString();
numLabel.enabled = true;
item.SetId(id);
public void PlusNum(int num = 1)
this.num +=
numLabel.text = this.num.ToString();
public void ClearInfo()
info = null;
numLabel.enabled = false;
&  5、背包物品的拾取功能
  &由于我们txt文件中只存储了3种物品,这里我们使用随机数模拟一下拾取功能,每次按下x键随机一个数,并根据id取得该物品其他信息,实例化该物体并调整坐标及parent
1 void Update () {
if (Input.GetKeyDown(KeyCode.X))
GetSomething(Random.Range(1001, 1004));
private void GetSomething(int id)
InventoryItemGrid grid = null;
//检测grid中有没有当前物体
foreach (InventoryItemGrid temp in itemGridList)
if (temp.id == id)
if (grid != null)//有当前物体 num加1
grid.PlusNum(1);
else//没有当前物体 查找是否有空格 根据id是否为0判断
foreach (InventoryItemGrid temp in itemGridList)
if (temp.id == 0)
if (grid != null)//有空格
//在当前格实例化物体
GameObject go = NGUITools.AddChild(grid.gameObject, inventoryItemGameobject);
go.transform.localPosition = Vector3.
go.GetComponent&UISprite&().depth = 8;
grid.SetId(id);
else//没有空格
print(&背包满了&);
&  6、实现背包物品的拖拽功能
  物品的拖拽有几种情况需要分别实现:物品拖到一个空格,物品拖到有物品的格子、两物品应交换位置信息,物品拖到背包界面外等应还在当前格子
  拖拽功能的实现应在protected override void OnDragDropRelease(GameObject surface) 这个函数中实现,判断拖放结束时停留的物体为surface,要区分surface的类别,应根据tag来实现,因此我们给所有的格子添加Tag InventoryItemGrid,给物品添加Tag InventoryItem,为了方便tag管理我们添加一个Tags脚本,其中存储各种Tags信息  
1 using UnityE
2 using System.C
4 public class Tags : MonoBehaviour {
public const string GROUND = &Ground&;
public const string PLAYER = &Player&;
public const string INVENTORY_ITEM = &InventoryItem&;
public const string INVENTORY_ITEM_GRID = &InventoryItemGrid&;
&物品拖拽功能的实现代码如下
1 protected override void OnDragDropRelease(GameObject surface)
base.OnDragDropRelease(surface);
if (surface != null)
//拖放到一个有物体的格子
if (surface.tag == Tags.INVENTORY_ITEM)
InventoryItemGrid oldGrid = this.transform.parent.GetComponent&InventoryItemGrid&();
int id = oldGrid.
int num = oldGrid.
InventoryItemGrid newGrid = surface.transform.parent.GetComponent&InventoryItemGrid&();
//交换数据
oldGrid.SetId(newGrid.id, newGrid.num);
newGrid.SetId(id, num);
ResetPosition();
//拖放到一个空格子
else if (surface.tag == Tags.INVENTORY_ITEM_GRID)
//拖放到自己的格子
if (surface.transform.parent == this.transform.parent)
ResetPosition();
else//其他空格子
InventoryItemGrid oldGrid = this.transform.parent.GetComponent&InventoryItemGrid&();
InventoryItemGrid nowGrid = surface.GetComponent&InventoryItemGrid&();
this.transform.parent = surface.
ResetPosition();
nowGrid.SetId(oldGrid.id, oldGrid.num);
oldGrid.ClearInfo();
ResetPosition();
ResetPosition();
private void ResetPosition()
transform.localPosition = Vector3.
&  7、背包物品的信息提示
  &在unity背包下面添加一个sprite作为提示信息的背景,背景下面添加一个label显示提示信息。
  我们应在鼠标放在物品上时显示该物品的详细信息,鼠标移出时提示信息框则应消失。要实现这种效果,我们可以在物品prefab上添加一个NGUI提供的UI Event Trigger组件,On Hover Over和On Hover Out分别绑定InventoryItem中的两个函数
public void OnHoverOver()
InventoryDes._instance.Show(id,this.transform.position);
public void OnHoverOut()
InventoryDes._instance.Hide();
  在界面上提示信息的sprite上面挂一个脚本命名为InventoryDes,该脚本控制提示信息的显示隐藏等功能
1 using UnityE
2 using System.C
4 public class InventoryDes : MonoBehaviour {
public static InventoryDes _
private UIL
void Awake()
_instance = this;
label = this.GetComponentInChildren&UILabel&();
this.gameObject.SetActive(false);
public void Show(int id,Vector3 pos)
this.gameObject.SetActive(true);
this.transform.position =
ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);
string des = &&;
switch (info.type)
case ObjectType.Drug:
des = GetDrugDes(info);
case ObjectType.Equip:
case ObjectType.Mat:
label.text =
public void Hide()
this.gameObject.SetActive(false);
private string GetDrugDes(ObjectInfo info)
string s = &&;
s += &名称:& + info.name + &\n&;
s += &回复Hp:& + info.hp + &\n&;
s += &回复Mp:& + info.mp + &\n&;
s += &出售价:& + info.priceSell + &\n&;
s += &购买价:& + info.priceBuy + &\n&;
&  8、背包物品的整理功能
  &当我们背包中的物品排列很散乱是,点击整理按钮,所有的物品应有序的从前到后排列整齐,中间应该没有空格,这个功能该如何实现呢?这里提供一种方法,可能效率不是最高的(没有想到更好的实现方法,有更好方法的朋友请告诉我一下,谢谢),但可以实现基本要求:
  例如上面的图,有红圈的地方代表有物体,其余为空格,我们怎样将物品排列好使之没有空格呢?我们可以先将所有格子遍历一变,记下空格的索引;然后从最小的索引
开始向后循环,将所有格子从后向前遍历,如果格子中有物体则将其移动到该空格索引对应的格子中,然后继续...  
  下面是代码:
1 //整理背包物品
public void OnArrangeInventory()
List&int& nullGridIndexList = new List&int&();
for (int i = 0; i & itemGridList.C i++)
if (itemGridList[i].id == 0)
nullGridIndexList.Add(i);
//背包满了不整理
if (nullGridIndexList.Count != 0)
foreach (int index in nullGridIndexList)
for (int i = itemGridList.Count - 1; i & i--)
if (itemGridList[i].id != 0)
if (i & index)
ExchangeItemToANullGrid(index, i);
//index为空格子索引, i为有物品的格子
private void ExchangeItemToANullGrid(int index, int i)
Transform transform = itemGridList[i].GetComponentInChildren&InventoryItem&().gameObject.
transform.parent
= itemGridList[index].
transform.localPosition = Vector3.
itemGridList[index].SetId(itemGridList[i].id, itemGridList[i].num);
itemGridList[i].ClearInfo();
&  至此,一个简单但功能齐全的背包系统便做好了!&
本文已收录于以下专栏:
相关文章推荐
三十 背包系统的制作
1 创建一个Sprite作为背景图片
2 创建一个Sprite作为格子并保存为Prefab
3 在格子下创建Sprite作为物品
4 在物品下创建Label用来显示物品个数
NGUI做背包 NGUI 做游戏商店
RPG黑暗之光(5)NGUI实现背包系统初步
1、物品信息的解析
我们用ObjectsInfoList.txt来存储物品信息:
1001,小瓶血药,icon-potion1,Dr...
整理背包有两种思路
1.替换父类子类物体
2.改变物体的相对父类的坐标
这两者的区别在于前者需要找到父类或者子类物体
后者则相对简单,只用知道父类格子的坐标,然后替换就行了
查找算法,类似与C...
欢迎来到我们的狗刨网,我们今天主要讲的是背包系统和血条的显示,这个东西很好玩,下面我们就讲解一下是如何实现的吧。
首先来看看效果吧:
一、背包系统
前话在许多类型游戏中我们经常会使用到背包,利用背包来设置相应角色属性,多了背包也会让游戏增色拓展不少。
那在Unity3D游戏开发中该如何编写背包系统呢?因为有高人开发了NGUI插件,因此我们进行简...
using UnityE
using System.C
public class DragAndDrop : MonoBehaviour {
public ...
他的最新文章
讲师:吴岸城
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)使用NGUI实现带有放大功能的scrollview背包 - CSDN博客
使用NGUI实现带有放大功能的scrollview背包
效果如图:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
在gameobject上面加上table组件
在给gameobjct添加一个脚本,在脚本中创建item ,我这里创建了10个item,然后给它们都添加上boxcollider,rigidbody,因为后面在item缩放时候会用到。
刚接触ngui,让中间的item变大,我得得到这个item的通知,他怎么给我通知呢,我们这里在中间创建一个point,在其中创建一个脚本,来监听这个触发。
在这里面处理item的放大
这里我做的时候遇到一个坑点,也是基础不好的原因:
被检测item必须是要给上rigidbody的不然point检测不到!
但是这种做法是我自己写的。不知道ngui的这种制作有没有更好的办法。希望看到的网友可以传授一下。
本文已收录于以下专栏:
相关文章推荐
写在前面(废话):
想用NGUI实现个多层血条,在网上没找到相关信息,思来想去还是自己实现一个吧。最后捣鼓捣鼓还是出来了。写了两个版本的,第一个版本是纯体力制作,就是有几层血就预先放好几个血条,比如...
using UnityE
using System.C
/// 脚本位置:预制体身上
public class RoleItem : MonoBe...
NGUI的Scrollview当要重置的时候那几个reposition阿,ResetPosition,repositionNow什么的非常烦。
现在稍稍总结一下
首先。分清两个概念。一个是UIScro...
上篇笔记写到,NGUI自定义的基础窗体实际上只有那么有限的几种,那NGUI是怎么实现强大的功能呢,答案就是通过脚本。通过挂接功能脚本,所对应的窗体立即获得响应的功能,相当的方便。那么NGUI怎么挂接脚...
在使用NGUI的Scroll View的时候,碰到Scroll Item太多的话(超过100个),在滑动的过程中就会明显掉帧。究其原因就是NGUI每帧会对所有的Scroll Item进行更新,无论它是...
下面学些下NGUI的TweenPosition位移动画,下面介绍两种游戏中常用的用法:
用法1.NGUI的控件从PosA位置移动到PosB位置,播放动画
用法2.在游戏中需要动态创建带有TweenPo...
//NewBehaviourScript.cs
// Created by [JiangXinhou]
// Copyright
最近在处理项目时,遇到一个棘手的问题,搞了好久终于把他弄出来了。项目要求实现一个功能,就是当在ScrollView滚动到终点的时候,控制ScrollView回弹到初始的位置,比如列表有14条数据,当滑...
NGUI在Unity3D游戏开发中非常常用,而NGUI对于每一个UI场景,都是以一个UIRoot为UI游戏对象树的根的,那么这个UIRoot是起什么作用的呢?
先简单看一下UIRoot中的基本属性
using UnityE
using System.C
public class ThisRotate : MonoBehaviour
Vector3 ...
他的最新文章
讲师:吴岸城
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)3524人阅读
UGUI(14)
本实例通过UGUI实现背包的简单功能,物品的拖拽,交换,拖放位置处理,还有针对背包多页面的滑动翻页的处理。
具体的代码如下。
首先是UIItem类,这个背包内各种物体的类:
using UnityE
using System.C
using UnityEngine.UI;
public class UIItem : MonoBehaviour {
public void Show(string name)
this.name.text =
然后是拖拽事件类,出来鼠标拖拽UIItem类:
using UnityE
using System.C
using System.Collections.G
using UnityEngine.EventS
using UnityEngine.UI;
public class DragEvent : MonoBehaviour,IBeginDragHandler ,IDragHandler ,IEndDragHandler ,IPointerDownHandler ,IPointerUpHandler {
public RectTransform rectT
public RectTransform rectTransformS
public Transform draggedItemB
private Vector2 pointerO
public CanvasGroup canvasG
/// &summary&
/// 开始拖拽!
/// &/summary&
/// &param name=&eventData&&&/param&
public void OnBeginDrag(PointerEventData eventData)
SetDraggedPosition(eventData);
/// &summary&
/// 设置拖拽开始和结束的位置
/// &/summary&
/// &param name=&eventData&&&/param&
private void SetDraggedPosition(PointerEventData eventData)
if (rectTransform == null)
if(eventData .button ==PointerEventData .InputButton .Left )
//把世界坐标转换为局部坐标
rectTransform.SetAsLastSibling();
transform.SetParent(draggedItemBox);
Vector2 localPointerP
//画布是否接受射线
canvasGroup.blocksRaycasts =
if(RectTransformUtility .ScreenPointToLocalPointInRectangle (rectTransformSlot ,Input .mousePosition ,
eventData .pressEventCamera ,out localPointerPosition ))
rectTransform.localPosition = localPointerPosition - pointerO
public void OnDrag(PointerEventData eventData)
SetDraggedPosition(eventData);
public void OnEndDrag(PointerEventData eventData)
SetDraggedPosition(eventData);
canvasGroup.blocksRaycasts =
if(eventData .pointerCurrentRaycast .gameObject .name ==&skill&)
transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.
else if(eventData .pointerCurrentRaycast .gameObject .name ==&Text99&)
this.transform.SetParent(eventData.pointerCurrentRaycast.gameObject.transform.parent.parent);
transform.position = eventData.pointerCurrentRaycast.gameObject.transform.parent.parent.
eventData.pointerCurrentRaycast.gameObject.transform.parent.SetParent(grid);
transform.SetParent(grid);
public void OnPointerDown(PointerEventData eventData)
transform.localPosition = Vector3.one * 0.5f;
public void OnPointerUp(PointerEventData eventData)
transform.localPosition = Vector3.
最后的对画布和Scroll的类处理:
using UnityE
using System.C
using UnityEngine.UI;
using UnityEngine.EventS
using System.Collections.G
public class ScrollRectHelper : MonoBehaviour ,IBeginDragHandler ,IEndDragHandler {
/// &summary&
/// 滑动速度
/// &/summary&
private float smooting = 2;
/// &summary&
/// 每页显示的数目
/// &/summary&
private int pageCount = 16;
private ScrollRect sR
/// &summary&
/// 总页数
/// &/summary&
private float pageI
/// &summary&
/// 是否拖拽结束
/// &/summary&
private bool isDrag =
/// &summary&
/// 总页数索引比例
/// &/summary&
private List&float& listPageValue = new List&float& { 0 };
/// &summary&
/// 滑动目标位置
/// &/summary&
float targetPos = 0;
/// &summary&
/// 当前索引位置
/// &/summary&
float nowIndex = 0;
public GameObject UII
void Awake()
sRect = GetComponent&ScrollRect&();
ListPageValueInit();
/// &summary&
/// 初始化UIItem
/// &/summary&
void Start()
for (int i = 0; i & i++)
GameObject go = Instantiate(UIItem);
go.name += +i;
go.SetActive(true);
go.transform.localPosition = UIItem.transform.localP
go.transform.parent = UIItem.transform.
go.GetComponent&UIItem&().Show(&物品& + i);
/// &summary&
/// 每页比例
/// &/summary&
private void ListPageValueInit()
pageIndex = (count / pageCount) - 1;
if(count !=0)
for (int i = 1; i & pageI i++)
listPageValue.Add((i / pageIndex));
void Update()
if(!isDrag )
sRect.horizontalNormalizedPosition = Mathf.Lerp(sRect.horizontalNormalizedPosition, targetPos, Time.deltaTime);
/// &summary&
/// 开始拖拽
/// &/summary&
/// &param name=&eventData&&&/param&
public void OnBeginDrag(PointerEventData eventData)
/// &summary&
/// 拖拽结束
/// &/summary&
/// &param name=&eventData&&&/param&
public void OnEndDrag(PointerEventData eventData)
//获取拖动的值
tempPos = sRect.horizontalNormalizedP
var index = 0;
//拖动的绝对值
float offset = Mathf.Abs(listPageValue[index] - tempPos);
for (int i = 0; i & listPageValue .C i++)
float temp = Mathf.Abs(tempPos - listPageValue[i]);
if(temp &offset )
targetPos = listPageValue[index];
nowIndex =
#region 如果需要分页,此处为左右分页代码,现有功能不需要,所以注释掉了
//public void BtnLeftGo()
nowindex = Mathf.Clamp(nowindex - 1, 0, pageIndex);
targetPos = listPageValue[Convert.ToInt32(nowindex)];
//public void BtnRightGo()
nowindex = Mathf.Clamp(nowindex + 1, 0, pageIndex);
targetPos = listPageValue[Convert.ToInt32(nowindex)];
#endregion
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:126308次
积分:2602
积分:2602
排名:第15013名
原创:108篇
转载:75篇
译文:14篇
(1)(5)(5)(15)(9)(14)(8)(7)(19)(26)(5)(42)(6)(6)(2)(22)(1)(4)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'

我要回帖

更多关于 ngui scrollview 拖动 的文章

 

随机推荐