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 tddaniels · May 26, 2015 at 02:34 AM · c#adscallback

Callback help...cuz it's hard

I'm trying to integrate with an ad service. They provide a callback class I can use to determine when a video has ended/completed/etc. However the class is not monobehavior, instead it is AndroidJavaProxy. This causes me problems because I can't access any of my scripts from here. I'm not sure how to receive a value back letting me know what the callback script has actually done.

The basics are like so:

In my class I set up the call back like this:

  SomeAdService.setVideoCallbacks(newSomeAdServiceVideoCallbacks());    
  SomeAdService.Show(SomeAdService.Video)
 //////////////////////////////////////////////////
 //SomeAdServiceVideoCallbacks script below
 ///////////////////////////////////
 using UnityEngine;
 using System;    
 public class SomeAdServiceVideoCallbacks : AndroidJavaProxy
 { 
  public SomeAdServiceVideoCallbacks() : base("com.SomeAdService.ads.VideoCallbacks") { }
     void onVideoFinished() 
     {
        //stuff happens here but now do I send a value back to my calling script 
     }
     
 }

I have 0 experience with delegates, callbacks, lamdas, kung fu, etc and looking over the interwebs has just left me more confused. How can I get my class to read the results of the callback?

Appreciate any help

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nerull22 · May 26, 2015 at 03:16 AM

So I'm gonna' try and answer your question as I understand it. I think where you're falling short is your understanding of delegates and events. I don't know what ad service your using or what exactly you're looking for, so I'll try and explain delegates and events to help you understand, and hopefully you can fix the problem yourself.

So a delegate is required in order for an event to trigger. A delegate is a template for an event. So an event is just like it sounds, it's when something else happens. A delegate tells it what should be returned and what parameters the calling functions need. This will make more sense in a minute.

A delegate in C# is written:

 public delegate int ExampleDelegate(int x, int y);

Alright, so we classify it as a delegate. The next is the int, this is what any function should return. And then it ends with the two ints in the paranthesis, those are parameters that functions should take. So like an interface defines what methods an inheriting class should have, a delegate defines the structure of functions given should have. So now we'll move to events and show how delegates come in.

An event in C# will be written as:

 public event ExampleDelegate Changed;

So we do as we did with the delegate and instead we define the variable as an event. Then the identifier is the delegate that we defined earlier. And lastly is the name of the event that we just created. So what's cool about this, is now we can add as many methods as we want to listen to this event. And then whenever the event is triggered, all of those methods will be called, without the necessity of us having to call them one at a time. An example will probably help a lot.

 private void Awake()
 {
    Changed += MethodOne;
    Changed += MethodThree;
 }
 
 private int MethodOne(int x, int y)
 {
    // Method one will be called
 }
 
 private int MethodTwo(int x, int y)
 {
    // This won't be called
 }
 
 private int MethodThree(int x, int y)
 {
    // Method Three will be called
 }

So as you can see above, we add method one and method three to watch that event. So whenever the method shown below gets invoked method one and method three code will run, but method two won't.

 Changed.Invoke();

So that ad service is waiting for a response back from the video or their servers or something, and then after it receives that information, it's triggering this event to occur. Or in your case, you're passing a method that you've defined in as a delegate to their function, so they know what function to call when that event occurs.

I highly recommend going over a lot of tutorials of delegates and events until you really understand them, as they're very powerful and can lead to many many great coding in the future. Lambdas are a little harder to understand, but super powerful when you combine them with Linq queries. I hope this helps.

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 tddaniels · May 26, 2015 at 03:34 AM 0
Share

Yes, this helps very much. Using this I can start to see how I can pull back some info - still got a ways to go but this definitely helps.

avatar image
0

Answer by HTwist · May 26, 2015 at 03:34 AM

   SomeAdService.setVideoCallbacks(new SomeAdServiceVideoCallbacks(() => println("ad finished")));    
   SomeAdService.Show(SomeAdService.Video)
  //////////////////////////////////////////////////
  //SomeAdServiceVideoCallbacks script below
  ///////////////////////////////////
  using UnityEngine;
  using System;    
  public class SomeAdServiceVideoCallbacks : AndroidJavaProxy
  { 
      private Action onFinished;
      public SomeAdServiceVideoCallbacks(Action callback) : base("com.SomeAdService.ads.VideoCallbacks") 
      { 
         this.onFinished = callback; 
      }

      void onVideoFinished() 
      {
         this.onFinished();
         //stuff happens here but now do I send a value back to my calling script 
      }
      
  }

I haven't tested it but basically the constructor takes an action. An action is a reference to a method (void) which which we will call when onVideoFinished is executed.

So at the moment you instantiate the class, either pass a method or directly write a lamdba which represent a method eith no parameters "()" and return no results (void) but only execute side effects which here is to print a text saying the video is done.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

OnAdRewarded called twice 0 Answers

An OS design issue: File types associated with their appropriate programs 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