Преглед изворни кода

解决(节点未激活时启动不了协程加载头像)

lvjincheng пре 3 година
родитељ
комит
baead09b03

+ 19 - 8
Assets/BowArrow/Scripts/Manager/RoleMgr.cs

@@ -14,6 +14,10 @@ public class RoleMgr
     }
     //获取角色信息(适用于本地PK获取对手或者我的信息)
     public static string GetRoleInfo(int id, Image image)
+    {   
+        return GetRoleInfo(id, image, image);
+    }
+    public static string GetRoleInfo(int id, Image image, MonoBehaviour coroutineStarter)
     {   
         string nickname = null;
         if (IsRoleAvatar(id)) {
@@ -21,7 +25,7 @@ public class RoleMgr
         } else {
             nickname = LoginMgr.myUserInfo.nickname;
         }
-        SetAvatarToImage(image, id, LoginMgr.myUserInfo.avatarUrl);
+        SetAvatarToImage(image, coroutineStarter, id, LoginMgr.myUserInfo.avatarUrl);
         return nickname;
     }
 
@@ -34,14 +38,20 @@ public class RoleMgr
 
     public const int NullAvatarID = int.MinValue;
     public static void SetAvatarToImage(Image image, int avatarID, string avatarUrl) {
-        if (avatarID == NullAvatarID) image.sprite = null;
-        else if (avatarID < 0) image.StartCoroutine(LoadAvatar(avatarUrl, image));
+        SetAvatarToImage(image, image, avatarID, avatarUrl);
+    }
+    public static void SetAvatarToImage(Image image, MonoBehaviour coroutineStarter, int avatarID, string avatarUrl) {
+        if (avatarID == NullAvatarID)
+        {
+            if (image) image.sprite = null;
+        }
+        else if (avatarID < 0) coroutineStarter.StartCoroutine(LoadAvatar(avatarUrl, image));
         else 
         {
             string path = "Textures/Avatar/";
             if (avatarID < 7) path += "Player" + avatarID;
             else path += avatarID - 7;
-            image.sprite = Resources.Load<Sprite>(path);
+            if (image) image.sprite = Resources.Load<Sprite>(path);
         }
     }
 
@@ -51,15 +61,16 @@ public class RoleMgr
     {
         if (string.IsNullOrWhiteSpace(url))
         {
-            image.sprite = null;
+            if (image) image.sprite = null;
             yield break;
         }
         if (remoteAvatarMap.ContainsKey(url))
         {
-            image.sprite = remoteAvatarMap[url];
+            if (image) image.sprite = remoteAvatarMap[url];
             yield break;
         }
-        image.sprite = null;
+        if (image) image.sprite = null;
+        else yield break;
         using (UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) {
             uwr.downloadHandler = new DownloadHandlerTexture();
             yield return uwr.SendWebRequest();
@@ -67,7 +78,7 @@ public class RoleMgr
             Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
             Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
             remoteAvatarMap[url] = sprite;
-            image.sprite = sprite;
+            if (image) image.sprite = sprite;
         }
     }
 }

+ 2 - 1
Assets/BowArrow/Scripts/View/PKGameReadyView.cs

@@ -47,7 +47,7 @@ public class PKGameReadyView : MonoBehaviour
     void RenderPlayerInfo() {
         if (GlobalData.pkMatchType == PKMatchType.LocalPK) {
             string nickName = RoleMgr.GetRoleInfo(PKGameMode.playerRoleIDs[pKGameMode.currentPlayerIndex],
-                this.transform.Find("Panel/Avatar/Sprite").GetComponent<Image>());
+                this.transform.Find("Panel/Avatar/Sprite").GetComponent<Image>(), this);
             this.transform.Find("Panel/Name").GetComponent<Text>().text = nickName;
         }
         else if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
@@ -57,6 +57,7 @@ public class PKGameReadyView : MonoBehaviour
             string nickname = GlobalData.matchPlayerInfos[curPlayerIndex].nickname;
             RoleMgr.SetAvatarToImage(
                 this.transform.Find("Panel/Avatar/Sprite").GetComponent<Image>(),
+                this,
                 avatarID, avatarUrl
             );
             this.transform.Find("Panel/Name").GetComponent<Text>().text = nickname;

+ 2 - 1
Assets/BowArrow/Scripts/View/PKGameReadyView_Challenge.cs

@@ -35,7 +35,7 @@ public class PKGameReadyView_Challenge : MonoBehaviour
         if (GlobalData.pkMatchType == PKMatchType.LocalPK) {
             int curPlayerIndex = ((ChallengeGameModeLocalPK) gameMode).GetCurrentPlayIndex();
             string nickName = RoleMgr.GetRoleInfo(GlobalData.localPK_playerRoleIDs[curPlayerIndex],
-                this.transform.Find("Panel/Avatar/Sprite").GetComponent<Image>());
+                this.transform.Find("Panel/Avatar/Sprite").GetComponent<Image>(), this);
             this.transform.Find("Panel/Name").GetComponent<Text>().text = nickName;
         }
         else if (GlobalData.pkMatchType == PKMatchType.OnlinePK) {
@@ -45,6 +45,7 @@ public class PKGameReadyView_Challenge : MonoBehaviour
             string nickname = GlobalData.matchPlayerInfos[curPlayerIndex].nickname;
             RoleMgr.SetAvatarToImage(
                 this.transform.Find("Panel/Avatar/Sprite").GetComponent<Image>(),
+                this,
                 avatarID, avatarUrl
             );
             this.transform.Find("Panel/Name").GetComponent<Text>().text = nickname;