Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Socapex · Mar 13, 2016 at 04:46 PM · prefabeventeventsystemtargetevents

How to change target of UnityEvent

I am using UnityEvents to prepare scripts that will run later. My level designer sets a prefab in them and selects the correct method to run later. At runtime, I now need to accept arguments.

I used to simple get the name (string) of a method and Invoke that on the new object. Unfortunately I can't get the parameters this way. The real goal is to change the target, but there are no setters for UnityEvents. I could easily call the right thing using reflection, but there isn't a getter for parameters either.

Anyone has faced a similar problem with UnityEvents? Are there extra methods that I missed? Maybe some stuff in UnityInternal?

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

3 Replies

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

Answer by Socapex · Mar 13, 2016 at 09:31 PM

Use black magic...

Ok, jokes aside, you can retrieve the parameters but I wouldn't recommend it. I wont post my code here because it is ugly and dangerous and will get trashed by experienced users. I also wouldn't want anyone to think this is OK, it is evil.

I did the responsible thing and opened up a feature request. You can vote it up if you need this, but we all know these requests never get implemented so don't waste your time. https://feedback.unity3d.com/suggestions/unityevents-add-setter-for-m-target-and-slash-or-getter-for-m-arguments

Comment
Add comment · Show 1 · 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 CyRaid · Jan 03, 2017 at 10:51 PM 0
Share

$$anonymous$$an would a method to set target be useful..

avatar image
0

Answer by YJack · Mar 13, 2016 at 08:03 PM

The method you described, uses a Hashtable. You can implement a similar solution like that using the official tutorial here.

You can also see more about that on the official page here or on that awesome thread here.

About the arguments... you probably are looking for some custom UnityEvents like the ones below:

 using UnityEngine;
 using UnityEngine.Events;
 using System;
 
 [Serializable]
 public class UnityEventGameObject : UnityEvent<GameObject> { }
 
 [Serializable]
 public class UnityEventTransform : UnityEvent<Transform> { }
 
 [Serializable]
 public class UnityEventVector3 : UnityEvent<Vector3> { }
 
 [Serializable]
 public class UnityEventVector2 : UnityEvent<Vector2> { }
 
 [Serializable]
 public class UnityEventString : UnityEvent<string> { }
 
 [Serializable]
 public class UnityEventFloat : UnityEvent<float> { }
 
 [Serializable]
 public class UnityEventInt : UnityEvent<int> { }
 
 [Serializable]
 public class UnityEventBool : UnityEvent<bool> { }

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 Socapex · Mar 13, 2016 at 08:22 PM 0
Share

Hello, thanks for trying to help :) But, no and no ^_^ $$anonymous$$y question might not be easy to instantly grasp.

I am using UnityEvent PersistentEvents, which means none of it is created at runtime (or I would've used simple Actions or delegates). It also means you can't use RemoveListener or AddListener (http://docs.unity3d.com/ScriptReference/Events.UnityEvent.html). UnityActions are non-persistent listeners.

It is an interface for my level designer. He drags a prefab in a scriptable object, and when an editor script is run I execute his selected method to a newly created object. These objects are not available when setting the unityevent PersistentListeners.

The real solution would be to code my own UnityEvents and their respective EditorGUI, but this is a pet project I work on w-e for free, which means I don't have 2 weeks to spend on that.

I have a somewhat close to working solution, but the code is scary, black magic, wrong, dangerous, non-production-ready and will easily break on Unity updates. Because of that, I wont post it here, but what I want to do is definitely hackable :)

BTW, that Unity Event$$anonymous$$anager tutorial is garbage. You shouldn't use it. Just use normal C# delegates, and if you want a messaging system, register the listeners on a singleton with public Events(C#). Done, XD

Thank you again for helping! Really appreciated :)

avatar image YJack Socapex · Mar 14, 2016 at 03:19 AM 1
Share

Ok. Now I got it.

I really misunderstand your question.

Yes, the Event$$anonymous$$anager tutorial is really an introduction. I like to use something like the relay patterns from JoeStrout for instantiated stuff. For In-scene stuff, I like to use a more direct approach like the one you are using. That`s the best way I found so far in order to reduce dependence injection.

I'm not sure if there`s an easy way to set UnityEvent at editor time by code. Eventually, I will test that this week and comment here but I don`t recall of any previous experience on that.

Please, let us know if you advance on that.

avatar image Socapex YJack · Mar 14, 2016 at 04:34 AM 0
Share

Yes it's a little weird. In a way, I'm mainly interested in the great UI of UnityEvents. Traditional delegates usually work fine for programmatic stuff in my experience. This time though, I am making tools for someone else, which is a really fun challenge in and of itself. Using an inspector that he is comfortable with is a big bonus.

I got it working quite well actually, but I am hacking around internal stuff :) You can even change the target if you want (though you can't set scene objects inside a ScriptableObject).

All you need is a little reflection magic, nothing is really private in C# [wink wink]. I'm basically accessing the parameters inside UnityEvent. They're stored in a variant/union thing, but with an enum for types ( http://docs.unity3d.com/ScriptReference/Events.PersistentListener$$anonymous$$ode.html ). Then I use traditional reflection to call my methods on newly created objects.

Cheers

avatar image
0

Answer by rumaniel · Apr 07, 2016 at 11:29 AM

I use UnityEditor.Events.UnityEventsTools.AddPersistentListener();

Check this docs.

http://docs.unity3d.com/ScriptReference/Events.UnityEventTools.AddPersistentListener.html

Comment
Add comment · 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

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

46 People are following this question.

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

Related Questions

Correct way to send an event to a specific enemy 0 Answers

How to handle multiples instances of an object raising the same event? 0 Answers

Make prefab that generated more than once listener in Unity Events 0 Answers

Animation Event not working properly 0 Answers

Setting up EventTrigger programmatically for GameObject throws NullReferenceException 0 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