using UnityEngine; using SimpleJSON; using System.Collections.Generic; namespace dlu { public class SceneController : MonoBehaviour { public string instructionJson = "instruction.json"; public string contextJson = "context.json"; [HideInInspector] public SemanticBehaviour[] semanticBehaviours; public void Start() { if (semanticBehaviours == null) { FindSemanticLabels(); } // string jsonContent = Resources.Load(instructionJson).text; // JSONArray scene = JSON.Parse(jsonContent) as JSONArray; // foreach (JSONObject obj in scene) // { // SetGameObjectState(obj); // } } // private void SetGameObjectState(JSONObject sceneObject) // { // string name = sceneObject["name"]; // // Debug.Log($"name {name}"); // GameObject go = GameObject.Find($"Objects/{name}"); // if (go != null) { // // Debug.Log(go); // } // } public void FindSemanticLabels() { this.semanticBehaviours = GameObject.FindObjectsOfType(); } public JSONArray GenerateContextJSON() { if (this.semanticBehaviours == null) { FindSemanticLabels(); } Dictionary sceneDict = new Dictionary(); JSONArray sceneArray = new JSONArray(); foreach (SemanticBehaviour sb in this.semanticBehaviours) { JSONObject jsonObject = null; if (!sceneDict.ContainsKey(sb.name)) { sceneDict[sb.name] = new JSONObject(); sceneDict[sb.name].Add("is_a", new JSONArray()); sceneArray.Add(sceneDict[sb.name]); } jsonObject = sceneDict[sb.name]; jsonObject["is_a"].Add(sb.is_a); switch (sb) { case InstanceIdentifier identifier: HandleInstanceIdentifier(jsonObject, identifier); break; default: Debug.LogWarning("Trying to add SemanticBehaviour to JSON, but no case exists.", sb); break; } } return sceneArray; } private void HandleInstanceIdentifier(JSONObject jsonObject, InstanceIdentifier identifier) { jsonObject["name"] = identifier.name; } } }