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
1
Question by Nerull22 · Jul 06, 2016 at 08:49 PM · eventsexecuteineditmode

Unity Events in Execute In Edit Mode

So I have a script that I want to execute in edit mode. The script works fine inside of play mode. I'm triggering a unity event in the update loop of execute in edit mode, but no event is being raised it seems. Is this expected behavior?

 [ExecuteInEditMode]
 public class CardManager : UIBehaviour
 {

     /// <summary>
     ///     Triggered when the loot item has changed.
     /// </summary>
     [SerializeField]
     private LootItemDataEvent _onLootItemAssigned = null;

         private void Update()
         {
             // If the previous item does not match the current then we've switch items and should trigger the event
             if (_lootItem == _previousLootItem)
             {
                 return;
             }
             Debug.Log("Switch!");
             _previousLootItem = _lootItem;
             _onLootItemAssigned.Invoke(_lootItem);
         }
 }

So there is clearly more to the code that I posted above, but this gives the quickest example of what I'm seeing. The "LootItemDataEvent" is just a custom unity event, I'll paste the code below in case there is an issue there.

 namespace Meta.Event
 {
     using System;
     using Loot;
     using UnityEngine.Events;
 
     /// <summary>
     ///     Allows a unity event to send loot item data with it.
     /// </summary>
     /// <seealso cref="UnityEngine.Events.UnityEvent{LootItemData}" />
     [Serializable]
     public class LootItemDataEvent : UnityEvent<LootItemData>
     { }
 }

Thanks in advance!

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

2 Replies

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

Answer by JoshDangIt · Jul 14, 2016 at 05:37 PM

Are you editing the UnityEvent in the inspector? If so, all you have to do is change the "Runtime Only" parameter to "Editor And Runtime".

Comment
Add comment · Show 2 · 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 Nerull22 · Jul 14, 2016 at 06:43 PM 0
Share

Well, don't I feel foolish. I will say that my method did work, but this is definitely the route to go. Thank you for posting and saving me from myself.

avatar image usernameHed · Aug 20, 2020 at 12:32 PM 0
Share

I was searching for a GetPersistentEventCount & Get$$anonymous$$ethod() (like @nerull22), but this method is... really obvious :D

avatar image
0

Answer by Nerull22 · Jul 14, 2016 at 02:28 PM

So I left this up for anyone to answer instead of myself as I don't really like answering my own question on a forum. But since there was absolutely no replies or answers provided, I'll do it in case someone runs across this again. I don't know if this is by design on Unity's part or not, but I have a workaround. See the code below.

 #if UNITY_EDITOR
 
         /// <summary>
         ///     Called once per frame.
         /// </summary>
         private void Update()
         {
             // Do not run this while playing
             if (Application.isPlaying)
             {
                 return;
             }
 
             // If the previous item does not match the current then we've switch items and should trigger the event
             if (_lootItem == _previousLootItem)
             {
                 return;
             }
             _previousLootItem = _lootItem;
 
             for (int i = 0; i < _onLootItemAssigned.GetPersistentEventCount(); i++)
             {
                 _onLootItemAssigned.GetPersistentTarget(i)
                     .GetType()
                     .GetMethod(_onLootItemAssigned.GetPersistentMethodName(i))
                     .Invoke(_onLootItemAssigned.GetPersistentTarget(i), new object[] {_lootItem});
             }
         }
 
 #endif

So this section is only for in the editor to use. I still call normal invokes on the object while the game is running in the editor or in deployment.

Basically it's just using a for loop to go through all the registered targets with the method on the event and is manually triggering them one by one using reflection. Not super ideal, but is functional.

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

45 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

Related Questions

How do I make a box in inspector appear that allows me to call functions in scripts similar to OnClick on Buttons? 2 Answers

Is there a way to implement mouse events in the game view in edit mode? 1 Answer

One listener for multiple objects? 2 Answers

New Input System how to fire events continuously? 2 Answers

ExecuteInEditMode is not working. 1 Answer


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