Search Unity

Using UWP OnActivated with Unity

Discussion in 'Windows' started by me_Stoffel, Sep 25, 2017.

  1. me_Stoffel

    me_Stoffel

    Joined:
    Dec 5, 2014
    Posts:
    7
    I'm trying to have my application open for a specific protocol. For this I set up the player settings for the protocol and when I open an URL the application gets started succesfully, so that part is fine.

    However I'm at loss how to get the callback with the Url within Unity. I read about the OnActivated event that can be overridden in the VS project. But how can I add this functionality to my unity c# code, so that I don't have to manually edit the VS project for each build? Here https://docs.unity3d.com/Manual/windowsstore-appcallbacks.html it is mentioned, that unity subscribes to most of the callbacks, also for Activated. However I have no idea how to subscribe to these now.

    Any help or hints would be appreciated!
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
  3. me_Stoffel

    me_Stoffel

    Joined:
    Dec 5, 2014
    Posts:
    7
    Thanks for the tip, I'm trying to get the arguments like this:
    Code (CSharp):
    1.  
    2.     private void Start()
    3.     {
    4.     UnityEngine.WSA.Application.windowActivated += Application_windowActivated;
    5.     }
    6.  
    7.     private void Application_windowActivated(UnityEngine.WSA.WindowActivationState state)
    8.     {
    9.     Debug.LogError("Application_windowActivated - state: " + state);
    10.     var arguments = UnityEngine.WSA.Application.arguments;
    11.     Debug.LogError("Application_windowActivated - arguments: " + arguments);
    12.     }
    13.  
    However the debug log shows the arguments to be empty:

    Application_windowActivated - state: CodeActivated
    (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)

    Application_windowActivated - arguments:
    (Filename: C:/buildslave/unity/build/artifacts/generated/Metro/runtime/DebugBindings.gen.cpp Line: 51)

    The app gets activated via the given protocol, which I guess is indicated by the window activated state CodeActivated. So I thought the arguments should then contain the url, but they just are always empty.

    Any idea what I'm doing wrong?
     
    Last edited: Sep 25, 2017
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    I'm not sure url is passed to those arguments.
    In App.xaml.cs see OnActivated method. URL is available there. You can add public static variable to one of your scripts, it will be accessible from XAML cs code.
     
  5. me_Stoffel

    me_Stoffel

    Joined:
    Dec 5, 2014
    Posts:
    7
    Hmm, so would I need to do this within the build visual studio project? Would I then to do this each time I built manually or is there a better way?

    Is it the App.cs file in the build project in vs? Can't seem to find an App.xaml.cs and no OnActivated method in the whole project.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    You can build from Unity on top of already exported solution, your custom modifications should be kept. Yes, App.xaml.cs should be in exported solution (assuming you use XAML export type and .NET scripting backend).
     
  7. me_Stoffel

    me_Stoffel

    Joined:
    Dec 5, 2014
    Posts:
    7
    Okay, got it working with editing App.xaml.cs.Now I only would like to have that one line to call my class added automatically, as I want to use a automated build setup later. However now just building in the same folder with the changes being kept is fine. So thanks Aurimas-Cernius! :)
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    The usual thing that people do that want to have modified VS solution is that they check the modified files into their source control along side the Unity project and then always build on top of the same folder. Just make sure to not check in everything as it's going to be a waste of your server space and will make your source control slower.
     
  9. ISeidingerReflekt

    ISeidingerReflekt

    Joined:
    Jun 2, 2017
    Posts:
    5
    Hi there, I am wondering if i can also use deeplinking with d3d? I am using holotoolkit and have some dependencies there for d3d. Hints and help appreciated!
     
    me_Stoffel likes this.
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    What do you mean by deeplinking with d3d?
     
  11. ISeidingerReflekt

    ISeidingerReflekt

    Joined:
    Jun 2, 2017
    Posts:
    5
    I meant how can I get the Url that started the app when I build with BuildType D3D instead of Xaml.
    I figured out the solution, similar and thanks to the Xaml solution in this post:
    In the generated App.cs I changed the following method:
    Code (CSharp):
    1.  
    2.         private void ApplicationView_Activated(CoreApplicationView sender, IActivatedEventArgs args)
    3.         {
    4.  
    5.             CoreWindow.GetForCurrentThread().Activate();
    6.         }
    to:

    Code (CSharp):
    1.         private void ApplicationView_Activated(CoreApplicationView sender, IActivatedEventArgs args)
    2.         {
    3.             switch (args.Kind)
    4.             {
    5.                 case ActivationKind.Protocol:
    6.                     ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
    7.                     Your.Static.Field = eventArgs.Uri.AbsoluteUri;
    8.                     break;
    9.             }
    10.             CoreWindow.GetForCurrentThread().Activate();
    11.         }
     
  12. abhijit93

    abhijit93

    Joined:
    Mar 5, 2016
    Posts:
    4
    @Tautvydas-Zilys This all works fine with .NET backend. But when I'm building for IL2CPP, How exactly can I call my function (defined in one of my unity scripts) in ApplicationView_Activated in App.cpp?
     
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    abhijit93 likes this.
  14. marck_ozz

    marck_ozz

    Joined:
    Nov 30, 2018
    Posts:
    107
    Hello all.

    I'm not able to find the "App.xaml.cs" file in my project. I have the next build configuration in order to reduce the iteration time between builds since I'm testing some changes in the app.

    1.- Does the "App.xaml.cs" is only generated with "XAML Project" type?
    2.- where should I find the "App.xaml.cs" file?

    upload_2021-3-24_0-51-9.png


    Thanks in advance!!
     

    Attached Files:

  15. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Unity nowadays generated App.xaml.cpp for XAML builds. App.xaml.cs was only generated for .NET scripting backend which got removed in 2019.1.
     
    marck_ozz likes this.
  16. marck_ozz

    marck_ozz

    Joined:
    Nov 30, 2018
    Posts:
    107
    Thank you!!

    Too bad, my intend was to catch the "Close Event" in my desktop app, with the restricted capability "confirmAppClose" and in most of the UWP forums they suggest to modify the "App.xaml.cs" like this:

    https://stackoverflow.com/questions/35056464/uwp-on-desktop-closed-by-top-x-button-no-event
    or
    https://blog.mzikmund.com/2018/09/app-close-confirmation-in-uwp/

    So, there's no way to access to the "App.xaml.cs"?? I'm using 2019.15LTS I wouldn't like to downgrade my project.
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    App.xaml.cpp is the equivalent of App.xaml.cs. It does exactly the same things.

    That said, the code you linked can just be added to any of your Unity scripts in the Awake() method. There's no need to modify any of the generated projects.
     
    marck_ozz likes this.
  18. marck_ozz

    marck_ozz

    Joined:
    Nov 30, 2018
    Posts:
    107
    :eek:

    Ok ok Thanks a lot, I'll work on that.