首页 > 解决方案 > 如何:获取对 UI 图像的引用

问题描述

与我之前提出的问题类似,我现在正在学习如何TestImage在脚本中获取我的图像的参考。

到目前为止,我已经尝试过:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class imageGrab : MonoBehaviour
{

        [Space] public RawImage testImage;

         Texture2D profileTexture;




    // Start is called before the first frame update
    void Start()
    {
        profileTexture = new Texture2D(2, 2);
    }

    void Awake() 
    {
        PlayPortalProfileClient.Instance.GetMyProfilePicture((error, data) =>
            {
                if (error != null)
                {
                    ErrorLogging("Error requesting my profile picture.", error);
                }
                else
                {
                    Debug.Log("Got my profile picture successfully.");

                    profileTexture.LoadImage(data);
                    testImage.texture = profileTexture;
                }
            });
    }

    // Update is called once per frame
    void Update()
    {

    }
}

但是遇到这个错误,Assets/imageGrab.cs(8,24): error CS0246: The type or namespace name 'RawImage' could not be found (are you missing a using directive or an assembly reference?)

我可以做些什么来正确获得参考?有没有像引用文本元素一样简单的东西?谢谢!

标签: unity3dgame-development

解决方案


把它放在最上面:

using UnityEngine.UI

推荐阅读