Advertisement
Guest User

GamestateExampleLogic

a guest
Jul 9th, 2018
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Newtonsoft.Json.Linq;
  6. using NDream.AirConsole;
  7.  
  8. public class GamestateExampleLogic : MonoBehaviour {
  9.  
  10.     Button[] gameStateButtons;
  11.  
  12.     void Awake(){
  13.         //register all the events I need
  14.         AirConsole.instance.onReady += OnReady;
  15.         AirConsole.instance.onMessage += OnMessage;
  16.         AirConsole.instance.onCustomDeviceStateChange += OnCustomDeviceStateChange;
  17.  
  18.         //no game state can be set until AirConsole is ready, so I disable the buttons until then
  19.         gameStateButtons = FindObjectsOfType<Button>();
  20.         for (int i = 0; i < gameStateButtons.Length; ++i) {
  21.             gameStateButtons [i].interactable = false;
  22.         }
  23.     }
  24.  
  25.     void OnReady(string code){
  26.         //Initialize Game State
  27.         JObject newGameState = new JObject();
  28.         newGameState.Add ("view", new JObject ());
  29.  
  30.         AirConsole.instance.SetCustomDeviceState(newGameState);
  31.  
  32.         //now that AirConsole is ready, the buttons can be enabled
  33.         for (int i = 0; i < gameStateButtons.Length; ++i) {
  34.             gameStateButtons [i].interactable = true;
  35.         }
  36.     }
  37.  
  38.     void OnMessage (int deviceId, JToken message){
  39.         Debug.Log ("received message from device " + deviceId + ". content: " + message);
  40.     }
  41.  
  42.     public void SetView(string viewName){
  43.         //I set a new game state using the NewView helper function
  44.         AirConsole.instance.SetCustomDeviceState (NewView (AirConsole.instance.GetCustomDeviceState (0), viewName));
  45.     }
  46.  
  47.     void OnCustomDeviceStateChange(int from, JToken state){
  48.         Debug.Log ("custom device update: " + state);
  49.     }
  50.  
  51.     void OnDestroy() {
  52.  
  53.         //Unregister events
  54.         if (AirConsole.instance != null) {
  55.             AirConsole.instance.onReady -= OnReady;
  56.             AirConsole.instance.onMessage -= OnMessage;
  57.             AirConsole.instance.onCustomDeviceStateChange -= OnCustomDeviceStateChange;
  58.         }
  59.     }
  60.  
  61.     public static JToken NewView(JToken oldGameState, string sceneName){
  62.         //this is a helper function to create the updated game state
  63.         //this makes it easier to ensure that no info is lost when adding something new to the game state
  64.         JObject newGameState = oldGameState as JObject;
  65.  
  66.         //in this case, the "view" property of the game state cannot be null because I always create it in OnReady
  67.         if (newGameState ["view"] != null) {
  68.             newGameState ["view"] = sceneName;
  69.         } else {
  70.             newGameState.Add ("view", sceneName);
  71.         }
  72.  
  73.         Debug.Log ("returning new game state: " + newGameState);
  74.         return newGameState;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement