iT邦幫忙

0

[Unity] 測試連網請求功能 - Unity導入雲端排行榜步驟紀錄 02

  • 分享至 

  • xImage
  •  

2023/06/18

建立連網請求功能測試 Unity專案

接下來實作連網功能測試

先創建一個測試連線的版本

  1. 開啓 Unity Hub
  2. 使用 Unity 2021.3.7f1 開一個新專案
  3. 建立 一般 UI 或 GUI 擇一即可,測試建議使用GUI較方便

- 使用一般 UI

  1. 滑鼠右鍵 "Hierachy" > "UI" > "Canvas" (Create Canvas)
  2. 滑鼠左鍵 點Canvas in "Hierachy"> "Inspector" > "Canvas Scaler">
    "UI Scale Mode" Change to "Scale With Screen Size">
    "Reference Resolution" Set to X"1920" Y"1080"
  3. 滑鼠左鍵 點Canvas in "Hierachy"(Select "Canvas")>
    滑鼠右鍵 "Canvas">"UI">"Legacy">"Text"(Create Text)>"txt_Result"(Name it)
  4. 滑鼠左鍵 點Project>Asset>滑鼠右鍵"Create">"Folder">"Scripts"(Name it)
  5. 左鍵雙擊"Scripts"Folder>滑鼠右鍵"Create"> C# Script>Type "GetTest"(得到 GetTest.cs)
  6. 左鍵雙擊GetTest.cs打開IDE(Visual Studio)
    以下是 GetTest.cs 的代碼:
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class GetTest : MonoBehaviour
{
    public Text text;
    void Start()
    {
        StartCoroutine(GetTesting());
    }

    IEnumerator GetTesting() 
    {
        UnityWebRequest webRequest = UnityWebRequest.Get("https://google.com");
        yield return webRequest.SendWebRequest();
        text.text = webRequest.downloadHandler.text;
    }
}

9.滑鼠右鍵 "Hierachy" > "Create Empty" >
type "NetWorkManager"(得到名爲NetWorkManager的GameObject)
"Inspector">"Add Component">type"GetTest">附加GetTest.cs到"NetWorkManager"GameObject上>
拖曳"txt_Result"(txt_Result.text in "Hierachy")到"GetTest(Script)">Text > None(Text)>
綁定成功
10.按下播放鍵測試> 如果 txt_Result.text 顯示 "< !d octype html><html" 意味著成功連網.


使用 GUI

3.建立名爲NetWorkManager的GameObject,附加GetTest.cs
以下是 GetTest.cs 的代碼:

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class GetTest : MonoBehaviour
{
    private string downloadedText = "";
    private Vector2 scrollPosition = Vector2.zero;

    void Start()
    {
        StartCoroutine(GetTesting());
    }

    IEnumerator GetTesting()
    {
        UnityWebRequest webRequest = UnityWebRequest.Get("https://google.com");
        yield return webRequest.SendWebRequest();
        downloadedText = webRequest.downloadHandler.text;
    }

    private void OnGUI()
    {
        // Set the background box position and size
        Rect backgroundRect = new Rect(Screen.width / 2 - 250,
                                      Screen.height / 2 - 250, 500, 500);

        // Draw the black background box
        GUI.Box(backgroundRect, "", GUI.skin.box);

        // Set the scroll view position and size
        Rect scrollViewRect = new Rect(Screen.width / 2 - 240, 
                                      Screen.height / 2 - 240, 480, 480);

        // Begin the scroll view
        scrollPosition = GUI.BeginScrollView(scrollViewRect, scrollPosition, 
                                            new Rect(0, 0, 460, 1000));

        // Draw the label with downloaded text
        GUI.Label(new Rect(0, 0, 460, 1000), downloadedText);

        // End the scroll view
        GUI.EndScrollView();
    }
}

4.按下播放鍵測試> 如果GUI顯示 "< !d octype html><html..." ,可滑動scrollbar看全文,意味著成功連網.


這樣就成功創建了一個測試連網請求功能的測試 Unity專案。


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言