UICenterOnChild
2017-04-18 21:03
337 查看
类似 NGUI 里面的那种效果, 挂在ScrollRect 上就可以了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
[RequireComponent(typeof(ScrollRect))]
public class UICenterOnChild : MonoBehaviour, IBeginDragHandler, IEndDragHandler {
// Use this for initialization
public ScrollRect scrollRect;
public bool isDraging;
public List<float> pageArray = new List<float>();
public int pageIndex;
public float curretPosition;
void Start ()
{
scrollRect = GetComponent<ScrollRect>();
float each = 1f / (scrollRect.content.childCount - 1);
for (int i = 0; i < scrollRect.content.childCount; i++)
{
pageArray.Add(each * i);
}
}
float currentVelocity;
// Update is called once per frame
void Update ()
{
if (!isDraging)
{
scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, pageArray[pageIndex], Time.deltaTime *8);
}
curretPosition = scrollRect.horizontalNormalizedPosition;
}
public void OnBeginDrag(PointerEventData eventData)
{
isDraging = true;
}
public void OnEndDrag(PointerEventData eventData)
{
isDraging = false;
float posX = scrollRect.horizontalNormalizedPosition;
pageIndex = 0;
float offSet = Mathf.Abs(pageArray[pageIndex] - posX);
for (int i = 1; i < pageArray.Count; i++ )
{
float offsetTemp = Mathf.Abs(pageArray[i] - posX);
if (offsetTemp < offSet )
{
pageIndex = i;
offSet = offsetTemp;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System;
[RequireComponent(typeof(ScrollRect))]
public class UICenterOnChild : MonoBehaviour, IBeginDragHandler, IEndDragHandler {
// Use this for initialization
public ScrollRect scrollRect;
public bool isDraging;
public List<float> pageArray = new List<float>();
public int pageIndex;
public float curretPosition;
void Start ()
{
scrollRect = GetComponent<ScrollRect>();
float each = 1f / (scrollRect.content.childCount - 1);
for (int i = 0; i < scrollRect.content.childCount; i++)
{
pageArray.Add(each * i);
}
}
float currentVelocity;
// Update is called once per frame
void Update ()
{
if (!isDraging)
{
scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, pageArray[pageIndex], Time.deltaTime *8);
}
curretPosition = scrollRect.horizontalNormalizedPosition;
}
public void OnBeginDrag(PointerEventData eventData)
{
isDraging = true;
}
public void OnEndDrag(PointerEventData eventData)
{
isDraging = false;
float posX = scrollRect.horizontalNormalizedPosition;
pageIndex = 0;
float offSet = Mathf.Abs(pageArray[pageIndex] - posX);
for (int i = 1; i < pageArray.Count; i++ )
{
float offsetTemp = Mathf.Abs(pageArray[i] - posX);
if (offsetTemp < offSet )
{
pageIndex = i;
offSet = offsetTemp;
}
}
}
}
相关文章推荐
- Unity-NGUI组件UICenterOnChild使对象在UIDarggablePanel居中显示
- NGUI UIcenterOnChild
- Unity-NGUI组件UICenterOnChild使对象在UIDarggablePanel居中显示
- unity开发 --------- NGUI (UIDragPanelContents、UIDraggablePanel、UICenterOnChild、UIScollBar、SpringPanel)
- NGUI UICenter On Child 在3.69版本中 不能居中!
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- 当主页面关闭时关闭所有子窗口(Close all the child windows on unload of main page)
- requestLayout()与onLayout();onDraw()与drawChild()的区别和联系
- Android完美解决The specified child already has a parent. You must call removeView() on the child
- . 报错 specified child already has a parent. You must call removeView() on the child's parent first
- 关于向父view添加相同的view问题!The specified child already has a parent. You must call removeView() on the chil
- 【Android异常】The specified child already has a parent. You must call removeView() on the child's paren
- #479 – 对Canvas面板中的子元素使用布局转换(Using a Layout Transform on Child Elements in a Canvas)
- android The specified child already has a parent. You must call removeView() on the child's parent f
- Android PopupWindow You must call removeView() on the child's parent first.
- The specified child already has a parent. You must call removeView() on the chil
- The specified child already has a parent. You must call removeView() on the child's parent first.
- 关于ExpandableListView.setOnChildClickListener获取不到点击事件
- The specified child already has a parent. You must call removeView() on the child's parent first
- The specified child already has a parent. You must call removeView() on the child's parent first