Unity3D UGUI学习小结
来源:互联网 发布:java encoder编码器 编辑:程序博客网 时间:2024/06/05 15:41
今天看了下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 framevoid 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 0
- Unity3D UGUI学习小结
- Unity3D UGUI学习笔记
- Unity3D之UGUI学习EventSystem
- 【UGUI】unity3D中ugui事件
- 【Unity3d】UGUI 之 Animator, Animation 动画系统学习笔记
- Unity3D 学习笔记4 —— UGUI+uLua游戏框架
- Unity3D学习日记(一)使用UGUI制作虚拟摇杆
- 【Unity3d】UGUI 之 Animator, Animation 动画系统学习笔记
- Unity3d学习笔记 UGUI锚点屏幕自适应问题
- Unity3d入门基础之UGUI-基本控件的学习
- (二十七)unity4.6学习Ugui中文文档-------Unity3D UI (uGUI)窗口扩展
- Unity3D NGUI,uGUI总结
- 【Unity3D】【UGUI】UI Overview
- 【Unity3D ugui】使用艺术字
- 【Unity3D ugui】使用艺术字
- Unity3D -- (UGUI)Scrollbar
- unity3D -- (UGUI)Scroll View
- Unity3D -- (UGUI)Canvas
- easyUI comboGrid设置Value和Text
- 概率密度函数 ××××× ××
- 对javabean的一些理解
- 联想小新V4000安装的win7系统主引导区被删除,利用U盘重装系统
- 【HDU1512】【左偏树】Monkey King 题解
- Unity3D UGUI学习小结
- Github收藏之Awesome C/C++
- JavaScript 30天每日效果图
- 智能建模之最小二乘辨识LS:Matlab实现
- 学习Python列表的访问,添加和删除
- 选择语句和循环语句练习
- DOM方法
- 十五天
- Android学习之简单使用SeekBar