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
9
Question by Lando · Aug 11, 2011 at 04:59 PM · delegatecallback

How to make a callback function

Hi, i was wondering if someone could show or point me in the right direction of making my own delegate/callback function. Basically I want to make something that fires as a result of something, for instance if you have used the prime31 plug-ins (social networking ones in particular) they have delegates that fire once you have logged in or if you've failed to log in, hope i explained this well enough as im not the most experienced of programmers, any help would be greatly appreciated, thanks.

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
12
Best Answer

Answer by ABerlemont · Jun 18, 2014 at 10:09 AM

I found the answer of that question through those examples.

http://forum.unity3d.com/threads/another-noob-question-callback-functions-solved.27998/

http://msdn.microsoft.com/en-us/library/aa288459%28v=vs.71%29.aspx

 public delegate void WhateverType(); // declare delegate type
 
 protected WhateverType callbackFct; // to store the function
 
 public void something(WhateverType myCallback){  
   callbackFct = myCallback;
 }
 
 void callProcess(){
   something(dosomething);
 }
 
 void dosomething(){
   //...
 }
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 ABerlemont · May 17, 2017 at 12:15 PM 4
Share

Another faster way to do the same thing would be to use the Action class from System

 using System;
 
 public Action myAction;
 public Action<string> anotherAction;
 
 void Start(){
   myAction += callback;
   anotherAction += callbackString;
 
   myAction();
   anotherAction("foo");
 }
 
 void callback(){ ... }
 void callbackString(string aStringParam){ ... }

and you can subscribe multiple function to the same Action.

 myAction += aCallback;
 myAction += bCallback;
 
 myAction(); // will call a and b
avatar image
5

Answer by Ray-Pendergraph · Aug 11, 2011 at 07:13 PM

Have you tried Microsoft's delegate tutorial? That's pretty much all you need to know to start creating delegates. It has code samples, links to other resources and good explanation.

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 Physis · Nov 05, 2017 at 07:16 PM 0
Share

Thanks. It was really usefull

avatar image
3

Answer by Alumi · May 11, 2018 at 02:39 AM

refer to this article: Link, using System.Action is the fastest way to implement callback function.

 public IEnumerator FetchResponseFromWeb (System.Action<bool> callback) {
     WWWForm form = new WWWForm();
     WWW www = new WWW(url, form);
 
     // ‘done’ will be null until the information has been fetched from the web or there is an error
     yield return www;
 
     // Make sure all code paths set the callback
     if(www.error != null) {
         callback(false);
     } else {
         print(www.text);
         callback(true);
     }
 }
 
 Start() {
     StartCoroutine(FetchResponseFromWeb(callbackFunc);
 }
 
 public void callbackFunc(bool done) {
     if(done != null) {
         if(done)
             print(“successfully fetched something from the web”);
         else
             print(“There was an error”);
     }
 }


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

12 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

Related Questions

Action delegate doesn't show in inspector. 1 Answer

Login form: Do I have to set custom delegates to null in OnDestroy? 0 Answers

Navmesh event on navigation end 0 Answers

How can callback from shared object 0 Answers

Delegate.Target null-check fails when it's actually null and also prints as null. 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