uGUI練習(四) Draggable Object

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   2014   

一、步驟

監聽UI的Drag事件,需要我們寫一點點的代碼。

1、建立一個Panel ,設定size為(100,100)

2、建立DraggableObjectScene.cs指令碼

3、把指令碼綁定在Panel上

4、指令碼內容如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;using UnityEngine.EventSystems;public class DraggableObjectScene : MonoBehaviour,IDragHandler,IPointerDownHandler,IPointerUpHandler{    //Drag事件,設定目標的位置為滑鼠的位置    public void OnDrag(PointerEventData eventData)    {        GetComponent<RectTransform>().pivot.Set(0,0);        transform.position=Input.mousePosition;    }        public void OnPointerDown(PointerEventData eventData)    {        //縮小        transform.localScale=new Vector3(0.7f,0.7f,0.7f);    }    public void OnPointerUp(PointerEventData eventData)    {        //正常大小        transform.localScale=new Vector3(1f,1f,1f);    }}

Class DraggableObjectScene 繼承了IDragHandler,IPointerDownHandler,IPointerUpHandler三個介面,從而可以接受到 OnDrag(PointerEventData eventData)  ,OnPointerDown(PointerEventData eventData)   ,OnPointerUp(PointerEventData eventData)  三個事件

二、事件系統

Namespace:UnityEngine.EventSystems

IDragHandler (MouseDrag)

Interface to implement if you wish to receive OnDrag callbacks.

IPointerDownHandler (MouseDown)

Interface to implement if you wish to receive OnPointerDown callbacks.

IPointerUpHandler (MouseUp)

Interface to implement if you wish to receive OnPointerUp callbacks.

三、Drag 效果

uGUI練習(四) Draggable Object

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.