Unity與服務區交互數據


Unity與服務區交互數據

  Unity可能在用的時候使用到登陸等需要與服務器交互數據。今天嘗試使用了WWW類和WWWForm類來實現Get請求與Post請求。

1.WWW

  Unity聖典解釋:

  WWW會返回一個新的WWW對象。當它被下載,其結果可以從返回對象中獲取。這個函數創建和發送一個GET請求,流將自動開始下載響應。流創建之后,你必須等待它完成,然而可以訪問已下載的數據。作為一個方面的流可以被中斷,因此你可以容易的告訴Unity等待下載完成。你可以檢查isDone屬性來查看是否已經下載完成,或者yield自動等待下載物體,直到它被下載完成(不會影響游戲的其余部分)。

Variables變量

  • Returns the contents of the fetched web page as a string (Read Only).
    通過網頁獲取並以字符串的形式返回內容(只讀)。
  • Returns the contents of the fetched web page as a byte array (Read Only).
    以字節組的形式返回獲取到的網絡頁面中的內容(只讀)。
  • Returns an error message if there was an error during the download (Read Only).
    返回一個錯誤消息,在下載期間如果產生了一個錯誤的話。(只讀)
  • Returns a Texture2D generated from the downloaded data (Read Only).
    從下載的數據返回生成的一個Texture2D(只讀)。
  • Returns a AudioClip generated from the downloaded data (Read Only).
    從下載的數據,返回一個AudioClip。(只讀)
  • Returns a MovieTexture generated from the downloaded data (Read Only).
    從下載的數據,返回一個MovieTexture(只讀)。
  • Is the download already finished? (Read Only)
    判斷下載是否已經完成(只讀)?
  • How far has the download progressed (Read Only).
    下載進度有多少(只讀)?
  • How far has the upload progressed (Read Only).
    上傳進度有多少(只讀)
  • Load an Ogg Vorbis file into the audio clip.
    加載一個Ogg Vorbis文件到音頻剪輯。
  • url
    The URL of this WWW request (Read Only).
    該WWW請求的URL(只讀)。
  • Streams an AssetBundle that can contain any kind of asset from the project folder.
    AssetBundle的數據流,可以包含項目文件夾中的任何類型資源。
  • Priority of AssetBundle decompression thread.
    AssetBundle解壓縮線程的優先級。

Constructors構造器

  • WWW
    Creates a WWW request with the given URL.
    用給定的URL創建一個WWW請求。

Functions函數

  • Returns a AudioClip generated from the downloaded data (Read Only).
    從下載數據,返回一個AudioClip(只讀)。
  • Replaces the contents of an existing Texture2D with an image from the downloaded data.
    利用一個從下載數據中的圖像來替換現有Texture2D
  • Loads the new web player data file.
    加載新的web播放器數據文件。

Class Functions類函數

    • Encodes string into an URL-friendly format.
      字符串編碼成一個URL的格式。
    • Decodes string from an URL-friendly format.
      從一個URL格式解碼字符串。
    • Loads an assetBundle from the cache, or downloads it, in case it is not cached.
      從緩存加載一個資源包,如果沒有被緩存,或從下載加載。 

2.WWWForm

  輔助類。用來生成表單數據,使用WWW類傳遞到web服務器。

Variables變量

  • (Read Only) Returns the correct request headers for posting the form using the WWW class.
    (只讀)為使用WWW類傳遞的表單返回一個正確的請求頭。
  • (Read Only) The raw data to pass as the POST request body when sending the form.
    (只讀)在發送表單的時,原始數據作為POST請求發送。

Constructors構造器

  • Creates an empty WWWForm object.
    創建一個空的網頁表單對象。

Functions函數

    • Add a simple field to the form.
      添加一個簡單的域到表單。
    • Add binary data to the form.
      添加二進制數據到表單。 

3.使用方法

  這里試着從網上下載一個圖片替換物體的貼圖。

using System;
using UnityEngine;
using System.Collections;using System.Net;public class WWWZ : MonoBehaviour {void Start()
    {string path= "http://a3.att.hudong.com/72/37/01200000194734134393377005543_s.jpg";
        StartCoroutine(GoForm());
    }
    IEnumerator GoStart()
    {
        WWW www = new WWW(path);
        yield return www;

        if (www.isDone)
        {
            GetComponent<Renderer>().material.mainTexture = www.texture;
        }
    }
}

 

接下來是想URL傳遞表單數據。

using System;
using UnityEngine;
using System.Collections;
using System.Net;
using System.Text;
using LitJson;

public class WWWZ : MonoBehaviour {

    public string url = "http://192.168.16.118:8080/google";//接受表單的地址


    void Start()
    {

        form = new WWWForm();

        form.AddField("action","login");
        form.AddField("username","abc");
        form.AddField("password", "123");

        StartCoroutine(GoForm());

        
    }
 

    IEnumerator GoForm()
    {
        WWW w=new WWW(url, form);
        yield return w;

        if (w.error != null)
            print(w.error);
        else
            Debug.Log(“提交成功!”);

    }
}

當然在可以和服務器簡單的下載和提交信息后,我們甚至可以向服務器傳遞或從服務器獲取xml,Json等信息,方便我們使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM