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 SceneController() { JSONNode.VectorContainerType = JSONContainerType.Object; JSONNode.QuaternionContainerType = JSONContainerType.Object; } 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; string name = sb.GetComponent().name; if (!sceneDict.ContainsKey(name)) { jsonObject = new JSONObject(); jsonObject.Add("name", name); jsonObject.Add("is_a", new JSONArray()); jsonObject.Add("components", new JSONObject()); sceneDict[name] = jsonObject; sceneArray.Add(sceneDict[name]); } jsonObject = sceneDict[name]; jsonObject["is_a"].Add(sb.is_a); switch (sb) { case Entity entity: Handle(jsonObject, entity); break; default: Debug.LogWarning($"No Handler for SemanticBehaviour \"{sb.GetType().Name}\""); break; } } return sceneArray; } private void Handle(JSONObject jsonObject, Entity entity) { JSONObject transform = new JSONObject(); transform["position"] = entity.transform.position; transform["orientation"] = entity.transform.rotation; jsonObject["components"]["transform"] = transform; } } }