Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by orlandobatista · May 25, 2015 at 09:42 PM · editorinspectoreventdrag-and-drop

How to make a field in inspector to pass events like "On Click()"?

Hi everyone!

I'm wondering how can I create a field to drag'n'drop functions in inspector like "On Click()" does. Anyone can help me with this?

alt text

Thanks!

example.png (2.4 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
13
Best Answer

Answer by DiegoSLTS · May 25, 2015 at 10:35 PM

Define a serializable class that extends UnityEvent and then add a member on your script of that type.

For example, an event that takes a string and a GameObject would look like this:

 using UnityEngine;
 using UnityEngine.Events;
 
 using System;
 using System.Collections;
 
 [Serializable]
 public class MyEvent : UnityEvent<string,GameObject> {}
 
 public class EventTest : MonoBehaviour {
 
     public MyEvent OnEvent;
 
 }

The name of the member will be the name of the event in the inspector. To call all the functions added to that event use the Invoke method.

For an event that doesn't take any parameter define a class like this:

 [Serializable]
 public class MyEvent : UnityEvent {}

Also, look here: http://docs.unity3d.com/460/Documentation/Manual/UnityEvents.html

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image orlandobatista · May 26, 2015 at 05:25 AM 0
Share

Works like a charm! Thanks a lot!

avatar image JuanDavid0705 · Feb 20, 2016 at 09:13 AM 0
Share

Thanks DiegoSLTS, this was my solution :)

avatar image ericmg123 · Apr 30, 2019 at 12:32 PM 0
Share

This was helpful, but a bit more clarification for those who come across this: I made SwipeEvents.cs, which looks like:

 using UnityEngine;
 using UnityEngine.Events;
 
 using System;
 using System.Collections;
 
 [Serializable]
 public class SwipeEvents : UnityEvent{ }
 
 public class EventTest : $$anonymous$$onoBehaviour
 {
 
     public SwipeEvents OnEvent;
 
 }

Then, on another script> "add a member on your script of that type": using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class SwipeListener : $$anonymous$$onoBehaviour {
 
     private Vector3 fp;   //First touch position
     private Vector3 lp;   //Last touch position
     public float dragDistance;  //$$anonymous$$imum distance for a swipe to be registered
     public SwipeEvents OnLeft;
     public SwipeEvents OnRight;
     public SwipeEvents OnTap;
 

And finally, here's the part that took me a little bit to get right:

                         if ((lp.x > fp.x))  //If the movement was to the right)
                         {   //Right swipe
                             Debug.Log("Right Swipe");
                             OnRight.Invoke();
 
                         }
 

Works great, thanks again for the help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there an event being fired off when the Inspector is being resized? 1 Answer

How to add UnityEvent using Custom Inspector? 1 Answer

Custom Inspector Invocation behaviour? 1 Answer

Left Click Up Event in Editor 4 Answers

Why cannot I drag and drop a gameObject from Hierarchy to a prefab's slot in the Inspector? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges