참고: UNet은 지원이 중단되었으며 향후 Unity에서 삭제될 예정입니다. 현재 새로운 시스템이 개발 중입니다. 자세한 내용과 다음 단계는 이 블로그 포스트와 FAQ를 참조하십시오. |
HTML 양식으로 포맷된 서버에 데이터를 전송하기 위한 두 가지 기본 함수가 있습니다. WWW 시스템에서 마이그레이션하는 경우 아래의 WWWForm 사용을 참조하십시오.
양식 데이터를 지정하는 방식을 더 원활하게 조절하기 위해 UnityWebRequest
시스템에는 사용자가 구현 가능한 IMultipartFormSection
인터페이스가 포함되어 있습니다. 표준 애플리케이션의 경우 Unity는 데이터와 파일 섹션에 대한 디폴트 구현 MultipartFormDataSection
및 MultipartFormFileSection
도 제공합니다.
UnityWebRequest.POST
의 오버로드는 두 번째 파라미터로서 List 인수를 받아들입니다. 이 인수의 멤버는 모두 IMultipartFormSections
여야 합니다. 함수 시그니쳐는 다음과 같습니다.
WebRequest.Post(string url, List<IMultipartFormSection> formSections);
UnityWebRequest
를 생성하며, 타겟 URL을 첫 번째 문자열 파라미터로 설정합니다. 또한 UnityWebRequest
의 Content-Type 헤더를 IMultipartFormSection
오브젝트의 리스트에 지정되어 있는 양식 데이터에 적절하게 설정합니다.DownloadHandlerBuffer
를 UnityWebRequest
에 연결합니다. 이 작업은 편의를 위한 것으로, 서버의 응답을 확인하는 데 사용할 수 있습니다.WWWForm POST
함수와 마찬가지로, 이 HLAPI 함수는 제공된 각 IMultipartFormSection
을 순서대로 호출하여 RFC 2616에 지정된 표준 멀티파트 양식으로 포맷합니다.UploadHandlerRaw
오브젝트에 저장된 후 UnityWebRequest
에 연결됩니다. 그 결과 UnityWebRequest.POST
호출 후 IMultipartFormSection
오브젝트의 변경 사항은 서버에 전송된 데이터에 반영되지 않습니다.using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class MyBehavior : MonoBehaviour {
void Start() {
StartCoroutine(Upload());
}
IEnumerator Upload() {
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add( new MultipartFormDataSection("field1=foo&field2=bar") );
formData.Add( new MultipartFormFileSection("my file data", "myfile.txt") );
UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", formData);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}
}
WWW 시스템에서 마이그레이션할 수 있도록 UnityWebRequest 시스템은 WWWForm 오브젝트를 사용하여 양식 데이터를 제공할 수 있도록 허용합니다.
이 경우 함수 시그니쳐는 다음과 같습니다.
WebRequest.Post(string url, WWWForm formData);
UnityWebRequest
를 생성하고, 타겟 URL을 첫 문자열 인수의 값으로 설정합니다. 또한 WWWForm
인수(예: Content-Type)에 의해 생성된 커스텀 헤더를 읽고 UnityWebRequest
에 복사합니다.DownloadHandlerBuffer
를 UnityWebRequest
에 연결합니다. 이 작업은 편의를 위한 것으로, 서버의 응답을 확인하는 데 사용할 수 있습니다.WWWForm 오브젝트
에 의해 생성된 원시 데이터를 읽고 UnityWebRequest
에 연결된 UploadHandlerRaw
오브젝트에 버퍼링합니다. 따라서 UnityWebRequest.POST
호출 후 WWWForm
오브젝트를 변경해도 UnityWebRequest
의 콘텐츠가 변경되지 않습니다.using UnityEngine;
using System.Collections;
public class MyBehavior : public MonoBehaviour {
void Start() {
StartCoroutine(Upload());
}
IEnumerator Upload() {
WWWForm form = new WWWForm();
form.AddField("myField", "myData");
UnityWebRequest www = UnityWebRequest.Post("http://www.my-server.com/myform", form);
yield return www.SendWebRequest();
if(www.isNetworkError || www.isHttpError) {
Debug.Log(www.error);
}
else {
Debug.Log("Form upload complete!");
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.