欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UGUI背包拖拽

程序员文章站 2022-05-30 17:37:59
...
public class ItemDrag : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerDownHandler,IPointerUpHandler,IDragHandler 
{	
	//鼠标起点
	private Vector2 originalLocalPointerPosition;
	//面板起点
	private Vector3 originalLPaneLocalPosition;
	//当前面板
	private RectTransform panelRectTransform;
	//父节点,这个最好是ui父节点,因为它的矩形大小刚好是屏幕大小
	public RectTransform parentRectTransform;
	//格子列表
	public bool isDoubleCick;
	public float AvaiClickTime=0.3f;
	public float timer;

	float t1;  
	float t2;  
	//public GameObject desText;
	private GameObject gridManager;//在gridlist之上 添加一个新的空物体作为父节点
	private GameObject originalGrid; //记录物品拖动前的位置
	private static int siblingIndex = 0;

	public GameObject LightItem;


	void Awake()
	{
		panelRectTransform = transform as RectTransform;
		parentRectTransform = GameObject.FindGameObjectWithTag ("ScrollRect").GetComponent<RectTransform> () as RectTransform;
		gridManager = GameObject.Find ("GridManager");
	}
	// Use this for initialization
	//鼠标移入

	public void OnPointerEnter(PointerEventData evendata)
	{
		//GameObject TextDesc = transform.parent.parent.GetComponent<GridManger> ().TextDesc;
		//TextDesc.SetActive (true);

		//Vector2 _pos = Vector2.one;

		//RectTransformUtility.ScreenPointToLocalPointInRectangle(TextDesc.transform as RectTransform,evendata.position,null,out _pos);
		//TextDesc.transform.localPosition= evendata.pointerEnter.transform.parent.transform.position;
		Debug.Log (evendata.pointerEnter.name);

	}
	public void OnPointerExit(PointerEventData evendata)
	{
		//transform.parent.parent.GetComponent<GridManger> ().TextDesc.SetActive (false);
		//desText.gameObject.SetActive (false);
		
	}	
	//
	//鼠标按下
	public void OnPointerDown(PointerEventData data)
	{
		//记录物品当前所在的格子信息
		originalGrid=panelRectTransform.parent.gameObject;
		//将物品放置在gridManager下,并设置层级,保证物品显示在整个背包层面之上
		panelRectTransform.SetParent(gridManager.transform);
		siblingIndex++;//层级管理
		panelRectTransform.transform.SetSiblingIndex(siblingIndex);
		//记录当前面板起点
		originalLPaneLocalPosition = panelRectTransform.localPosition;
		//通过屏幕中的鼠标点,获取父节点中的鼠标点
		//parentRectTransform:父节点
		//data.position :当前鼠标位置
		//data.pressEventCamera:当前事件的摄像机
		//originalLocalPointerPosition:获取当前鼠起点
		RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform,data.position,data.pressEventCamera,out originalLocalPointerPosition);

		
	}



	public void OnPointerUp(PointerEventData data)
	{	//transform.SetParent(gridlist.transform);
		RaycastHit2D hit = Physics2D.Raycast(Input.mousePosition,-Vector2.up);
		if (hit.collider != null) {//如果射线检测到的gameobject为grid ,就把当前物品放在grid节点下
			if (hit.collider.gameObject.CompareTag ("Grid") && hit.collider.gameObject.transform.Find (transform.name + "(Clone)") == null) {
				transform.parent = hit.transform;
			} else {	
				//如果不是格子或没有检测到物体,则将物品放回原来的格子内
				Destroy(gameObject);
			}
		} else 
		{	
			Destroy(gameObject);
		}
		//重置物品位置
		t2 = Time.realtimeSinceStartup; 
		transform.localPosition=Vector3.zero;
		if (t2 - t1 < 0.2)  
		{  
			Debug.Log("双击");  
			isDoubleCick = !isDoubleCick;

		}  
		t1 = t2; 

			
		
	}
	public void UseLight ()
	{
		
	}

	public void OnDrag(PointerEventData data)
	{
		if (panelRectTransform == null || parentRectTransform == null) 
		{
			return;
		}
		Vector2 localPointerPosition;
		//获取本地鼠标位置
		if(RectTransformUtility.ScreenPointToLocalPointInRectangle(parentRectTransform,data.position,data.pressEventCamera,out localPointerPosition))
		{
			//移动位置 =鼠标当前位置- 本地鼠标 起点位置
			Vector3 offsetToOriginal =localPointerPosition - originalLocalPointerPosition;
			//当前物品位置 = 物品起点+移动位置
			panelRectTransform.localPosition = originalLPaneLocalPosition + offsetToOriginal;
		}
		//ClampToWindow();
		
	}
	void Start () 
	{
		
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (isDoubleCick) 
		{	
			GameObject player = GameObject.FindGameObjectWithTag ("Player");
			GameObject Temp=Instantiate(LightItem,player.transform.position,player.transform.rotation);	//生产 ,再从背包摧毁
			gridManager.transform.GetComponentInChildren<GridManger>().grid.RemoveAt(0);
			isDoubleCick=false;
		}
	}

}

