Last active
December 17, 2015 04:38
-
-
Save hirokim/711558060a6089359fda to your computer and use it in GitHub Desktop.
unityの通信処理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class NetworkConection : MonoBehaviour { | |
| private const string imageParamKey = "IMAGE_FILE"; | |
| private const string imageMimeType = "image/png"; | |
| public void SendAsyncTextRequest (string url, POSTParameter paramObj, Action<string, string> completed) { | |
| StartCoroutine(RequestText(url, paramObj, (www)=> { | |
| if (!string.IsNullOrEmpty(www.error)) { | |
| completed(null, www.error); | |
| return; | |
| } | |
| completed(www.text, null); | |
| })); | |
| } | |
| IEnumerator RequestText(string url, POSTParameter paramObj, Action<WWW> act) { | |
| WWWForm wwwForm = new WWWForm(); | |
| if (paramObj != null && | |
| paramObj.textParams != null) { | |
| foreach (string key in paramObj.textParams.Keys) { | |
| wwwForm.AddField(key, paramObj.textParams[key].ToString()); | |
| } | |
| } | |
| if (paramObj != null && | |
| paramObj.image != null && | |
| paramObj.image.Length > 0) { | |
| wwwForm.AddBinaryData(imageParamKey, paramObj.image, paramObj.imageName, imageMimeType); | |
| } | |
| using( WWW www = new WWW(url, wwwForm)) { | |
| while(!www.isDone && string.IsNullOrEmpty(www.error)) { | |
| yield return null; | |
| } | |
| if(act!=null) | |
| act(www); | |
| } | |
| } | |
| } | |
| public class POSTParameter { | |
| public Hashtable textParams {get; private set;} | |
| public string imageName {get; private set;} | |
| public byte[] image {get; private set;} | |
| public POSTParameter () { | |
| this.image = null; | |
| this.imageName = null; | |
| this.textParams = new Hashtable(); | |
| } | |
| public void addTextParam (string key, string val) { | |
| this.textParams.Add(key, val); | |
| } | |
| public void setImage (string fileName, byte[] image) { | |
| this.imageName = fileName; | |
| this.image = image; | |
| } | |
| public void setImage (string fileName, Texture2D image) { | |
| this.imageName = fileName; | |
| this.image = image.EncodeToPNG(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment