在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;
  }

}

最佳答案

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


这是因为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);
}

关于c# - 在编辑器窗口插件中使用WWW和UnityWebRequest,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53285037/

10-12 05:21