'Unity/C#'에 해당되는 글 3건

Unity Web Client

Unity/C# 2018. 1. 18. 16:37



using UnityEngine;

using UnityEngine.Networking;

using System.Collections;

using System.Text;

using BestHTTP;



public class Test : MonoBehaviour

{

bool _isSend = false;


// Use this for initialization

void Start()

{

StartCoroutine(CoExe());

}


IEnumerator CoExe()

{

//*

Send("http://127.0.0.1:3000/addUser/test1", "{\"password\":\"123456\",\"name\":\"789\"}", "POST");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/addUser/test2", "{\"password\":\"123456\",\"name\":\"789\"}", "POST");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/list", "", "GET");

yield return new WaitWhile(() => _isSend);


Send("http://127.0.0.1:3000/deleteUser/test1", "", "DELETE");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/deleteUser/test2", "", "DELETE");

yield return new WaitWhile(() => _isSend);

Send("http://127.0.0.1:3000/list", "", "GET");


yield break;

/*/

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/addUser/test1", "{\"password\":\"123456\",\"name\":\"789\"}", "POST"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/addUser/test2", "{\"password\":\"123456\",\"name\":\"789\"}", "POST"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/list", "", "GET"));


yield return StartCoroutine(CoSend("http://127.0.0.1:3000/deleteUser/test1", "", "DELETE"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/deleteUser/test2", "", "DELETE"));

yield return StartCoroutine(CoSend("http://127.0.0.1:3000/list", "", "GET"));

//*/

}


void Send(string url, string body, string httpMethod)

{

_isSend = true;


HTTPMethods method = HTTPMethods.Get;

switch (httpMethod)

{

case "GET":

method = HTTPMethods.Get;

break;

case "POST":

method = HTTPMethods.Post;

break;

case "PUT":

method = HTTPMethods.Put;

break;

case "DELETE":

method = HTTPMethods.Delete;

break;

}


HTTPRequest request = new HTTPRequest(new System.Uri(url), method, Ack);

request.SetHeader("Content-Type", "application/json; charset=UTF-8");


request.RawData = Encoding.UTF8.GetBytes(body);

request.Send();

}


void Ack(HTTPRequest request, HTTPResponse response)

{

if (response != null)

{

Debug.LogError(request.Uri);

Debug.LogError(response.DataAsText);

}


_isSend = false;

}


IEnumerator CoSend(string url, string body, string httpMethod)

{

var request = new UnityWebRequest(url, httpMethod);

if (!string.IsNullOrEmpty(body))

{

byte[] bodyRaw = new UTF8Encoding().GetBytes(body);

request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);

}

request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

request.SetRequestHeader("Content-Type", "application/json; charset=UTF-8");


yield return request.Send();


Debug.LogError(url);

Debug.LogError(request.downloadHandler.text);


request.Dispose();

}

}




'Unity > C#' 카테고리의 다른 글

Debug 교체  (0) 2016.03.17
Partial 클래스 정의(C# 프로그래밍 가이드)  (0) 2011.06.17
블로그 이미지

영스파파

3D 세상을 만들기 위한 프로그래밍 정보들을 정리하는 공간

,

Debug 교체

Unity/C# 2016. 3. 17. 21:34


using UnityEngine;

using System;



//*

public static class Debug

{

public static bool isDebugBuild = false;


private static string hr = "\n\n-------------------------------------------------------------------------------";


public static void Log(object message)

{

if (isDebugBuild)

UnityEngine.Debug.Log(message + hr);


}

public static void Log(object message, UnityEngine.Object context)

{

if (isDebugBuild)

UnityEngine.Debug.Log(message, context);

}


public static void LogError(object message)

{

if (isDebugBuild)

UnityEngine.Debug.LogError(message);

}


public static void LogError(object message, UnityEngine.Object context)

{

if (isDebugBuild)

UnityEngine.Debug.LogError(message, context);

}


public static void LogWarning(object message)

{

if (isDebugBuild)

UnityEngine.Debug.LogWarning(message.ToString());

}


public static void LogWarning(object message, UnityEngine.Object context)

{

if (isDebugBuild)

UnityEngine.Debug.LogWarning(message.ToString(), context);

}


public static void Assert(bool condition)

{

if (isDebugBuild)

UnityEngine.Debug.Assert(condition);

}


public static void Assert(bool condition, object message)

{

if (isDebugBuild)

UnityEngine.Debug.Assert(condition, message);

}


public static void LogFormat(string format, params object[] args)

{

if (isDebugBuild)

UnityEngine.Debug.LogFormat(format, args);

}


public static void LogErrorFormat(string format, params object[] args)

{

if (isDebugBuild)

UnityEngine.Debug.LogErrorFormat(format, args);

}


public static void LogWarningFormat(string format, params object[] args)

{

if (isDebugBuild)

UnityEngine.Debug.LogWarningFormat(format, args);

}


public static void DrawLine(Vector3 start, Vector3 end, Color color = default(Color), float duration = 0.0f, bool depthTest = true)

{

if (isDebugBuild)

UnityEngine.Debug.DrawLine(start, end, color, duration, depthTest);

}


public static void DrawRay(Vector3 start, Vector3 dir, Color color = default(Color), float duration = 0.0f, bool depthTest = true)

{

if (isDebugBuild)

UnityEngine.Debug.DrawRay(start, dir, color, duration, depthTest);

}


public static void LogException(Exception e)

{

if (isDebugBuild)

UnityEngine.Debug.LogException(e);

}


public static void Break()

{

if (isDebugBuild)

UnityEngine.Debug.Break();

}

}

//*/



Debug.cs


'Unity > C#' 카테고리의 다른 글

Unity Web Client  (0) 2018.01.18
Partial 클래스 정의(C# 프로그래밍 가이드)  (0) 2011.06.17
블로그 이미지

영스파파

3D 세상을 만들기 위한 프로그래밍 정보들을 정리하는 공간

,

클래스나 구조체의 정의 또는 인터페이스를 두 개 이상의 소스 파일로 분할할 수 있습니다.

public partial class Employee
{
    public void DoWork()
    {
    }
}

public partial class Employee
{
    public void GoToLunch()
    {
    } 
} 


'Unity > C#' 카테고리의 다른 글

Unity Web Client  (0) 2018.01.18
Debug 교체  (0) 2016.03.17
블로그 이미지

영스파파

3D 세상을 만들기 위한 프로그래밍 정보들을 정리하는 공간

,