//// Copyright | Raumgleiter AG | 2018 | All Rights Reserved ////*********************************************************************************************************************** ////Headers //using System.Collections; //using System.Collections.Generic; //using UnityEngine; //using UnityEngine.Networking; //using System.IO; //using SimpleJSON; //using UnityEngine.UI; //using System.Text; //using System; //namespace Raumgleiter.RGDKDevLab //{ // public class WebRequestExample3 : MonoBehaviour // { // string matManufacturers; // private string mtexturUrl = "https://staging.mtextur.ch/api/v1/"; // string mtexturApiKey = mtexApiKey.mtexturApiKey; // public Dropdown manDd, matDd; // public Text selectedManufacturer, manufacturerDescription, materialInfo; // public Button start, matButton, manButton; // // public Image preview; // public RawImage Rimage; // public GameObject cube; // public Material newMaterialRef; // public int m_DropdownValue; // public string m_Message; // private string mm_Message; // string manufacturers; // string result; // public List materialList; // private IEnumerator coroutine; // ManufacturerInfo manufacturerInfo = new ManufacturerInfo(); // void Start() // { // start.onClick.AddListener(UpdateManList); // manButton.onClick.AddListener(UpdateManufacturer); // matButton.onClick.AddListener(UpdateMaterial); // } // void Update() // { // /* // GetManufacturerFromDropdown(); // // update if selected from dropdown // if (mm_Message != m_Message && m_Message != "-- select manufacturer --") // { // mm_Message = m_Message; // coroutine = GetAllMaterials(); // StartCoroutine(coroutine); // materialInfo.text = "material description will be here"; // Debug.Log("changed"); // } // // File.WriteAllText(Application.dataPath + "/saveFile.json", matManufacturers); // // Debug.Log(Application.dataPath + "/saveFile.json"); // */ // } // private void UpdateMaterial() // { // Debug.Log(matDd.options[matDd.value].text); // Debug.Log(matDd.value); // Debug.Log(materialList[matDd.value].id); // coroutine = GetMaterial(); // StartCoroutine(coroutine); // } // private void UpdateManufacturer() // { // m_DropdownValue = manDd.value; // m_Message = manDd.options[m_DropdownValue].text; // selectedManufacturer.text = m_Message; // coroutine = GetAllMaterials(); // StartCoroutine(coroutine); // } // string GetManufacturerUrl() // { // string url; // return url = mtexturUrl + "manufacturers/" + manufacturerInfo.id; ; // } // private void GetManufacturerDescription(string matManufacturers) // { // var json = JSON.Parse(matManufacturers); // manufacturerDescription.text = json[m_DropdownValue - 1].ToString(); // manufacturerInfo = new ManufacturerInfo(json[m_DropdownValue - 1]["id"], json[m_DropdownValue - 1]["name"], json[m_DropdownValue - 1]["shortname"], json[m_DropdownValue - 1]["url"], json[m_DropdownValue - 1]["updated_at"]); // } // private void GetManufacturerFromDropdown() // { // m_DropdownValue = manDd.value; // m_Message = manDd.options[m_DropdownValue].text; // selectedManufacturer.text = m_Message; // } // void UpdateManList() // { // coroutine = GetAllManufacturers(); // StartCoroutine(coroutine); // } // public IEnumerator GetAllManufacturers() // { // yield return GetJsonLikeString(mtexturUrl + "manufacturers/"); // // yield return new WaitForSeconds(1); // Debug.Log(result); // matManufacturers = result; // PopulateDd(GetManufacturerList(result), manDd); // StopCoroutine(coroutine); // } // public IEnumerator GetAllMaterials() // { // matDd.ClearOptions(); // GetManufacturerDescription(matManufacturers); // string url = GetManufacturerUrl(); // yield return GetJsonLikeString(url); // // Json like string to list of objects // var materialList = MaterialList(result); // // list of names to populate dropdown // var matList = new List(); // Debug.Log("matlist lenght :" + matList.Count); // foreach (var entry in materialList) // { // matList.Add(entry.name); // } // matDd.AddOptions(matList); // StopCoroutine(coroutine); // } // private IEnumerator GetMaterial() // { // string url = mtexturUrl + "materials/" + materialList[matDd.value].id; // yield return GetJsonLikeString(url); // var materialInfoString = result; // materialInfo.text = materialInfoString; // var json = JSON.Parse(materialInfoString); // string imageUrl = "https://www.mtextur.com" + json["image"]["urls"]["medium"]; // Debug.Log(json["image"]["urls"]["medium"]); // StartCoroutine(GetTexture(imageUrl)); // } // IEnumerator GetTexture(string someUrl) // { // Debug.Log(someUrl); // UnityWebRequest www = UnityWebRequestTexture.GetTexture(someUrl); // // www.SetRequestHeader("X-Api-Key", mtexturApiKey); // yield return www.SendWebRequest(); // if (www.isNetworkError || www.isHttpError) // { // Debug.Log(www.error); // } // else // { // Texture myTexture = ((DownloadHandlerTexture)www.downloadHandler).texture; // // preview = myTexture; // Rimage.texture = myTexture; // newMaterialRef.mainTexture = myTexture; // } // } // public IEnumerator GetJsonLikeString(string someUrl) // { // using (UnityWebRequest webRequest = UnityWebRequest.Get(someUrl)) // { // Debug.Log(someUrl); // //Add Headers // webRequest.SetRequestHeader("X-Api-Key", mtexturApiKey); // yield return webRequest.SendWebRequest(); // if (webRequest.isNetworkError) // { // Debug.Log(": Error: " + webRequest.error); // } // else // { // result = webRequest.downloadHandler.text; // Debug.Log(result); // } // } // } // public void PopulateDd(List someList, Dropdown dd) // { // dd.AddOptions(someList); // } // public List GetManufacturerList(string manufacturersJsonLike) // { // List manufacturersList = new List(); // // string json = JsonUtility.ToJson(matManufacturers); // var json = JSON.Parse(manufacturersJsonLike); // for (int i = 0; i < (json.Count); i++) // { // manufacturersList.Add(json[i]["name"]); // } // return manufacturersList; // } // public List MaterialList(string someJsonLikeString) // { // var json = JSON.Parse(someJsonLikeString); // for (int i = 0; i < (json["materials"].Count); i++) // { // materialList.Add(new MaterialBasicInfo(json["materials"][i]["id"], json["materials"][i]["name"], i)); // } // return materialList; // } // } // /* // [Serializable] // public class MaterialBasicInfo // { // public int id { get; set; } // public string name { get; set; } // public int idx { get; set; } // public MaterialBasicInfo(int id, string name, int idx) // { // this.id = id; // this.name = name; // this.idx = idx; // } // } // [Serializable] // public class ManufacturerInfo // { // public int id { get; set; } // public string name { get; set; } // public string shortname { get; set; } // public string url { get; set; } // public string updated_at { get; set; } // public ManufacturerInfo() // { // } // public ManufacturerInfo(int id, string name, string shortname, string url, string updated_at) // { // this.id = id; // this.name = name; // this.shortname = shortname; // this.url = url; // this.updated_at = updated_at; // } // } // */ //}