Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with communication from Unity to XAML in example XALMUnityConnectionV6.zip

Discussion in 'Windows' started by carlosricardocm, Dec 26, 2014.

  1. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi, I downloaded the example XAMLUnityConnectionV6.zip from http://docs.unity3d.com/Manual/windowsstore-examples.html. When I run the code alll work correctly.

    My problem is when I change this code just a little.
    What I want is launch the game when a user clicks a button from a xaml page that I create insted of launching the game from the begging i.e.

    Code from PaginaInicial.xaml (xaml page with the button that will launch the game)
    Code (CSharp):
    1.  
    2. //Button that launch the game
    3. private void Button_Click(object sender, RoutedEventArgs e)
    4.         {
    5.           if (!appCallbacks.IsInitialized())
    6.             {
    7.                 //Here I change MainPage to launch the game withouth need of SplashScreen
    8.                 var mainPage = new MainPage();
    9.  
    10.                
    11.                 Window.Current.Content = mainPage;
    12.                 Window.Current.Activate();
    13.  
    14.                 mainPage.RemoveSplashScreen();
    15.                 mainPage.PositionImage();
    16.  
    17.                 // Setup scripting bridge
    18.                 _bridge = new WinRTBridge.WinRTBridge();
    19.                 appCallbacks.SetBridge(_bridge);
    20.  
    21.                 appCallbacks.SetSwapChainBackgroundPanel(mainPage.GetSwapChainBackgroundPanel());
    22.  
    23.                 Debug.WriteLine("La ventana actual es:"+Window.Current.ToString());
    24.  
    25.                 appCallbacks.SetCoreWindowEvents(Window.Current.CoreWindow);
    26.  
    27.                 appCallbacks.InitializeD3DXAML();
    28.             }
    29.  
    30.             Window.Current.Activate();
    31.         }
    32.  
    *Note that I change MainPage.xaml to do not use splashscreen because I don't what launch the game from the beggining

    So, when I click this button the example is launched and all looks right except the communication from Unity to XAML (even the comunnication from XAML to Unity works).

    Any ideas?

    Regards
    PD. I attach the files that I changed
     

    Attached Files:

  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,627
    Which version of Unity do you use?
    There was a bug that you couldn't start unity player on demand, but it should be fixed in most recent version.
     
  3. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi, I have the most recent version. (4.6.1).

    Regards,
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    Do you still execute - Communications.SetEvent(UnityToXAML); as the original sample did?
     
  5. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi, I execute Communications.SetEvent(UnityToXAML), as the original sample did.

    Code (CSharp):
    1. private void OnInitialized()
    2.         {
    3.             Communications.SetEvent(UnityToXAML);
    4.         }
    5.         public void UnityToXAML(object arg)
    6.         {
    7.             UnityPlayer.AppCallbacks.Instance.InvokeOnUIThread(new UnityPlayer.AppCallbackItem(() =>
    8.             {
    9.                 eventWasReceivedCount++;
    10.                 textBox.Text = "Event received " + eventWasReceivedCount + " times";
    11.             }
    12.             ), false);
    13.  
    14.  
    15.         }
    And the code for the class Communications it's the same as the original sample.

    Code (CSharp):
    1. public delegate void UnityEvent(object arg);
    2.     public sealed class Communications
    3.     {
    4.         public static void SetCubeMaterialColor(byte r, byte g, byte b)
    5.         {
    6.             UnityEngine.GameObject go = UnityEngine.GameObject.Find("Cube");
    7.             UnityEngine.Material mat = go.GetComponent<XAMLConnection>().material;
    8.             mat.color = new UnityEngine.Color32(r, g, b, 255);
    9.         }
    10.         public static void SetEvent(UnityEvent e)
    11.         {
    12.             UnityEngine.GameObject go = UnityEngine.GameObject.Find("Cube");
    13.             if (go != null)
    14.             {
    15.                 go.GetComponent<XAMLConnection>().onEvent = new XAMLConnection.OnEvent(e);
    16.             }
    17.             else
    18.             {
    19.                 throw new Exception("Cube not found, have exported the correct scene?");
    20.             }
    21.         }
    22.     }
    In the attached files you can find all the changes I did to the original example.
    If it's necesary I post all the changes but basically I made changes in App.xaml.cs MainPage.xalm.cs and add a new Page called PaginaInicial.xaml with a button to launch the unity player.

    Regards,
    Ricardo
     
  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    Well, the easiest way to find the problem is to export Unity solution with Unity C# projects checkbox checked - http://docs.unity3d.com/Manual/windowsstore-debugging.html and then put breakpoints in the code and see if everything is set correctly.

    Do note though, if you check Unity C# projects checkbox, Unity will need to replace *.sln file, so either delete it and rename it to something else.
     
  7. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi, I did what you said (about rebuild with debug option enabled) but there is not error and all looks fine except the communication from Unity to XAML.
    I attach my Log file.
     

    Attached Files:

    • Log.txt
      File size:
      39.8 KB
      Views:
      1,913
    Last edited: Jan 12, 2015
  8. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Update: I notice that when I switch among windows (with command alt + tab) when I get back to the example all works fine again.
     
  9. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    To make things easier, could you upload your project somewhere?
     
  10. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
  11. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    Hi,

    I've looked at your code, I couldn't compile it as there are dependencies to Assembly-CSharp-* projects, which are not included.

    Anyways, I made my own test and applied your code, you could have mentioned that the input in Unity isn't working, that's why you couldn't click on "Send Event to XAML" button, and that's why events were not being received.

    Short story is, because you moved AppCallbacks initialization from App.xaml.cs to PaginaInicial, some of the events needed by AppCallbacks were not being received, for ex., there's an event CoreWindow::Activated which is called during application initialization in App.xaml.cs.

    Here's a quick fix you can try

    Code (csharp):
    1.  
    2.                 appCallbacks.InitializeD3DXAML();
    3.                 appCallbacks.UnityActivate(null, CoreWindowActivationState.CodeActivated); // Add this line
    4.  
    This will emulate Activate event for AppCallbacks and input should work in Unity application.
     
  12. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi, it works!

    Thank you very much.

    Regards
     
  13. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi Tomas, I asking again.

    I have the previous scenario and all works fine, the new problem arise when I add a button to return from the game to PaginaInicial.xaml, I know a window store app uses their own system to navigate among pages (it use NavigationHelper.cs, ObservableDictionary.cs, RelayCommand.cs and SuspensionManeger.cs to manage this), the issue araise when I want to navigate from the page with the game to PaginaInicial.xaml using a xaml button that calls the following line: Frame.Navigate(typeof(PaginaInicial));
    also I tried with: Frame.goBack();
    but nothing works.

    Any ideas?

    Regards,
    Ricardo
    PD. Sorry for annoying you
     
  14. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    This is probably because MainPage is assigned to Window.Curent.Content instead of Frame class.
    Try creating a BlankApp from Visual Studio's template projects, and see how it's done.
     
  15. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    Hey again,

    I got interested in your way of initializing Unity, I prepared a simple example where you can freely switch between regular XAML page and XAML page with Unity, also fixed few issues along the way.

    Hope that helps you. I'll also include this example in the documentation for Windows Store Apps, so it would be beneficial for others.

    Cheers
     

    Attached Files:

    Elyes_TN likes this.
  16. bJam

    bJam

    Joined:
    Sep 4, 2013
    Posts:
    10
    Oh hey. Thank you, Tomas. I don't mean to hijack the thread or anything but this multiple pages example actually solved my issue with the file picker in my thread down below. So, as confirmation, this also works on WP8.1 =D Yay!
     
  17. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    Glad to hear that :)
     
    Elyes_TN and blqck like this.
  18. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Hi, Tomas, thank you very much.

    The example actually solved my issue, and I'm glad to hear that the example will be available for others.
    Just another question. How can I launch a game from another folder insted of Data?
    This because I want launch various games from a single xaml instance. i.e I would like have many games in others folders (not only in Data) and the app colud launched from these others folders.
     
    Elyes_TN likes this.
  19. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    carlosricardocm and Elyes_TN like this.
  20. Elyes_TN

    Elyes_TN

    Joined:
    Nov 5, 2014
    Posts:
    4
    really this post saved my life xD
     
    carlosricardocm likes this.
  21. carlosricardocm

    carlosricardocm

    Joined:
    Dec 26, 2014
    Posts:
    15
    Glad to hear that, Elyes :)
     
    Elyes_TN likes this.
  22. Elyes_TN

    Elyes_TN

    Joined:
    Nov 5, 2014
    Posts:
    4
    i wan't to deallocate the unity from Memory when i press back button , unity scene is paused ! and this cost a lot of memory for mobile applications , i had try : appCallbacks.Dispose();
    but this close all the application , anyone have an idea plz ?
     
  23. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,658
    Once Unity is loaded, you cannot unload, you can though load empty scene from Unity, which would deallocate memory.
     
    Elyes_TN likes this.