// Initializes the site with loaded JSON data. public void InitializeSite(SerializableSite siteJSON) { siteData = siteJSON; // Initialiez all data sets associated with this site. InitializeSiteElements(); }
// Loads site data from JSON and creates POIs out of it. public void LoadSites(string dataPath) { // If the data file isn't found, create a sample site so the system still shows SOMETHING. if (!File.Exists(dataPath)) { // Loads sample data of UCSD. SerializableSites sampleSites = new SerializableSites(); sampleSites.sites = new SerializableSite[1]; SerializableSite newSite = new SerializableSite(); // UC San Diego Lat/Lon as sample newSite.latitude = 32.8801f; newSite.longitude = 117.2340f; newSite.name = "UC San Diego"; newSite.description = "This site was generated as a sample of what the JSON file should look like, roughly"; sampleSites.sites[0] = newSite; string jsonText = JsonUtility.ToJson(sampleSites); // Save the JSON file as a sample. File.WriteAllText(dataPath, jsonText); // Return since there isn't any more we can do. return; } // Read the JSON from file. string jsonString = File.ReadAllText(dataPath); // Load in the JSON object as a Serializable Sites object. SerializableSites siteData = JsonUtility.FromJson <SerializableSites>(jsonString); // Load in custom JSON overrides and save if (File.Exists(GameManager.instance.caveSettings.pathToCustomOverrideFile)) { jsonString = File.ReadAllText(GameManager.instance.caveSettings.pathToCustomOverrideFile); customOverrides = JsonUtility.FromJson <SerializableElements>(jsonString); Debug.Log("loaded custom overrides"); Debug.Log(customOverrides.elements); } else { Debug.LogWarning(GameManager.instance.caveSettings.pathToCustomOverrideFile + " does not exist"); } // If the site data of the JSON isn't null and there are sites that are part of it. if (siteData.sites != null && siteData.sites.Length > 0) { // Iterate through each site from JSON. foreach (SerializableSite site in siteData.sites) { // Create an object for this site and set it's parent. GameObject newSiteObject = new GameObject(site.name); newSiteObject.transform.parent = this.transform; // Add the "Site" component to the new object. Site newSite = newSiteObject.AddComponent <Site>(); // Initialize the site by passing it it's JSON data. newSite.InitializeSite(site); // Add this site to the list. sites.Add(newSite); } } // If there were no sites in the JSON, print an error and return. else { Debug.LogErrorFormat("Error: No sites loaded. Please check the following file: {0}", GameManager.dataJsonFile); return; } Debug.LogFormat("Loaded {0} sites", sites.Count); // Create POIs from all the loaded sites. StartCoroutine(CreatePOIs()); }