// Convert all hotspots in the Hotspots game object into a JSON representation. public void SaveJson() { var hotspotScene = new HotspotScene(); hotspotScene.hotspots = new HotspotSaveable[Controller.transform.childCount]; //Save position of each hotspot. //The position is calculated in pixels from the leftmost surface. for (int i = 0; i < hotspotScene.hotspots.Length; i++) { var child = Controller.transform.GetChild(i); Vector3 screenPos = immersiveCamera.cameras[0].WorldToScreenPoint(child.position); HotspotSaveable hotspot = new HotspotSaveable() { position = new Vector2(screenPos.x, screenPos.y), name = child.name }; hotspotScene.hotspots[i] = hotspot; } string json = JsonUtility.ToJson(hotspotScene); print(json); File.WriteAllText(Application.persistentDataPath + "/hotspots2.json", json); #if UNITY_EDITOR UnityEditor.AssetDatabase.Refresh(); #endif }
private void LoadFromJSON() { //Load and Parse JSON string json = File.ReadAllText(Application.persistentDataPath + "/hotspots2.json"); print(json); HotspotScene hs = JsonUtility.FromJson <HotspotScene>(json); GameObject hotspotHolder = GameObject.Find("Hotspots"); if (hotspotHolder == null) { hotspotHolder = new GameObject(); hotspotHolder.name = "Hotspots"; } foreach (HotspotSaveable hotspot in hs.hotspots) { //Instantiate Hotspot var hotspotObject = Instantiate(hotspotPrefab); hotspotObject.transform.parent = hotspotHolder.transform; // Works out which camera is associated with the surface hotspot appears on. // Also provides the position of the hotspot on that surface in pixels. var(cam, pos) = immersiveCamera.FindCameraFromScreenPosition(hotspot.position); if (cam == null) { return; } //Make hotspot face camera HotspotScript hotScript = hotspotObject.GetComponent <HotspotScript>(); // Cast ray through screen point and place hotspot 2 units from the camera. Ray ray = cam.ScreenPointToRay(new Vector3(pos.x, pos.y)); hotspotObject.transform.position = ray.GetPoint(2); } }