Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using Newtonsoft.Json.Linq;
- using NDream.AirConsole;
- public class GamestateExampleLogic : MonoBehaviour {
- Button[] gameStateButtons;
- void Awake(){
- //register all the events I need
- AirConsole.instance.onReady += OnReady;
- AirConsole.instance.onMessage += OnMessage;
- AirConsole.instance.onCustomDeviceStateChange += OnCustomDeviceStateChange;
- //no game state can be set until AirConsole is ready, so I disable the buttons until then
- gameStateButtons = FindObjectsOfType<Button>();
- for (int i = 0; i < gameStateButtons.Length; ++i) {
- gameStateButtons [i].interactable = false;
- }
- }
- void OnReady(string code){
- //Initialize Game State
- JObject newGameState = new JObject();
- newGameState.Add ("view", new JObject ());
- AirConsole.instance.SetCustomDeviceState(newGameState);
- //now that AirConsole is ready, the buttons can be enabled
- for (int i = 0; i < gameStateButtons.Length; ++i) {
- gameStateButtons [i].interactable = true;
- }
- }
- void OnMessage (int deviceId, JToken message){
- Debug.Log ("received message from device " + deviceId + ". content: " + message);
- }
- public void SetView(string viewName){
- //I set a new game state using the NewView helper function
- AirConsole.instance.SetCustomDeviceState (NewView (AirConsole.instance.GetCustomDeviceState (0), viewName));
- }
- void OnCustomDeviceStateChange(int from, JToken state){
- Debug.Log ("custom device update: " + state);
- }
- void OnDestroy() {
- //Unregister events
- if (AirConsole.instance != null) {
- AirConsole.instance.onReady -= OnReady;
- AirConsole.instance.onMessage -= OnMessage;
- AirConsole.instance.onCustomDeviceStateChange -= OnCustomDeviceStateChange;
- }
- }
- public static JToken NewView(JToken oldGameState, string sceneName){
- //this is a helper function to create the updated game state
- //this makes it easier to ensure that no info is lost when adding something new to the game state
- JObject newGameState = oldGameState as JObject;
- //in this case, the "view" property of the game state cannot be null because I always create it in OnReady
- if (newGameState ["view"] != null) {
- newGameState ["view"] = sceneName;
- } else {
- newGameState.Add ("view", sceneName);
- }
- Debug.Log ("returning new game state: " + newGameState);
- return newGameState;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement