ngui一个滑动

最近由于工作需要学习UNITY3D,对于做PHP的程序猿挑战性灰常大,unity3D国内相关资料少得可怜唉!

根据需求做个防“天天爱消除”主界面左右滑动窗体的效果,百度搜到雨凇大神的一个帖子

NGUI研究院之自制 Scroll View实现触摸滚动相册效果(四)

不过效果不怎么理想,没有平滑的spring动画。研究NGUI自带的Example 7 - Scroll View (Panel) 例子

实现了如下效果:


UIDragSlider.cs: 该脚本扩展了UICenterOnChild.CS的功能!用于灰白点队列的初始化工作以及根据最近中心点的窗体下标控制白点显示的位置。

UIDragSlider.cs


001 using UnityEngine;
002  
003 /// <summary>
004 /// Ever wanted to be able to auto-center on an object within a draggable panel?
005 /// Attach this script to the container that has the objects to center on as its children.
006 /// </summary>
007  
008 //[AddComponentMenu("NGUI/Interaction/Center On Child")]
009 public class UIDragSlider : MonoBehaviour
010 {
011     /// <summary>
012     /// The strength of the spring.
013     /// </summary>
014  
015     public float springStrength = 8f;
016  
017     /// <summary>
018     /// Callback to be triggered when the centering operation completes.
019     /// </summary>
020  
021     public SpringPanel.OnFinished onFinished;
022      
023     //用来放置灰色、白色小点
024     public Transform  ponit;
025     //白色的小点
026     public GameObject prefabMaskDot;
027     //灰色的小点
028     public GameObject prefabBaseDot;
029     //白色小点的临时对象
030     private GameObject maskDot;
031     //灰色、白色小点下方的起始位置。
032     int start;
033     UIDraggablePanel mDrag;
034     GameObject mCenteredObject;
035  
036     /// <summary>
037     /// Game object that the draggable panel is currently centered on.
038     /// </summary>
039  
040     public GameObject centeredObject { get return mCenteredObject; } }
041  
042     void OnEnable ()
043     {
044         Recenter ();
045         //initSlider ();
046     }
047  
048     void Start ()
049     {
050         initSlider ();
051     }
052  
053     void OnDragFinished ()
054     {
055         if (enabled)
056             Recenter ();
057     }
058  
059     /// <summary>
060     /// Recenter the draggable list on the center-most child.
061     /// </summary>
062  
063     public void Recenter ()
064     {
065      
066         if (mDrag == null) {
067             mDrag = NGUITools.FindInParents<UIDraggablePanel> (gameObject);
068  
069              
070             //mDrag = GameObject.Find("UIPanel (Clipped View)").GetComponent<UIDraggablePanel>();
071             if (mDrag == null) {
072                 Debug.LogWarning (GetType () + " requires " typeof(UIDraggablePanel) + " on a parent object in order to work"this);
073                 enabled = false;
074                 return;
075             else {
076                 //mDrag = mDrag.GetComponent<UIDraggablePanel>();
077                 mDrag.onDragFinished = OnDragFinished;
078                 //Debug.Log(mDrag.panel);
079                 if (mDrag.horizontalScrollBar != null)
080                     mDrag.horizontalScrollBar.onDragFinished = OnDragFinished;
081  
082                 if (mDrag.verticalScrollBar != null)
083                     mDrag.verticalScrollBar.onDragFinished = OnDragFinished;
084             }
085         }
086          
087         if (mDrag.panel == null)
088             return;
089          
090         // Calculate the panel's center in world coordinates
091         Vector4 clip = mDrag.panel.clipRange;
092         Transform dt = mDrag.panel.cachedTransform;
093         Vector3 center = dt.localPosition;
094         center.x += clip.x;
095         center.y += clip.y;
096         center = dt.parent.TransformPoint (center);
097  
098         // Offset this value by the momentum
099         Vector3 offsetCenter = center - mDrag.currentMomentum * (mDrag.momentumAmount * 0.1f);
100         mDrag.currentMomentum = Vector3.zero;
101  
102         float min = float.MaxValue;
103         Transform closest = null;
104         Transform trans = transform;
105  
106         // Determine the closest child
107         for (int i = 0, imax = trans.childCount; i < imax; ++i) {
108             Transform t = trans.GetChild (i);
109             float sqrDist = Vector3.SqrMagnitude (t.position - offsetCenter);
110  
111             if (sqrDist < min) {
112                 min = sqrDist;
113                 closest = t;
114             }
115         }
116  
117         if (closest != null) {
118             Debug.Log (closest.gameObject.name);
119             mCenteredObject = closest.gameObject;
120              
121             // Figure out the difference between the chosen child and the panel's center in local coordinates
122             Vector3 cp = dt.InverseTransformPoint (closest.position);
123             Vector3 cc = dt.InverseTransformPoint (center);
124             Vector3 offset = cp - cc;
125  
126             // Offset shouldn't occur if blocked by a zeroed-out scale
127             if (mDrag.scale.x == 0f)
128                 offset.x = 0f;
129             if (mDrag.scale.y == 0f)
130                 offset.y = 0f;
131             if (mDrag.scale.z == 0f)
132                 offset.z = 0f;
133  
134             // Spring the panel to this calculated position
135             SpringPanel.Begin (mDrag.gameObject, dt.localPosition - offset, springStrength).onFinished = onFinished;
136             //两个方法一个设置名字
137             //获取当前下标
138             /*
139             int index=0;
140             foreach(Transform child in transform){
141                 if(child.gameObject==closest.gameObject){
142                     Debug.Log("index:"+index);
143                     setMaskPos(index);
144                 }
145                 index++;
146             }
147             */
148             //第二个方法需要命名
149             string[] indexName = closest.gameObject.name.Split ('_');
150             setMaskPos (int.Parse (indexName [1]));
151              
152              
153         else
154             mCenteredObject = null;
155     }
156  
157     void initSlider ()
158     {
159         //因为下方灰色 白色的小点需要根据相册列表的数量来计算居中显示
160         int size = transform.childCount;
161         //乘以16表示计算所有小点加起来的宽度
162         int length = (size - 1) * 17;
163         //得到下方灰色 白色 小点的居中起始位置
164         start = (-length) >> 1;
165         for (int i=0; i< size; i++) {
166             //把每一个灰色小点加入3D世界
167             GameObject hui = (GameObject)Instantiate (prefabBaseDot);
168             //设置灰色小点的父类为另外一个面板
169             hui.transform.parent = ponit;
170             //设置每一个灰色小点的位置与缩放,总之让它们居中排列显示在相册列表下方。
171             hui.transform.localPosition = new Vector3 (start + i * 24, -240f, 0f);
172             hui.transform.localScale = new Vector3 (17, 17, 1);
173  
174             //深度 因为是先在屏幕下方绘制4个灰色的小点, 然后在灰色上面绘制白色小点
175             //表示当前的窗口ID 所以深度是为了设置白色小点在灰色小点之上绘制
176             hui.GetComponent<UISprite> ().depth = 0;
177         }
178         //把白色小点也加载在3D世界中
179         maskDot = (GameObject)Instantiate (prefabMaskDot);
180         //设置它的深度高于 灰色小点,让白色小点显示在灰色小点之上
181         maskDot.GetComponent<UISprite> ().depth = 1;
182         //设置白色小点的开始位置
183         setMaskPos (0);
184          
185     }
186  
187     void setMaskPos (int index)
188     {
189         maskDot.transform.parent = ponit;
190         maskDot.transform.localPosition = new Vector3 (start + index * 24, -240f, -10f);
191         maskDot.transform.localScale = new Vector3 (17, 17, 1);
192          
193     }
194 }
把脚本加到UIGrid上面

如有疑问请站内

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值