SendingFormController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using Assets.Scripts.Models;
  2. //using Newtonsoft.Json;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using Unity.VectorGraphics;
  9. using UnityEngine;
  10. using UnityEngine.Networking;
  11. using UnityEngine.SceneManagement;
  12. using UnityEngine.UI;
  13. public class SendingFormController : MonoBehaviour
  14. {
  15. static string uuid = "07a13c8907d1-6a7bdba1-a2fz-aatgu4j";
  16. public static string url; // = "http://dev.prmsys.net/api/areality/";
  17. //private static string proxy = "http://dev.prmsys.net/areditor/proxy.php?url=";
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. //var r = PostRequest("http://test.prmsys.net/api/auth/usersinfo", "vsheiko", "8*9Efc2%");
  22. //Debug.Log(r.ToString());
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. }
  28. public static IEnumerator PostRequest(string controller, string api, Dictionary<string, string> data, Action<string, Dictionary<string, string>> callback = null, Action<WWW> errorCallback = null)
  29. {
  30. WWWForm form = new WWWForm();
  31. //form.AddField("login", login);
  32. //form.AddField("password", password);
  33. // form.headers.Add("uuid-key", "07a13c8907d1-6a7bdba1-a2fz-aatgu4j");
  34. Dictionary<string, string> headers = new Dictionary<string, string>()
  35. {
  36. { "uuid-key", uuid }
  37. };
  38. var url = controller + api;
  39. Debug.Log("api=" + url);
  40. WWW www;
  41. if (data != null)
  42. {
  43. foreach (var d in data)
  44. form.AddField(d.Key, d.Value);
  45. UnityWebRequest uwr = UnityWebRequest.Post(url, form);
  46. www = new WWW(url, form.data, headers);
  47. }
  48. else
  49. {
  50. // UnityWebRequest uwr = UnityWebRequest.Post(url);
  51. www = new WWW(url, null, headers);
  52. }
  53. yield return www;
  54. if (String.IsNullOrEmpty(www.error))
  55. {
  56. //Debug.Log("Received: " + www.text);
  57. callback(www.text, data);
  58. }
  59. else
  60. {
  61. Debug.Log("Error While Sending: " + www.error);
  62. //if(data["param"] != null) errorCallback(www, data["param"]);
  63. //else
  64. errorCallback(www);
  65. }
  66. }
  67. IEnumerator PostRequest(string url, string json)
  68. {
  69. var uwr = new UnityWebRequest(url, "POST");
  70. byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
  71. uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
  72. uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  73. uwr.SetRequestHeader("Content-Type", "application/json");
  74. //Send the request then wait here until it returns
  75. yield return uwr.SendWebRequest();
  76. if (uwr.isNetworkError)
  77. {
  78. Debug.Log("Error While Sending: " + uwr.error);
  79. }
  80. else
  81. {
  82. Debug.Log("Received: " + uwr.downloadHandler.text);
  83. }
  84. }
  85. // Web requests are typially done asynchronously, so Unity's web request system
  86. // returns a yield instruction while it waits for the response.
  87. //
  88. public static IEnumerator RequestRoutine(string url, Action<string> callback = null)
  89. {
  90. // Using the static constructor
  91. var request = UnityWebRequest.Get(url);
  92. // Wait for the response and then get our data
  93. yield return request.SendWebRequest();
  94. var data = request.downloadHandler.text;
  95. // This isn't required, but I prefer to pass in a callback so that I can
  96. // act on the response data outside of this function
  97. if (callback != null)
  98. callback(data);
  99. }
  100. // Callback to act on our response data
  101. private void ResponseCallback(string data)
  102. {
  103. Debug.Log(data);
  104. }
  105. /* public static IEnumerator LoadImage(Image img, System.Action<Texture2D> result)
  106. {
  107. WWW www = new WWW(url);
  108. yield return www;
  109. img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
  110. }*/
  111. public static IEnumerator LoadImage(string url, GameObject plane)
  112. {
  113. //string url = "https://example.com/image.jpeg";
  114. WWWForm form = new WWWForm();
  115. var headers = form.headers;
  116. headers["Access-Control-Allow-Credentials"] = "true";
  117. headers["Access-Control-Allow-Headers"] = "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time";
  118. headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
  119. //headers["Access-Control-Allow-Origin"] = "http://test.prmsys.net/";
  120. headers["Content-type"] = "image/jpg, image/jpeg, image/png, image/bmp";
  121. var proxy = "http://dev.prmsys.net/areditor/proxy.php?url=";
  122. Debug.Log("proxy=" + url);
  123. WWW img = new WWW( url); //, null, headers);
  124. yield return img;
  125. Texture2D texture = new Texture2D(1, 1);
  126. img.LoadImageIntoTexture(texture);
  127. Renderer renderer = plane.GetComponent<Renderer>();
  128. renderer.material.mainTexture = texture;
  129. plane.transform.localScale = new Vector3(img.texture.width / 100f, 1, img.texture.height / 100f);
  130. var scale = plane.transform.localScale;
  131. plane.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
  132. //GetComponent<Renderer>().material.mainTexture = img.texture;
  133. }
  134. /*public static IEnumerator LoadResources(string url_resources, string file, Action<Sprite> image = null, Action<WWW> errorCallback = null)
  135. {
  136. if (AuthorizationController.serverConfig != null)
  137. {
  138. //string url = "https://example.com/image.jpeg";
  139. WWWForm form = new WWWForm();
  140. //var headers = form.headers;
  141. //headers["Access-Control-Allow-Origin"] = "http://test.prmsys.net/areditor/";
  142. //headers["Access-Control-Allow-Credentials"] = "true";
  143. Debug.Log("proxy=" + AuthorizationController.serverConfig.proxy + url_resources + file);
  144. WWW img = new WWW(AuthorizationController.serverConfig.proxy + url_resources + file); //, null, headers);
  145. yield return img;
  146. if (String.IsNullOrEmpty(img.error))
  147. {
  148. var extension = System.IO.Path.GetExtension(file);
  149. //Debug.Log("extension: " + extension);
  150. //Debug.Log("Received: " + img.text);
  151. if (extension.Equals(".png")){
  152. Texture2D texture = new Texture2D(1, 1);
  153. img.LoadImageIntoTexture(texture);
  154. image(StagesResourcesController.ConvertTextureToSprite(texture, file));
  155. }
  156. if (extension.Equals(".svg"))
  157. {
  158. var tessOptions = new VectorUtils.TessellationOptions()
  159. {
  160. StepDistance = 100.0f,
  161. MaxCordDeviation = 0.5f,
  162. MaxTanAngleDeviation = 0.1f,
  163. SamplingStepSize = 0.01f
  164. };
  165. // Dynamically import the SVG data, and tessellate the resulting vector scene.
  166. var sceneInfo = SVGParser.ImportSVG(new StringReader(img.text));
  167. var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
  168. // Build a sprite with the tessellated geometry.
  169. var sprite = VectorUtils.BuildSprite(geoms, 50.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
  170. sprite.name = file;
  171. image(sprite);
  172. }
  173. }
  174. else
  175. {
  176. Debug.Log("Error While Sending: " + img.error);
  177. errorCallback(img);
  178. }
  179. //GetComponent<Renderer>().material.mainTexture = img.texture;
  180. }
  181. }*/
  182. }