Unity3D UGUI学习小结

今天看了下UGUI的教程,实在太棒了。不过学了个囫囵吞枣。事件处理源码啥的都没心思看下去。具体总结下今天学的:

1.锚点和中心点问题:简单点说就是,锚点四个花瓣在一个点时,中心点的坐标;不在一个点时,中心点和花瓣所组成边框的距离

2.点击事件处理,button可以在OnClick,添加方法。Image等组件可以在EventTrigger设置。也可通过编写类实现以下接口实现。

using UnityEngine.EventSystem

IPointEnterHandler,IPointExitHandler,IPointDownHandler,IPointUpHandler,IPointClickHandler,IDragHandler

IDropHandler,IScrollHandler,IUpdateSelectedHandler,ISelectHandler,IDeselectHandler,IMoveHandler

简单的写了一个鼠标拖动图片的小脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class drag:BeHaviour,IDragHandler,IDropHandler{
	private RectTransform rt;
 	public RectTransform canvas;
	Vector2 firstpos=new Vector2();
 	private bool isfirst=true;
 	void Start()
 	{
 		rt=transform as RectTransform;
 	}
 	public void OnDrag(PointsDates eventData)
	{
 		Vector2 mousepos = eventData.position;
  		Vector2 uguipos = new Vector2();
  		bool isrect = RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas,mousepos,eventData.enterEventCamera,out uguipos);
  		if(isfirst)
  		{
    			isfirst=false;
    			RectTransformUtility.ScreenPointToLocalPointRectangle(rt,mousepos,eventData.enterEventCamera,out firstpos);
  		}
		if (isrect)
        	{
           		 //rt.pivot = uguipos;
            		rt.anchoredPosition = uguipos-firstpos;
        	}
	}
	void Update () {
  
 	}
    	public void OnDrop(PointerEventData eventData)
   	{
        	isfirst = true; 
    	}
}

3.点击按钮显示背包等界面

public void onBtClick()
{
    RectTransform package = Instantiate(packageclone);//生成界面
    package.SetParent(this.transform.parent);//改变parent
    package.position = new Vector3(0, 0, 0);
    package.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
    package.localScale = new Vector3(1, 1, 1);
    package.anchoredPosition = new Vector2(0, 0);//初始化大小、位置、旋转、在画布的位置等。
}

4、最难的拖拽功能,看的迷迷糊糊的,没练习。

抓取物体

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;   
public class dragItem : MonoBehaviour,IDragHandler,IBeginDragHandler,IEndDragHandler {

    // Use this for initialization
    public dragPanelitem dragItempanel;
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
    public void OnDrag(PointerEventData eventData)
    {
        if (dragItempanel != null)
        {
            dragItempanel.move(eventData);//移动物体
        }
    }
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (dragItempanel != null)
        {
            dragItempanel.hideItem();//将Image隐藏
        }
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        if (dragItempanel != null)
        {
            dragItempanel.setDragItemID(1);//获取抓取的物体
        }
    }
}

单独定义一个panel,用于存储抓取的物体。并对其操作


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


  
  
public class dragPanelitem : MonoBehaviour {
    private int DragitemID = 0;     public Image dragimage; // Use this for initialization   void Start () {      }   // Update is called once per frame   void Update () {     }     public void move(PointerEventData eventData)     {         dragimage.rectTransform.position = eventData.position;//因为父容器铺满四周,且该image锚点在父容器中心点位置。可以不用转换坐标     }     public void setDragItemID(int itemID)     {         DragitemID = itemID;         dragimage.gameObject.SetActive(true);//获取抓取的物体实际上少了一步,给dragimage赋值的步骤     }     public void hideItem()     {         DragitemID = 0;         dragimage.gameObject.SetActive(false);     } }


好好学习,天天向上。有志同道合的朋友可以关注下。共同进步

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值