SendingFormController.cs 8.9 KB

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