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
0
Question by Prodigga · Jul 25, 2012 at 11:02 AM · notifications

CSharpNotificationCenter Posting Messages with data

Hey guys, I am using the CSharpNotificationCenter and i cant work out how to attach some data to my message that i am posting. There isn't really any examples on the Wiki. Could someone help me out here? The example given on the wiki does not (unfortunetly) send any data. When I try to send a hashtable in Post to a function listening that accepts a HAshtable, I get an error: MissingMethodException: The best match for method ErrorDialogue has some invalid parameter.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BjoernS · Jan 15, 2013 at 12:02 PM

for me this solution worked:

 * Example use Post Notification / AddObserver with data:
  * 
  * 1) Example use Post Notification:
  * 
     // Post Notification with data:
     Hashtable hashtable = new Hashtable();
     hashtable.Add("attribute1", 1000);
     hashtable.Add("attribute2", 55);    
     NotificationCenter.DefaultCenter.PostNotification(this, "OnBumperCollision", hashtable);
  * 
  *  2) Example AddObserver and reuse posted data of Notification:
  * 
     using UnityEngine;
     using System.Collections;
     public class testObserveB : MonoBehaviour {
         void Start () {
             NotificationCenter.DefaultCenter.AddObserver(this, "OnBumperCollision");
         }
         void OnBumperCollision (Notification note)
         {
             Debug.Log("OnBumperCollision detected!");
             // reuse data if it exists
             if (note.data!=null) {
                 Debug.Log("Sender="+note.sender.name);
                 Debug.Log("Data:");
                 foreach (DictionaryEntry entry in note.data)
                 {
                     Debug.Log(" "+entry.Key +"="+ entry.Value);
                 }
             } else {
                 Debug.Log("data is null.");
             }
         }    
     }

but befor this can work, the Notification class must be accessible from outside:

     public class Notification {
      
             //public Notification (GameObject aSender, string aName, Hashtable aData)
             //{
             //    throw new System.NotImplementedException ();
             //}
      
         public Component sender;
         public string name;
         public Hashtable data;
         public Notification (Component aSender, string aName) { sender = aSender; name = aName; data = null; }
         public Notification (Component aSender, string aName, Hashtable aData) { sender = aSender; name = aName; data = aData; }
      
      
     }

this was not the case with the script in the wiki...

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

Answer by studio1h · Jan 13, 2015 at 02:46 AM

To send/access a notification's data parameter you must first send some data along with your notification:

 NotificationCenter.DefaultCenter.PostNotification(this, "OnPlayerEnter", someVectorThree);

Then:

 void Start()
     {
         NotificationCenter.DefaultCenter.AddObserver(this, "OnPlayerEnter");
     }
 
 void OnPlayerEnter(NotificationCenter.Notification note)
     {
         Vector3 someVectorThree = note.data;
     }



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

Answer by Kryptos · Jul 25, 2012 at 12:02 PM

I never used this script, but it looks like of a custom implementation of delegates, which are supported by Unity. I would like to know the advantage of using this script instead of built-in delegates (after my first read of the community page I didn't see any).

See: delegate (C# Reference) and Delegates (C# Programming Guide)

See also: Delegates Tutorial


edit: I think that the receiving method should have this kind of signature:

 void MethodName(object arg)
 {
     // cast inside the method
     Hashtable hash = arg as Hashtable;
     if (hash == null)
     {
         // error
     }
 }

But I'm not sure since I never used Unity built-in messaging system (which is the final step of this implementation).

Comment
Add comment · Show 4 · 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 Prodigga · Jul 26, 2012 at 02:07 AM 0
Share

here is a good description of the benifits i suppose. I really like the messaging system, it is indeed a custom implementation of delegates, but it follows the observer pattern.

avatar image Kryptos · Jul 26, 2012 at 07:38 AM 0
Share

I will take a look at it after work. Thanks for the info.

avatar image CalxDesign · Dec 10, 2012 at 11:16 PM 0
Share

Did anyone get any further with this? I'm trying to use the C# version of NotificationCenter and I'm also stuck on how to send data.

avatar image valentin-simian · Jan 08, 2013 at 03:05 AM 0
Share

I have not figured out how to send data (The script above does not work), but have been successful with this:

http://wiki.unity3d.com/index.php?title=CSharp$$anonymous$$essenger_Extended

avatar image
0

Answer by BjoernS · Jan 15, 2013 at 12:02 PM

 public class Notification {
  
     public Component sender;
     public string name;
     public Hashtable data;
     public Notification (Component aSender, string aName) { sender = aSender; name = aName; data = null; }
     public Notification (Component aSender, string aName, Hashtable aData) { sender = aSender; name = aName; data = aData; }
  
  
 }
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

10 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

Related Questions

NotificationServices' UnregisterForRemoteNotifications not persisting between game sessions? 0 Answers

iOS Notifications without Server 2 Answers

Mobile Notifications asking for location access when I manually trigger it 0 Answers

Android/iOS notifications when the game is not running 1 Answer

Send notification into unity from native thread 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