Untitled

 avatar
unknown
csharp
2 years ago
2.1 kB
4
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Net.Http;
using System.Threading.Tasks;

[Serializable]
public class Star
{
    public double distance { get; set; }
    public int bodyCount { get; set; }
    public string name { get; set; }
    public Vector3 coords { get; set; }
    public bool coordsLocked { get; set; }
    public Information information { get; set; }
}

[Serializable]
public class Information
{
    public string allegiance { get; set; }
    public string government { get; set; }
    public string faction { get; set; }
    public string factionState { get; set; }
    public int population { get; set; }
    public string security { get; set; }
    public string economy { get; set; }
}

[System.Serializable]
public class StarArray
{
    [SerializeField]
    public Star[] starArray { get; set; }
}

public class GenerateData : MonoBehaviour
{
    public GameObject StarPrefab;

    // Start is called before the first frame update
    async void Start()
    {
        // Test Star for instantiation
        // GameObject star = (GameObject)Instantiate(StarPrefab, new Vector3(0, 0, 0), Quaternion.identity);
        // star.name = "Test Star";


        // Get star data from EDSM (https://www.edsm.net/en/api-v1)
        var url = "https://www.edsm.net/api-v1/sphere-systems?systemName=Colonia&radius=5&showCoordinates=1&showInformation=1";

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);

            HttpResponseMessage response = await client.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                string strResult = await response.Content.ReadAsStringAsync();
                strResult = "{\"starArray\":" + strResult + "}";
                Debug.Log(strResult);

                Star[] stars = (JsonUtility.FromJson<StarArray>(strResult)).starArray;

                foreach (Star star in stars)
                {
                    Debug.Log(star.information.population);
                }
            }
        }
    }
}
Editor is loading...