SendingFormController.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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, Vector3 pos = new Vector3(), Vector3 scale = new Vector3())
  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. using (UnityWebRequest img = UnityWebRequestTexture.GetTexture(url))
  122. {
  123. //Debug.Log(url);
  124. //var img = UnityWebRequestTexture.GetTexture(url); //, null, headers);
  125. yield return img.SendWebRequest();
  126. if (img.isNetworkError || img.isHttpError)
  127. {
  128. Debug.Log(img.error);
  129. }
  130. else
  131. {
  132. //Texture2D texture = new Texture2D(1, 1);
  133. var texture = DownloadHandlerTexture.GetContent(img);
  134. //img.LoadImageIntoTexture(texture);
  135. Renderer renderer = plane.GetComponent<Renderer>();
  136. renderer.material.mainTexture = texture;
  137. if (scale.x == 0 && scale.z == 0)
  138. {
  139. plane.transform.localScale = new Vector3(texture.width / 1000f, 1, texture.height / 1000f);
  140. scale = plane.transform.localScale;
  141. }
  142. else plane.transform.localScale = scale;
  143. if (pos.x == 0 && pos.z == 0)
  144. plane.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
  145. else plane.transform.position = pos;
  146. //GetComponent<Renderer>().material.mainTexture = img.texture;
  147. }
  148. }
  149. }
  150. /*public static IEnumerator LoadResources(string url_resources, string file, Action<Sprite> image = null, Action<WWW> errorCallback = null)
  151. {
  152. if (AuthorizationController.serverConfig != null)
  153. {
  154. //string url = "https://example.com/image.jpeg";
  155. WWWForm form = new WWWForm();
  156. //var headers = form.headers;
  157. //headers["Access-Control-Allow-Origin"] = "http://test.prmsys.net/areditor/";
  158. //headers["Access-Control-Allow-Credentials"] = "true";
  159. Debug.Log("proxy=" + AuthorizationController.serverConfig.proxy + url_resources + file);
  160. WWW img = new WWW(AuthorizationController.serverConfig.proxy + url_resources + file); //, null, headers);
  161. yield return img;
  162. if (String.IsNullOrEmpty(img.error))
  163. {
  164. var extension = System.IO.Path.GetExtension(file);
  165. //Debug.Log("extension: " + extension);
  166. //Debug.Log("Received: " + img.text);
  167. if (extension.Equals(".png")){
  168. Texture2D texture = new Texture2D(1, 1);
  169. img.LoadImageIntoTexture(texture);
  170. image(StagesResourcesController.ConvertTextureToSprite(texture, file));
  171. }
  172. if (extension.Equals(".svg"))
  173. {
  174. var tessOptions = new VectorUtils.TessellationOptions()
  175. {
  176. StepDistance = 100.0f,
  177. MaxCordDeviation = 0.5f,
  178. MaxTanAngleDeviation = 0.1f,
  179. SamplingStepSize = 0.01f
  180. };
  181. // Dynamically import the SVG data, and tessellate the resulting vector scene.
  182. var sceneInfo = SVGParser.ImportSVG(new StringReader(img.text));
  183. var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
  184. // Build a sprite with the tessellated geometry.
  185. var sprite = VectorUtils.BuildSprite(geoms, 50.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
  186. sprite.name = file;
  187. image(sprite);
  188. }
  189. }
  190. else
  191. {
  192. Debug.Log("Error While Sending: " + img.error);
  193. errorCallback(img);
  194. }
  195. //GetComponent<Renderer>().material.mainTexture = img.texture;
  196. }
  197. }*/
  198. }