但是,我的函数似乎没有执行。
那是因为
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);
}