IPointerEnterHandler,IPointerExitHandler,IPointerDownHandler,IPointerUpHandler,IDragHandler

这几个接口, 都要一一对应,实现,不然会报错。

public void OnPointerEnter(PointerEventData evendata){}

public void OnPointerExit(PointerEventData evendata){}

public void OnPointerDown(PointerEventData data){}

 public void OnPointerUp(PointerEventData data){}

public void OnDrag(PointerEventData data){}

官方API

而在NGUI中,已经封装好了 UIDrag Object

 UGUI背包拖拽

UGUI背包拖拽

Target是拖拽的目标,

Movement是拖拽时,target的速度, Scoll Wheel 就是滚动的方向,

Drag Effect 是 拖动的特效,Momentum是冲力的意思,就是 拖动 松开后给一个冲力, 这个力由上一拖动的快慢决定,

其实也是上一贞的鼠标转换屏幕位置,和松手时的鼠标转换屏幕位置的值,

NGUI Drag的 源码如下

先是判断状态.再从发射 射线探测,获取当前位置,和偏移亮,再根据选择Drag Effect的不同 ,进行计算,三种特效可选择,None,Monmentum(移动后 的缓慢插值 根据偏移量offset去计算) 

UGUI背包拖拽

	void OnDrag (Vector2 delta)
	{
		if (mPressed && mTouchID == UICamera.currentTouchID && enabled && NGUITools.GetActive(gameObject) && target != null)
		{
			UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta;

			Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.currentTouch.pos);
			float dist = 0f;

			if (mPlane.Raycast(ray, out dist))
			{
				Vector3 currentPos = ray.GetPoint(dist);
				Vector3 offset = currentPos - mLastPos;
				mLastPos = currentPos;

				if (!mStarted)
				{
					mStarted = true;
					offset = Vector3.zero;
				}

				if (offset.x != 0f || offset.y != 0f)
				{
					offset = target.InverseTransformDirection(offset);
					offset.Scale(scale);
					offset = target.TransformDirection(offset);
				}

				// Adjust the momentum
				if (dragEffect != DragEffect.None)
					mMomentum = Vector3.Lerp(mMomentum, mMomentum + offset * (0.01f * momentumAmount), 0.67f);

				// Adjust the position and bounds
				Vector3 before = target.localPosition;
				Move(offset);

				// We want to constrain the UI to be within bounds
				if (restrictWithinPanel)
				{
					mBounds.center = mBounds.center + (target.localPosition - before);

					// Constrain the UI to the bounds, and if done so, immediately eliminate the momentum
					if (dragEffect != DragEffect.MomentumAndSpring && panelRegion.ConstrainTargetToBounds(target, ref mBounds, true))
						CancelMovement();
				}
			}
		}
	}

 

相关标签: UGUI