Search code examples
c#windows-phone-8unity-game-enginewindows-store-apps

How to check a bool or call a function in script of Unity on Windows App Solution side


I need to call a function on Windows Store app side by checking a boolean from unity script. How to do that i used this code but it gives errors. I just need a simple solution in which i check a bool declared in unity script to call a function at ported windows store app end

using System;
using UnityEngine;

public class SphereScript : MonoBehaviour
{
private bool m_IsMoving = true;
private bool m_IsMovingLeft = false;

public Camera GameCamera;
public event Action<bool> SphereStateChanged;
public bool IsSphereMoving { get { return m_IsMoving; } }

void Start()
{
    if (GameCamera == null)
    {
        throw new Exception("Camera is not attached to the sphere script!");
    }
}

void FixedUpdate()
{
    if (!m_IsMoving)
    {
        return;
    }

    if (m_IsMovingLeft)
    {
        transform.position -= new Vector3(0.2f, 0.0f);

        if (GameCamera.WorldToScreenPoint(transform.position).x < 100.0f)
        {
            m_IsMovingLeft = false;
        }
    }
    else
    {
        transform.position += new Vector3(0.2f, 0.0f);

        if (GameCamera.WorldToScreenPoint(transform.position).x > Screen.width - 100.0f)
        {
            m_IsMovingLeft = true;
        }
    }
}

void OnGUI()
{
    var buttonText = m_IsMoving ? "Stop sphere" : "Start sphere movement";

    if (GUI.Button(new Rect(0, 0, Screen.width, 40), buttonText))
    {
        m_IsMoving = !m_IsMoving;

        if (SphereStateChanged != null)
        {
            SphereStateChanged(m_IsMoving);
        }
    }
}
}

Complete code is here


Solution

  • The way I understand you question is that when something happens on the Unity side, you'd like to call a method on the Windows Store app side, right?

    A nice way to do this is to have an event in your Unity code that your Win code can register for. For the sake of this example I'm going to call your event ButtonClicked.

    First you need a static class that the event will be in. Create a new C# script in the Unity Assets folder and open it up. I called mine StaticInterop. Clear the code that was generated, and make it this:

    public class StaticInterop
    {
      public static event EventHandler ButtonClicked;
    
      public static void FireButtonClicked()
      {
        if (ButtonClicked != null)
        {
          ButtonClicked(null, null);
        }
      }
    }
    

    Now, in Unity whenever that thing happens (in this case when the button is clicked), do this line of code:

    StaticInterop.FireButtonClicked();
    

    That is everything on the Unity side done. So create a build, and open up the VS Windows Store app project it creates.

    In the Win code you can now sign up for the event like this:

    StaticInterop.ButtonClicked+= StaticInterop_ButtonClicked;
    

    And declare this method for it to run:

    void StaticInterop_ButtonClicked(object sender, EventArgs e)
    {
    // Do whatever you need here.
    }
    

    The important thing to note is that the StaticInterop static class is showing up in your Win code. This means that you can use it to transfer anything between the Unity and Win side. You could even have a method in the class called something like PauseGame(), and then run that from the Win side with StaticInterop.PauseGame().