Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

How to run Unity app several time in UWP app

Discussion in 'Windows' started by unity_0PfXqw83o46aIg, Oct 12, 2018.

  1. unity_0PfXqw83o46aIg

    unity_0PfXqw83o46aIg

    Joined:
    Oct 12, 2018
    Posts:
    3
    Hi everyone,

    Is it possible to run several unity apps from UWP project code?
    I mean that I have Unity project builded as UWP application, and I want to use Unity application as a control several time, in different pages.

    I have two problems:
    1) I can't reset SwapChainPanel, when I do SetSwapChainPanel second time I get empty space in place whe should be Unity frame.
    2) I don't know and can't find the answer can I run Unity app two times at the same time and use two unity frames in one UWP page or not?

    If somebody can solve my problems It would be great.
    Thanks!

    P.S. I'm sorry for my maybe not very good english :)
     
  2. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,531
    You cannot run Unity app twice at the same time. You can, however, unload a Unity app using "Application.Unload()" and then initialize it from scratch again (recreating AppCallbacks, etc).
     
  3. unity_0PfXqw83o46aIg

    unity_0PfXqw83o46aIg

    Joined:
    Oct 12, 2018
    Posts:
    3
    Do you mean do
    Application.Unload();
    in Unity script and then just
    AppCallbacks = new AppCallbacks();
    ?

    I get exception when I use
    AppCallbacks = new AppCallbacks();
    second time, even after
    Application.Unload();
    .

    I suspect that I not exactly understand main idea.
     
  4. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,531
    Can you post the full code? What's the exception that you're getting? Which Unity version are you on?
     
  5. unity_0PfXqw83o46aIg

    unity_0PfXqw83o46aIg

    Joined:
    Oct 12, 2018
    Posts:
    3
    Yes, sure.
    Version of unity: 2018.2.11f1.
    With exception I have problem, I can't see what exception exactly I have. Visual studio just suggest me open different visual studio, but even after this I don't see details of exception.

    Code below.

    Unity script:

    Code (CSharp):
    1. using UnityEngine;
    2. using Application = UnityEngine.Application;
    3.  
    4. public class CubeBehaviourScript : MonoBehaviour
    5. {
    6.     private static bool _isNeedUnload;
    7.     private static RotationDirection _direction;
    8.  
    9.     public static void UnloadApplication()
    10.     {
    11.         _isNeedUnload = true;
    12.     }
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.         _isNeedUnload = false;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if (_isNeedUnload)
    24.         {
    25.             _isNeedUnload = false;
    26.             Application.Unload();
    27.         }
    28.     }
    29. }
    App.xaml.cs:

    Code (CSharp):
    1. using Windows.ApplicationModel.Activation;
    2. using Windows.UI.ViewManagement;
    3. using Windows.UI.Xaml;
    4. using Windows.UI.Xaml.Controls;
    5. using Unity;
    6. using UnityPlayer;
    7.  
    8. // The Blank Application template is documented at http://go.microsoft.com/fwlink/?LinkId=234227
    9.  
    10. namespace Cube
    11. {
    12.     /// <summary>
    13.     ///     Provides application-specific behavior to supplement the default Application class.
    14.     /// </summary>
    15.     sealed partial class App : Application
    16.     {
    17.         public static AppCallbacks AppCallbacks;
    18.  
    19.         /// <summary>
    20.         ///     Initializes the singleton application object.  This is the first line of authored code
    21.         ///     executed, and as such is the logical equivalent of main() or WinMain().
    22.         /// </summary>
    23.         public App()
    24.         {
    25.             InitializeComponent();
    26.             SetupOrientation();
    27.         }
    28.  
    29.         /// <summary>
    30.         ///     Invoked when the application is launched normally by the end user.  Other entry points
    31.         ///     will be used when the application is launched to open a specific file, to display
    32.         ///     search results, and so forth.
    33.         /// </summary>
    34.         /// <param name="args">Details about the launch request and process.</param>
    35.         protected override void OnLaunched(LaunchActivatedEventArgs args)
    36.         {
    37.             InitializeUnity(args.Arguments);
    38.         }
    39.  
    40.         private void InitializeUnity(string args)
    41.         {
    42.             AppCallbacks = new AppCallbacks();
    43.             ApplicationView.GetForCurrentView().SuppressSystemOverlays = true;
    44.  
    45.             AppCallbacks.SetAppArguments(args);
    46.             var rootFrame = Window.Current.Content as Frame;
    47.  
    48.             // Do not repeat app initialization when the Window already has content,
    49.             // just ensure that the window is active
    50.             if (rootFrame == null && !AppCallbacks.IsInitialized())
    51.             {
    52.                 rootFrame = new Frame();
    53.                 Window.Current.Content = rootFrame;
    54. #if !UNITY_HOLOGRAPHIC
    55.                 Window.Current.Activate();
    56. #endif
    57.                 rootFrame.Navigate(typeof(StartPage));
    58.             }
    59.  
    60.             // Setup scripting bridge
    61.             var bridge = new WinRTBridge.WinRTBridge();
    62.  
    63.             AppCallbacks.SetBridge(bridge);
    64.             AppCallbacks.InitializeD3DXAML();
    65.             AppCallbacks.SetCoreWindowEvents(Window.Current.CoreWindow);
    66.             Window.Current.Activate();
    67.             // I have exception after this point
    68.         }
    69.  
    70.         public static void ReInitializeUnity()
    71.         {
    72.             CubeBehaviourScript.UnloadApplication();
    73.             // This we need some delay, actualy I do "UnloadApplication" on button click and then ReInitializeUnity() on click of other button
    74.             ((App) Current).InitializeUnity(string.Empty);
    75.         }
    76.  
    77.         private void SetupOrientation()
    78.         {
    79.             UnityGenerated.SetupDisplay();
    80.         }
    81.     }
    82. }
    If it will usefull would be great.
    Anyway, Thanks!
     
  6. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,531
    Don't recreate WinRTBridge - that will cause issues. I'll post a code sample we use to test this later.

    By the way - you can enable "Mixed mode debugging" in project properties to be able to see details of the exception.
     
    semih-guresci likes this.
  7. semih-guresci

    semih-guresci

    Joined:
    Sep 3, 2013
    Posts:
    2
    I have the same problem, after Application.Unload() unity could not start properly. I removed unload and add
    Code (CSharp):
    1. NavigationCacheMode = Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
    Then, I managed to display unity and navigate between pages. but this way unity stays in the memory.

    Can you share the simple code to initialize unity multiple times in an uwp application?

    Thanks!
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,531
    Yeah. Here's code from our automated test:

    Code (csharp):
    1. void App::RunUnityPlayer()
    2. {
    3.     auto appArgs = ref new Platform::Array<String^>(1);
    4.     appArgs[0] = ref new Platform::String(L"-appendlog");
    5.     auto appCallbacks = ref new AppCallbacks(appArgs);
    6.     appCallbacks->SetCoreApplicationViewEvents(m_ApplicationView);
    7.     appCallbacks->SetCoreWindowEvents(m_CoreWindow);
    8.     appCallbacks->InitializeD3DWindow();
    9.     appCallbacks->Run();
    10. }
    11.  
    12. void App::Run()
    13. {
    14.     for (int i = 0; i < 10; i++)
    15.     {
    16.         RunUnityPlayer();
    17.  
    18.         // Pump UI events for 3 secs so all of our threads waiting on UI thread get a chance to exit
    19.         LARGE_INTEGER frequency, start, current;
    20.         QueryPerformanceFrequency(&frequency);
    21.         QueryPerformanceCounter(&start);
    22.         current = start;
    23.  
    24.         do
    25.         {
    26.             Windows::UI::Core::CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(Windows::UI::Core::CoreProcessEventsOption::ProcessAllIfPresent);
    27.             QueryPerformanceCounter(&current);
    28.         }
    29.         while (static_cast<double>(current.QuadPart - start.QuadPart) / static_cast<double>(frequency.QuadPart) < 3.0);
    30.  
    31.         CoFreeUnusedLibrariesEx(0, 0);
    32.     }
    33. }