SendingFormController.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. www = new WWW(url, form.data, headers);
  46. }
  47. else
  48. {
  49. www = new WWW(url, null, headers);
  50. }
  51. yield return www;
  52. if (string.IsNullOrEmpty(www.error))
  53. {
  54. //Debug.Log("Received: " + www.text);
  55. callback(www.text, data);
  56. }
  57. else
  58. {
  59. Debug.Log("Error While Sending: " + www.error);
  60. //if(data["param"] != null) errorCallback(www, data["param"]);
  61. //else
  62. errorCallback(www);
  63. }
  64. }*/
  65. IEnumerator PostRequest(string url, string json)
  66. {
  67. var uwr = new UnityWebRequest(url, "POST");
  68. byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json);
  69. uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend);
  70. uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  71. uwr.SetRequestHeader("Content-Type", "application/json");
  72. //Send the request then wait here until it returns
  73. yield return uwr.SendWebRequest();
  74. if (uwr.isNetworkError)
  75. {
  76. Debug.Log("Error While Sending: " + uwr.error);
  77. }
  78. else
  79. {
  80. Debug.Log("Received: " + uwr.downloadHandler.text);
  81. }
  82. }
  83. // Web requests are typially done asynchronously, so Unity's web request system
  84. // returns a yield instruction while it waits for the response.
  85. //
  86. public static IEnumerator RequestRoutine(string url, Action<string> callback = null)
  87. {
  88. // Using the static constructor
  89. var request = UnityWebRequest.Get(url);
  90. // Wait for the response and then get our data
  91. yield return request.SendWebRequest();
  92. var data = request.downloadHandler.text;
  93. // This isn't required, but I prefer to pass in a callback so that I can
  94. // act on the response data outside of this function
  95. if (callback != null)
  96. callback(data);
  97. }
  98. // Callback to act on our response data
  99. private void ResponseCallback(string data)
  100. {
  101. Debug.Log(data);
  102. }
  103. /* public static IEnumerator LoadImage(Image img, System.Action<Texture2D> result)
  104. {
  105. WWW www = new WWW(url);
  106. yield return www;
  107. img.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
  108. }*/
  109. public static IEnumerator LoadImage(string url, GameObject plane, Vector3 pos = new Vector3(), Vector3 scale = new Vector3())
  110. {
  111. //string url = "https://example.com/image.jpeg";
  112. ////WWWForm form = new WWWForm();
  113. ////var headers = form.headers;
  114. ////headers["Access-Control-Allow-Credentials"] = "true";
  115. ////headers["Access-Control-Allow-Headers"] = "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time";
  116. ////headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS";
  117. //////headers["Access-Control-Allow-Origin"] = "http://test.prmsys.net/";
  118. ////headers["Content-type"] = "image/jpg, image/jpeg, image/png, image/bmp";
  119. using (UnityWebRequest img = UnityWebRequestTexture.GetTexture(url))
  120. {
  121. //Debug.Log(url);
  122. //var img = UnityWebRequestTexture.GetTexture(url); //, null, headers);
  123. img.SetRequestHeader("Access-Control-Allow-Credentials", "true");
  124. img.SetRequestHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time");
  125. img.SetRequestHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
  126. img.SetRequestHeader("Access-Control-Allow-Origin", "*");
  127. yield return img.SendWebRequest();
  128. if (img.isNetworkError || img.isHttpError)
  129. {
  130. Debug.Log(img.error);
  131. }
  132. else
  133. {
  134. //Texture2D texture = new Texture2D(1, 1);
  135. var texture = DownloadHandlerTexture.GetContent(img);
  136. //img.LoadImageIntoTexture(texture);
  137. Renderer renderer = plane.GetComponent<Renderer>();
  138. renderer.material.mainTexture = texture;
  139. if (scale.x == 0 && scale.z == 0)
  140. {
  141. plane.transform.localScale = new Vector3(texture.width / 1000f, 1, texture.height / 1000f);
  142. scale = plane.transform.localScale;
  143. }
  144. else plane.transform.localScale = scale;
  145. if (pos.x == 0 && pos.z == 0)
  146. plane.transform.position = new Vector3(scale.x * 5, 0, scale.z * 5);
  147. else plane.transform.position = pos;
  148. //GetComponent<Renderer>().material.mainTexture = img.texture;
  149. }
  150. }
  151. }
  152. /*public static IEnumerator LoadResources(string url_resources, string file, Action<Sprite> image = null, Action<WWW> errorCallback = null)
  153. {
  154. if (AuthorizationController.serverConfig != null)
  155. {
  156. //string url = "https://example.com/image.jpeg";
  157. WWWForm form = new WWWForm();
  158. //var headers = form.headers;
  159. //headers["Access-Control-Allow-Origin"] = "http://test.prmsys.net/areditor/";
  160. //headers["Access-Control-Allow-Credentials"] = "true";
  161. Debug.Log("proxy=" + AuthorizationController.serverConfig.proxy + url_resources + file);
  162. WWW img = new WWW(AuthorizationController.serverConfig.proxy + url_resources + file); //, null, headers);
  163. yield return img;
  164. if (String.IsNullOrEmpty(img.error))
  165. {
  166. var extension = System.IO.Path.GetExtension(file);
  167. //Debug.Log("extension: " + extension);
  168. //Debug.Log("Received: " + img.text);
  169. if (extension.Equals(".png")){
  170. Texture2D texture = new Texture2D(1, 1);
  171. img.LoadImageIntoTexture(texture);
  172. image(StagesResourcesController.ConvertTextureToSprite(texture, file));
  173. }
  174. if (extension.Equals(".svg"))
  175. {
  176. var tessOptions = new VectorUtils.TessellationOptions()
  177. {
  178. StepDistance = 100.0f,
  179. MaxCordDeviation = 0.5f,
  180. MaxTanAngleDeviation = 0.1f,
  181. SamplingStepSize = 0.01f
  182. };
  183. // Dynamically import the SVG data, and tessellate the resulting vector scene.
  184. var sceneInfo = SVGParser.ImportSVG(new StringReader(img.text));
  185. var geoms = VectorUtils.TessellateScene(sceneInfo.Scene, tessOptions);
  186. // Build a sprite with the tessellated geometry.
  187. var sprite = VectorUtils.BuildSprite(geoms, 50.0f, VectorUtils.Alignment.Center, Vector2.zero, 128, true);
  188. sprite.name = file;
  189. image(sprite);
  190. }
  191. }
  192. else
  193. {
  194. Debug.Log("Error While Sending: " + img.error);
  195. errorCallback(img);
  196. }
  197. //GetComponent<Renderer>().material.mainTexture = img.texture;
  198. }
  199. }*/
  200. }