2012-05-21 4 views
1

저는 서버와의 공동 작업에 몇 가지 방법이 있습니다.하지만 클래스의 함수에서 www를 사용하면 실행되지 않습니다. 클래스에서 사용하는 코드와 같이 www를 사용하는지 확인하십시오. > 실행 ... 무슨 일이 일어나는지 모르겠다. 제발 도와주세요!Unity3d의 클래스에서 www를 호출하십시오.

+0

는 당신이 우리가 검토에 대한 몇 가지 예제 코드를 제공 할 수

var xmlHttp = null; function GetServerInfo() { var Url = "http://localhost"; xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = ProcessRequest; xmlHttp.open("GET", Url, true); xmlHttp.send(null); } function ProcessRequest() { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { if (xmlHttp.responseText == "Not found") { } else { var info = eval ("(" + xmlHttp.responseText + ")"); //Here is where it will get super tricky. You will need to parse these objects into unity objects. } } } 

이의 정말 복잡한 부분은 화합의 객체로 당신의 HTTP 응답을 구문 분석 할 필요가있다 ...? – aukaost

+0

나는 set get 메소드로 모델을 정의하는 클래스를 가지고있다. 나는 set 메소드를 호출 할 때이 클래스에서 webservice를 호출하려고한다. 왜냐하면 모델 클래스는 monobhaviour의 서브 클래스가 아니기 때문에'WWW' 함수가 없습니다. –

답변

1

MonoBehaviour를 상속하는 새 클래스를 만든 다음 호출하십시오. 자바 스크립트 (연합) 방식의

예 // 최신 웹캠 샷을 가져 외부 "금요일"타임 스퀘어 (Times Square) VAR URL에 = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg" ; function Start() { // 주어진 URL 다운로드 시작 var www : WWW = 새로운 WWW (url);

/// Gets the response. 
/// 
/// <param name="StrURL">The URL. 
/// HTML source 
public string GetResponse(string StrURL) 
{ 
    string strReturn = ""; 
    HttpWebRequest objRequest = null; 
    IAsyncResult ar = null; 
    HttpWebResponse objResponse = null; 
    StreamReader objs = null; 
    try 
    { 

     objRequest = (HttpWebRequest)WebRequest.Create(StrURL); 
     ar = objRequest.BeginGetResponse(new AsyncCallback(GetScrapingResponse),  objRequest); 

     //// Wait for request to complete 
     ar.AsyncWaitHandle.WaitOne(1000 * 60, true); 
     if (objRequest.HaveResponse == false) 
     { 
      throw new Exception("No Response!!!"); 
     } 

     objResponse = (HttpWebResponse)objRequest.EndGetResponse(ar); 
     objs = new StreamReader(objResponse.GetResponseStream()); 
     strReturn = objs.ReadToEnd(); 
    } 
    catch (Exception exp) 
    { 
     throw exp; 
    } 
    finally 
    { 
     if (objResponse != null) 
     objResponse.Close(); 
     objRequest = null; 
     ar = null; 
     objResponse = null; 
     objs = null; 
    } 
    return strReturn; 
} 

/// Gets the scraping response. 
/// 
/// <param name="result">The result. 
protected void GetScrapingResponse(IAsyncResult result) 
{ 
    //here you will need to cast/parse the response into what ever type you require e.g. a texture, an xml file, an asset bundle, ect. 
} 

전화 그래서 유니티에서

GetResponse('http://images.earthcam.com/ec_metros/ourcams/fridays.jpg'); 

[편집]

Basicly 자바 스크립트 파일이 자동으로 MonoBehavior에서 상속과 같은 C#을 방법의

// Wait for download to complete 
    yield www; 

    // assign texture 
    renderer.material.mainTexture = www.texture; 
} 

[편집] 예.

절대적으로 확실하다면 monobehaviour (예 1)를 상속받은 클래스를 만들어서 작업을 수행 한 다음 URL에 일반 XMLHttpRequest를 수행하는 방법을 묻는 것입니다. 여기

자바 스크립트 예 :

+0

정말 감사하지만, 모델 클래스에서 요청을하고 싶습니다. [그것은 monobhaviour가 아닙니다] 보통 클래스입니다. 너에게 몇 가지 아이디어를 줄 수 있니? –

+0

예 : –

+0

감사합니다. 열정을 가져 주셔서 감사합니다. js 스크립트로 HttpWebResponse를 사용할 수 있습니까? 왜냐하면 나는 js 스크립트에서 HttpWebResponse를 호출하려고했으나 성공하지 못했습니다. [이미 system.net을 가져 왔습니다.] u는 js 스크립트에서 호출 할 수 있습니다. –

관련 문제