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
3
Question by Kayriuu · May 11, 2018 at 01:18 PM · editor-scriptingscriptableobjecteventsystemgenerics

generic event system with scriptable objects

Hey, I currently try to build myself an event system. I'm using scriptable Objects to build loose couplings. This is what I currently have:

  [CreateAssetMenu]  
     public class GameEvent : ScriptableObject  {  
         private List<Action> subscribers = new List<Action>();
 
         public void Raise() {
             for(int i = subscribers.Count - 1; i >= 0; i--)  { subscribers[i](); }
         }
 
         public void Subscribe(Action subscriber) {
             subscribers.Add(subscriber);
         }
         public void Unsubscribe(Action subscriber) {
             subscribers.Remove(subscriber);
         }
     }

With that code I can subscribe every method without parameters by doing that:

 public class ExampleClass : MonoBehaviour {
     [SerializeField]
     private GameEvent gameEvent;
 
     private void OnEnable() {
         gameEvent.Subscribe(OnEventRaised);
     }
     private void OnDisable() {
         gameEvent.Unsubscribe(OnEventRaised);
     }
 
     public void OnEventRaised() { }
 }  


So know to my question!
I want to make a version of that that can take parameters but the only solution I found was making a new custom GameEvent class for every new combination of variable types. Like this for sending an int variable.

 [CreateAssetMenu]
 public class GameEventInt : ScriptableObject {
     private List<Action<int>> subscribers = new List<Action<int>>();
 
     [SerializeField]
     private int debugValue = 0;
     public int DebugValue { get { return debugValue; } }
 
     public void Raise(int value) {
         for (int i = subscribers.Count - 1; i >= 0; i--)  { subscribers[i](value);  }
     }
 
     public void Subscribe(Action<int> subscriber)  {
         subscribers.Add(subscriber);
     }
     public void Unsubscribe(Action<int> subscriber) {
         subscribers.Remove(subscriber);
     }
 }

(I also have a custom Editor to Raise the Event from the inspector of the created scriptable objects, therefore, is the debugValue)
So do you have an idea how to make a more generic version where I can select the variable type after creating the scriptable object?
(The best would be to have the option to select if and how many parameters you want to sent and then the types of the variables but I think that's very complex so I would be happy with the select variable type thing :D)


Ps: That's my custom editor just if you need it:

 [CustomEditor(typeof(GameEventInt))]
 public class GameEventIntEditor : Editor {
     public override void OnInspectorGUI()  {
         DrawDefaultInspector();
         GameEventInt myScript = (GameEventInt)target;
         if(GUILayout.Button("Raise Event")) {
             myScript.Raise(myScript.DebugValue);
         }
     }
 }
Comment
Add comment · Show 2
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 RobAnthem · May 11, 2018 at 01:25 PM 0
Share

What if, ins$$anonymous$$d of using Actions, you create a class than has an action as a member, with additional information. You could create an enum of types the events can take, and parse the inco$$anonymous$$g event object into whatever it is expecting. Then your game event class can keep a list of these new actionable classes, and handle them accordingly.

avatar image VideoJames · Nov 20, 2018 at 09:13 AM 1
Share

Did you ever find an answer to this? I would love to know your solution if you did.

0 Replies

· Add your reply
  • Sort: 

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

97 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Serializing specialized subclasses of generic classes not working 1 Answer

Get list of all "Action" classes 1 Answer

How i can subscribe to an Event through Inspector 0 Answers

Stop a MonoBehavior from being addable as a script? 0 Answers

Scriptable Objects, how to force Include in Compile / Build without referencing it in the scene? 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