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)
//Button that launch the game
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!appCallbacks.IsInitialized())
{
//Here I change MainPage to launch the game withouth need of SplashScreen
var mainPage = new MainPage();
Window.Current.Content = mainPage;
Window.Current.Activate();
mainPage.RemoveSplashScreen();
mainPage.PositionImage();
// Setup scripting bridge
_bridge = new WinRTBridge.WinRTBridge();
appCallbacks.SetBridge(_bridge);
appCallbacks.SetSwapChainBackgroundPanel(mainPage.GetSwapChainBackgroundPanel());
Debug.WriteLine("La ventana actual es:"+Window.Current.ToString());
appCallbacks.SetCoreWindowEvents(Window.Current.CoreWindow);
appCallbacks.InitializeD3DXAML();
}
Window.Current.Activate();
}
*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
1899008–122397–App.xaml.cs (4.13 KB)
1899008–122398–MainPage.xaml.cs (5.7 KB)
1899008–122399–PaginaInicial.xaml.cs (2.46 KB)
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.
Hi, I have the most recent version. (4.6.1).
Regards,
Do you still execute - Communications.SetEvent(UnityToXAML); as the original sample did?
Hi, I execute Communications.SetEvent(UnityToXAML), as the original sample did.
private void OnInitialized()
{
Communications.SetEvent(UnityToXAML);
}
public void UnityToXAML(object arg)
{
UnityPlayer.AppCallbacks.Instance.InvokeOnUIThread(new UnityPlayer.AppCallbackItem(() =>
{
eventWasReceivedCount++;
textBox.Text = "Event received " + eventWasReceivedCount + " times";
}
), false);
}
And the code for the class Communications it’s the same as the original sample.
public delegate void UnityEvent(object arg);
public sealed class Communications
{
public static void SetCubeMaterialColor(byte r, byte g, byte b)
{
UnityEngine.GameObject go = UnityEngine.GameObject.Find("Cube");
UnityEngine.Material mat = go.GetComponent<XAMLConnection>().material;
mat.color = new UnityEngine.Color32(r, g, b, 255);
}
public static void SetEvent(UnityEvent e)
{
UnityEngine.GameObject go = UnityEngine.GameObject.Find("Cube");
if (go != null)
{
go.GetComponent<XAMLConnection>().onEvent = new XAMLConnection.OnEvent(e);
}
else
{
throw new Exception("Cube not found, have exported the correct scene?");
}
}
}
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
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.
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.
1909074–123156–Log.txt (39.8 KB)
Update: I notice that when I switch among windows (with command alt + tab) when I get back to the example all works fine again.
To make things easier, could you upload your project somewhere?
Hi, the project could be downloaded from
Regards,
Ricardo
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
appCallbacks.InitializeD3DXAML();
appCallbacks.UnityActivate(null, CoreWindowActivationState.CodeActivated); // Add this line
This will emulate Activate event for AppCallbacks and input should work in Unity application.
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
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.
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
1916004–123640–MultiplePages.zip (71.3 KB)
1 Like
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!
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.
1 Like
That’s not possible… that would mean that you need to have every data included in your solution…
Something relatively close probably would be a protocol launching, read about it here - Jerry Nixon @Work: Walkthrough: using Windows 8 Custom Protocol Activation
2 Likes
really this post saved my life xD
1 Like