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
1
Question by fendorio · Apr 10, 2014 at 02:14 AM · c#plugin.net-assemblies

Using async/await operators in a WP8 plugin

Hi there,

The question is, how can I use Async methods in a Windows plugin?

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

1 Reply

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

Answer by fendorio · Apr 10, 2014 at 02:15 AM

Well this afternoon for some hours I spent my time making an IAP plugin for WP8. I spent at least an hour searching for how to use wrap async/await operators in a .Net 3.5 framework class library, and finally found out encapsulation is the answer!

You simple need to mark the methods that use Async/await as private.

In your WP8 dll you'll have some method like this that uses async:

     private static async void GetProductInfo(string productInfo)
     {
         var listing = await CurrentApp.LoadListingInformationAsync();

         //Blah blah code..
     }

Now if you're reading this I assume you know that to use the plugin you need to make a 'fake' class library on-top of this so that the editor doesn't pick up on the WP8 specific code.

All that needs to be done is to create a new class library project targeting .Net 3.5, and have it include every method you wish to use from the 'real' dll targeting the WP8 specific code, the method signatures must match, along with the assembly namespaces must match also.

This is what tripped me up, you can't use Async in .Net 3.5.

Writing the following in the 'fake' class lib that targets .Net 3.5:

     private async void GetProductInfo(string productInfo)
     {
          //This method really can just be left empty as it won't be called
            from the Windows device
     }

Throws an error, and rightly so - Async is not implemented in .Net 3.5.

What you must do is:

In the 'real' dll you need to mark the async methods as private and have public methods call them, like so:

     private async void GetProductInfo(string productInfo)
     {
         var listing = await CurrentApp.LoadListingInformationAsync();

         //Blah blah code..
     }

     public void GetProductInfoPublic(string ProductInfoPublic)
     {
         //Call the private async method in the non-async public method
         GetProductInfo(ProductInfoPublic);
     }

And that's it for the 'real' dll.

Now if you remember from above, the 'fake' dll must have matching method signatures for the methods you wish to use in your unity script. Since we made the Async methods private, you don't need to use them!

So in the 'fake' dll all you have to add is the following:

  public void GetProductInfoPublic(string ProductInfoPublic)
  {
        //Again nothing really needs to be here..
  }

And that's you done! Calling the GetProductInfoPublic method above on a windows device will result in the method in the 'real' dll being called, which calls the private async method, calling it from the editor, or a non windows device will result in the method in the 'fake' dll being called.

One last thing, in both dlls' both the 'Default namespace' and 'Assembly name' properties need to match.

In visual studio you can edit this by right-clicking on the project in the solution explorer, click properties, the two text boxes with the above two fields will be the first thing you see.

Hope that helps someone out there! Feel free to comment with any questions.

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 MagicBen · Sep 02, 2014 at 08:13 PM 0
Share

Good!! Thank you for this post, very useful. But I cannot make it work with Universal Apps trying to call: await Launcher.LaunchUriAsync(...)

Any idea?

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I connect unity to my smart fox server plugin 1 Answer

Is it possible to reference .NET references and give up cross-platform? 1 Answer

"Error Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default" using .Net 4.6 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