1

我有一些与服务器共享的方法,但是当我在类中的函数中使用 www -> 它不运行时,要检查我是否像在类中使用的代码一样使用 www -> 它运行.. 我不知道发生了什么,请帮助我!

4

1 回答 1

1

创建一个继承 MonoBehaviour 的新类,然后调用它。

Javascript(unity) 方式示例 // 从时代广场的“星期五”外获取最新的网络摄像头 var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg"; function Start () { // 开始下载给定的 URL var www : WWW = new WWW (url);

    // Wait for download to complete
    yield www;

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

[编辑] C# 方式的示例

/// 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');   

[编辑]

基本上,Unity 中的 Javascript 文件自动继承自 MonoBehavior。

如果您绝对确定您不能/不会只创建一个继承 monobehaviour(示例 #1)的类来为您完成工作,那么您将询问如何对 url 执行正常的 XMLHttpRequest。

此处的 Javascript 示例:

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 响应解析为统一对象...

于 2012-05-23T01:51:22.080 回答