Anyone know how to callback into unity from a Windows Phone 8 Plugin? If I recall from iOS I was able to call UnitySendMessage, however I’m not sure how to do it from my windows phone plugin.
Have you read the porting documentation?
Meltdown,
Thanks I will review the documentation.
Cool just found an example in the porting documentation, if anyone needs it here it is:
Marshall Unity calls in the Windows Phone app
Porting tips for Windows Phone with Unity 10
Always ensure that when calling into the Unity side, that you marshal back onto the App Thread.
Here’s an example where we are calling back into Unity after the window is resized.
UnityApp.BeginInvoke(() =>
{
// back to Unity
}, false);
Conversely use the Dispatcher to call into the Windows Phone side and switch from the Unity app
thread to the Windows Phone UI thread. For example, using the Windows Phone APIs for in app
purchases will require you to be on the UI thread to avoid exceptions.
Dispatcher.BeginInvoke(() =>
{
// UI Thread
}, false);
Meltdown,
I read the documentation but I’m still unsure on how to make my plugin as generic as possible. So I want my plugin to be self contained, meaning that it can callback to Unity without having to make modification to the build app. Based on the documentation looks like it assumes that you add a Dispatcher to your plugin which I’ve done, but then the MainPage.cs in the app needs to be modified with the following:
// Constructor
public MainPage()
{
// wire up dispatcher for plugin
MyPlugin.Dispatcher.InvokeOnAppThread = InvokeOnAppThread;
MyPlugin.Dispatcher.InvokeOnUIThread = InvokeOnUIThread;
}
public void InvokeOnAppThread(Action callback)
{
UnityApp.BeginInvoke(() => callback());
}
public void InvokeOnUIThread(Action callback)
{
Dispatcher.BeginInvoke(() => callback());
}
I’m ok with adding that, however what If I want to callback into Unity from my own plugin? This line works if you add the call back methods inside the MainPage.cs
Dispatcher.InvokeOnAppThread(() =>
{
// back to Unity
}, false);
What if I want to call back from my plugin and not the MainPage.cs ?
If you want to make your plugin as generic as possible, why do you need to tightly couple it to your game?
The plugin should have no idea about which game is being used.
What are you trying to do specifically?
All I’m trying to do is call back into Unity from my plugin, so for example I’m creating a plugin that basically provides Native MessageBox.show in which one of the overwrites methods required a call back to know if the user pressed OK or CANCEL buttons.
Does it make sense?
It should work regardless from where you’re invoking the dispatcher.
For the purpose of a re-usable plugin, no it doesn’t make sense.
This is the kind of thing that should rather be done in the MainPage.xaml.cs specific to the game.
But if you really want to call back into Unity from your plugin, just use the dfirect communication approach with a public Action on the Unity class that you want to call back into. The source code of our sample project has a UnityLoaded Action in WindowsGateway.cs. You can do something similar so that event is raised from within the plugin.
The thing I still don’t understand clearly after reading the doc and looking at the code is: when is the dispatcher absolutely needed and when is it not?
For example, it’s not used for Windows Phone in your ShowShareUI function, why is that? I wrote something similar a couple of days ago to share images after reading Unity’s guide (https://docs.unity3d.com/Documentation/Manual/wp8-plugins-guide-csharp.html), it works fine or I’m just being lucky, and I’m only learning about the dispatcher now that I’m looking into more complex scenarios requiring an async operation, and an event being raised on Unity’s side once it completed. Is that the difference, when an operation is async I need the dispatcher to ensure it’s running on the right thread, whereas I can live without it for short synchronous fire and forget calls?
Redirecting your function through the dispatcher is usually only needed interacting with XAML UI.
Will this UnityApp.BeginInvoke occur in any defined point on the unity side? I mean, will it always be executed during update, or could it as easily happen after LateUpdate and before rendering etc?
It will occur after current frame but before next frame. That means it will happen after both Update() and LateUpdate().