【问题标题】:How to get an image from a url and set it to an object in Unity如何从 url 获取图像并将其设置为 Unity 中的对象
【发布时间】:2022-03-15 00:03:51
【问题描述】:

我有一种从预制件创建对象的方法。预制件由文本“标题”和“描述”以及图像“图像”组成。

在下面的方法中,我得到“标题”、“描述”的文本和“图像”的图像 URL。

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json.Linq;

using System.Threading.Tasks;
using UnityEngine.Networking;

public class CreateMonumentButtons : MonoBehaviour
{
    public RectTransform prefarb;
    public RectTransform content;

    private string City;
    private string URL = DataHolders.URL;

    async private void Awake()
    {
        City = DataHolders.City;
        StartCoroutine(Get());
    }

    void InitializeItemView (GameObject viewGameObject, string TextTitle, string TextDescription, string URLImg)
    {
        TestItemView view = new TestItemView(viewGameObject.transform);
        view.Title.text = TextTitle;
        view.Description.text = TextDescription;

        File fileData;
        Texture2d txture;
        (File.Exists(URLImg))     {
            fileData = File.ReadAllBytes(URLImg); //read the bytes from the file at URLImg's destination
            txture = new Texture2D(0,0);//makes the new texture
            txture.LoadImage(fileData);//loads the image from the file
            view.Img.sprite = Sprite.Create(txture, new Rect(0,0,200,200), new Vector2());
            //set Img.sprite to a new sprite at 0,0 with the dimensions 200x200
        }
    }

    public class TestItemView
    {
        public Text Title;
        public Text Description;
        // public Image Image;

        public TestItemView (Transform rootView)
        {
            Title = rootView.Find("Title").GetComponent<Text>();
            Description = rootView.Find("Description").GetComponent<Text>();
            // Image = rootView.Find("Image").GetComponent<Image>();
        }
    }

    private IEnumerator Get()
    {
        WWWForm formT = new WWWForm();

        formT.AddField("City", City);

        WWW wwwT = new WWW(URL + "get_monuments.php", formT);
        yield return wwwT;
        if (wwwT.error != null)
        {
            Debug.Log("Ошибка: " + wwwT.error);
            yield break;
        }

        WWWForm formD = new WWWForm();

        formD.AddField("City", City);

        WWW wwwD = new WWW(URL + "get_description.php", formD);
        yield return wwwD;
        if (wwwD.error != null)
        {
            Debug.Log("Ошибка: " + wwwD.error);
            yield break;
        }

        WWWForm formI = new WWWForm();

        formI.AddField("City", City);

        WWW wwwI = new WWW(URL + "get_img.php", formI);
        yield return wwwI;
        if (wwwI.error != null)
        {
            Debug.Log("Ошибка: " + wwwI.error);
            yield break;
        }

        WWWForm formL = new WWWForm();

        formL.AddField("City", City);

        WWW wwwL = new WWW(URL + "get_lenLists.php", formL);
        yield return wwwL;
        if (wwwL.error != null)
        {
            Debug.Log("Ошибка: " + wwwL.error);
            yield break;
        }

        DataHolders.listLen = int.Parse(wwwL.text);
        DataHolders.listImg = wwwI.text;
        DataHolders.listDescription = wwwD.text;  
        DataHolders.listMonuments = wwwT.text; 

        var ListTitls = JObject.Parse(DataHolders.listMonuments);
        var ListDescriptions = JObject.Parse(DataHolders.listDescription);
        var ListImgs = JObject.Parse(DataHolders.listImg);

        for( int i = 0; i < DataHolders.listLen; i++ )
        {
            // Debug.Log(i.ToString());
            var Title = (string) ListTitls[i.ToString()];
            var Description = (string) ListDescriptions[i.ToString()];
            var Img = (string) ListImgs[i.ToString()];

            var instance = GameObject.Instantiate(prefarb.gameObject) as GameObject;
            instance.transform.SetParent(content, false);
            InitializeItemView(instance, Title, Description, Img);
        }      
    }
}

我不明白如何从图片网址获取图片并将其值设置为“图片”。

但我是初学者,所以最好用简单的语言解释一下。

【问题讨论】:

标签: c# unity3d


【解决方案1】:

Unity Beginner 已在 1.5 分钟内向Load Image from WebLink or URL 轻松解释。

但在这里,他们使用了已过时的 WWW,因此请改用 UnityWebRequest。 Here 是您在从 WWW 更改为 UnityWebRequest 时需要执行的更改。

【讨论】:

    【解决方案2】:

    图片没有Text属性,取而代之,你应该获取文件,将图片加载到纹理,并将图片的精灵设置为纹理。

    File fileData;
    Texture2d txture;
    if (File.Exists(URLImg))     {
             fileData = File.ReadAllBytes(URLImg); //read the bytes from the file at URLImg's destination
             txture = new Texture2D(0,0);//makes the new texture
             txture.LoadImage(fileData);//loads the image from the file
             view.Img.sprite = Sprite.Create(txture, new Rect(0,0,200,200), new Vector2());
             //set Img.sprite to a new sprite at 0,0 with the dimensions 200x200
         }
    

    view.Img.sprite 用作图像而不是“文本”,因为它不是在绘制文本,而是在绘制图像。

    【讨论】:

    • 出于某种原因,统一说“(30,30):错误CS1002:;预期”
    • 我需要查看您的代码。听起来您缺少分号@КаналShoker2
    • 它映射到这个位置(File.Exists(URLImg))
    • @КаналShoker2 似乎 URLImg 可能不是一个变量或者其他东西可能是错误的,我需要再次查看至少完整的方法来查看发生了什么问题
    • 我找到了解决方案。我像这样更改了您的代码:Texture2D txture; if (System.IO.File.Exists(URLImg)) { var fileData = System.IO.File.ReadAllBytes(URLImg); //read the bytes from the file at URLImg's destination txture = new Texture2D(0,0);//makes the new texture txture.LoadImage(fileData);//loads the image from the file view.Image.sprite = Sprite.Create(txture, new Rect(0,0,200,200), new Vector2()); //set Img.sprite to a new sprite at 0,0 with the dimensions 200x200 }
    猜你喜欢
    • 2015-05-27
    • 1970-01-01
    • 2017-10-09
    • 1970-01-01
    • 2017-08-27
    • 2022-11-18
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多