RoleMgr.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Networking;
  6. /* 内置角色信息管理者,(PK模式用到的角色选择) */
  7. public class RoleMgr
  8. {
  9. static string[] roleNames = {"海绵宝宝", "方狗狗", "五六七", "CHANEL", "Mimi", "喵了个咪"};
  10. public static int roleCount {
  11. get {
  12. return roleNames.Length;
  13. }
  14. }
  15. //获取角色信息(适用于本地PK获取对手或者我的信息)
  16. public static string GetRoleInfo(int id, Image image)
  17. {
  18. return GetRoleInfo(id, image, image);
  19. }
  20. public static string GetRoleInfo(int id, Image image, MonoBehaviour coroutineStarter)
  21. {
  22. string nickname = null;
  23. if (IsRoleAvatar(id)) {
  24. nickname = roleNames[id - 1];
  25. } else {
  26. nickname = LoginMgr.myUserInfo.nickname;
  27. }
  28. SetAvatarToImage(image, coroutineStarter, id, LoginMgr.myUserInfo.avatarUrl);
  29. return nickname;
  30. }
  31. public static bool IsRoleAvatar(int id) {
  32. return id >= 1 && id <= 6;
  33. }
  34. public static int GetAvatarListLen() {
  35. return 7 + 24;
  36. }
  37. public const int NullAvatarID = int.MinValue;
  38. public static void SetAvatarToImage(Image image, int avatarID, string avatarUrl) {
  39. SetAvatarToImage(image, image, avatarID, avatarUrl);
  40. }
  41. public static void SetAvatarToImage(Image image, MonoBehaviour coroutineStarter, int avatarID, string avatarUrl) {
  42. if (avatarID == NullAvatarID)
  43. {
  44. if (image) image.sprite = null;
  45. }
  46. else if (avatarID < 0) coroutineStarter.StartCoroutine(LoadAvatar(avatarUrl, image));
  47. else
  48. {
  49. string path = "Textures/Avatar/";
  50. if (avatarID < 7) path += "Player" + avatarID;
  51. else path += avatarID - 7;
  52. if (image) image.sprite = Resources.Load<Sprite>(path);
  53. }
  54. }
  55. //缓存网络图片,避免重复加载
  56. private static Dictionary<string, Sprite> remoteAvatarMap = new Dictionary<string, Sprite>();
  57. private static IEnumerator LoadAvatar(string url, Image image)
  58. {
  59. if (string.IsNullOrWhiteSpace(url))
  60. {
  61. if (image) image.sprite = null;
  62. yield break;
  63. }
  64. if (remoteAvatarMap.ContainsKey(url))
  65. {
  66. if (image) image.sprite = remoteAvatarMap[url];
  67. yield break;
  68. }
  69. if (image) image.sprite = null;
  70. else yield break;
  71. using (UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) {
  72. uwr.downloadHandler = new DownloadHandlerTexture();
  73. yield return uwr.SendWebRequest();
  74. if (uwr.result != UnityWebRequest.Result.Success) yield break;
  75. Texture2D texture = DownloadHandlerTexture.GetContent(uwr);
  76. Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
  77. remoteAvatarMap[url] = sprite;
  78. if (image) image.sprite = sprite;
  79. }
  80. }
  81. }