看了丛林战争学到了回调函数不能访问ui,需要异步方法,比如update,但是update里是不是不能调用协程
private string url;
private string enterPlayerIndex;
private bool isPlayerEnterRoom = false;
private void Update()
{
if (isPlayerEnterRoom == true)
{
SetUserIcon(url, enterPlayerIndex);
isPlayerEnterRoom = false;
url = null;
enterPlayerIndex = null;
}
}
public void SetUserIconSync(string url, string enterPlayerIndex)
{
isPlayerEnterRoom = true;
this.url = url;
this.enterPlayerIndex = enterPlayerIndex;
}
public void SetUserIcon(string url, string enterPlayerIndex)
{
StartCoroutine(UpdateLoadUserIcon(url,enterPlayerIndex));
}
IEnumerator UpdateLoadUserIcon(string url, string index)
{//客户端更新之后加入的别的客户端
WWW www = new WWW(url);
yield return www;
if (www.isDone && www.error == null)
{
Texture2D texture2D = www.texture;
imageList[int.Parse(index) - 1].sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero);
//isPlayerEnterRoom = false;
}
}
发现不能执行
public override void OnResponse(string data)
{
string[] strs = data.Split(',');
ReturnCode returnCode = (ReturnCode)(int.Parse(strs[0]));
if (returnCode == ReturnCode.NeedCallBack)
{
}
else if (returnCode == ReturnCode.Success)
{
//得到了别的客户端发来的本地需要更新的用户信息
string uRl = strs[1];
string iNdex = strs[2];
roomPanel.SetUserIconSync(uRl,iNdex);
}
}