代码之家  ›  专栏  ›  技术社区  ›  Get Off My Lawn

在编辑器窗口插件中使用WWW和UnityWebRequest

  •  2
  • Get Off My Lawn  · 技术社区  · 7 年前

    在Unity中,我有一个自定义窗口,我试图从AmazonS3加载一个图像并在窗口中显示它。但是,我的函数似乎没有执行。如果我放一个 Debug.Log 调试日志 在调用函数之前( LoadAvatarTexture IEnumerator 在一个环境中不起作用 EditorWindow 类,但为 MonoBehaviour 班级。如何使编辑器窗口加载图像?

    public class MyEditorWindow : EditorWindow {
    
      Texture2D avatarTexture;
    
      [MenuItem("GameSmart/Player Manager", false, 0)]
      public static void ShowManager() {
        var window = EditorWindow.GetWindow(typeof(MyEditorWindow));
        window.titleContent = new GUIContent("Player Manager");
        window.minSize = new Vector2(400, 300);
      }
    
      void OnGUI() {
        var loadPlayer = GUILayout.Button("Load Player");
        if (loadPlayer) {
          Debug.Log("I log to the console just fine");
          LoadAvatarTexture("http://dev-avatars.gamesmart.com/default.png");
        }
        if (avatarTexture != null) {
          float aspect = (float)avatarTexture.width / (float)avatarTexture.height;
          Rect previewRect = GUILayoutUtility.GetAspectRect(aspect, GUILayout.Width(100), GUILayout.ExpandWidth(true));
          GUI.DrawTexture(previewRect, avatarTexture, ScaleMode.ScaleToFit, true, aspect);
        }
      }
    
      IEnumerator LoadAvatarTexture(string url) {
        Debug.Log("I do not log to the console");
        var www = new WWW(url);
        yield return www;
        avatarTexture = www.texture;
      }
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Programmer    7 年前

    但是,我的函数似乎没有执行。

    那是因为 LoadAvatarTexture StartCoroutine 功能。例如 StartCoroutine(LoadAvatarTexture())

    启动程序 在您的特定情况下不起作用,因为这是一个编辑器插件 需要实例 MonoBehaviour 工作。您只能访问 当您的脚本从 但事实并非如此。

    您有两个选择:

    . 继续拥有 加载纹理 单一行为

    LoadAvatarTexture("http://dev-avatars.gamesmart.com/default.png");
    

    具有

    //Get camera's MonoBehaviour
    MonoBehaviour camMono = Camera.main.GetComponent<MonoBehaviour>();
    //Use it to start your coroutine function
    camMono.StartCoroutine(LoadAvatarTexture("http://dev-avatars.gamesmart.com/default.png"));
    

    请注意,在使用请求之前必须检查错误。下面是你的新照片 修改函数以检查错误:

    IEnumerator LoadAvatarTexture(string url)
    {
        Debug.Log("I do not log to the console");
        var www = new WWW(url);
        yield return www;
        if (string.IsNullOrEmpty(www.error))
            avatarTexture = www.texture;
        else
            Debug.Log(www.error);
    }
    

    2. . 另一个选择是使 void )函数而不是协程函数,然后使用 WWW.isDone 确定请求何时完成。

    void OnGUI()
    {
        var loadPlayer = GUILayout.Button("Load Player");
        if (loadPlayer)
        {
            Debug.Log("I log to the console just fine");
            LoadAvatarTexture("http://dev-avatars.gamesmart.com/default.png");
        }
    
        //Check if request is done then get the texture
        if (www != null && www.isDone)
        {
            if (string.IsNullOrEmpty(www.error))
                avatarTexture = www.texture;
            else
                Debug.Log(www.error);
    
            //Reset
            www = null;
        }
    
        if (avatarTexture != null)
        {
            float aspect = (float)avatarTexture.width / (float)avatarTexture.height;
            Rect previewRect = GUILayoutUtility.GetAspectRect(aspect, GUILayout.Width(100), GUILayout.ExpandWidth(true));
            GUI.DrawTexture(previewRect, avatarTexture, ScaleMode.ScaleToFit, true, aspect);
        }
    }
    
    WWW www;
    void LoadAvatarTexture(string url)
    {
        Debug.Log("I do not log to the console");
        www = new WWW(url);
        if (string.IsNullOrEmpty(www.error))
            avatarTexture = www.texture;
        else
            Debug.Log(www.error);
    }