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

时间:2018-11-13 16:08:41

标签: c# unity3d

在Unity中,我有一个自定义窗口,我试图从Amazon S3加载图像并将其显示在窗口中。但是,我的函数似乎没有执行。如果我在函数中放入Debug.Log,它将永远不会被记录,但是,在函数被调用(Debug.Log)之前的LoadAvatarTexture会记录。据我了解,IEnumeratorEditorWindow类中不起作用,但在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 个答案:

答案 0 :(得分:2)

  

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

这是因为LoadAvatarTexture是协程函数。您不会像普通函数那样调用协程函数。您可以使用StartCoroutine函数启动它。例如,StartCoroutine(LoadAvatarTexture())

即使以StartCoroutine开头也无法在您的特定情况下使用,因为这是一个Editor插件,并且StartCoroutine需要MonoBehaviour的实例才能工作。当脚本从StartCoroutine派生而来时,您只能访问MonoBehaviour,但不是。

您有两个选择:

1 。继续将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"));

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

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 。另一种选择是使LoadAvatarTexture函数成为普通(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);
}