Ugui exercise (4) draggable object

Source: Internet
Author: User
1. Steps

To listen to the drag event of the UI, We need to write a little bit of code.

1. Create a panel and set the size to 100,100)

2. Create the draggableobjectscene. CS script.

3. Bind the script to the Panel.

4. The script content is as follows:

Using unityengine; using system. collections; using unityengine. ui; using unityengine. eventsystems; public class draggableobjectscene: monobehaviour, idraghandler, ipointerdownhandler, ipointeruphandler {// drag event, set the target location to public void ondrag (pointereventdata eventdata) {getcomponent <recttransform> (). invalid. set (0, 0); transform. position = input. mouseposition;} public void onpointerdown (pointereventdata eventdata) {// narrow down transform. localscale = new vector3 (0.7f, 0.7f, 0.7f);} public void onpointerup (pointereventdata eventdata) {// normal transform size. localscale = new vector3 (1f, 1f, 1f );}}

Class draggableobjectscene inherits the three interfaces: idraghandler, ipointerdownhandler, and ipointeruphandler, which can be acceptedOndrag (pointereventdata eventdata),Onpointerdown (pointereventdata eventdata),Onpointerup (pointereventdata eventdata)Three events

Ii. Event System

Namespace: unityengine. eventsystems

Idraghandler (mousedrag)

Interface to implement if you wish to receiveOndragCallbacks.

Ipointerdownhandler (mousedown)

Interface to implement if you wish to receiveOnpointerdownCallbacks.

Ipointeruphandler (mouseup)

Interface to implement if you wish to receiveOnpointerupCallbacks.

Iii. Drag Effect

Ugui exercise (4) draggable object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.