| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- /* 内置角色信息管理者,(PK模式用到的角色选择) */
- public class RoleMgr
- {
- static string[] roleNames = {"海绵宝宝", "方狗狗", "五六七", "CHANEL", "Mimi", "喵了个咪"};
- public static int roleCount {
- get {
- return roleNames.Length;
- }
- }
- //获取角色信息(适用于本地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)) {
- nickname = roleNames[id - 1];
- } else {
- nickname = LoginMgr.myUserInfo.nickname;
- }
- SetAvatarToImage(image, coroutineStarter, id, LoginMgr.myUserInfo.avatarUrl);
- return nickname;
- }
- public static bool IsRoleAvatar(int id) {
- return id >= 1 && id <= 6;
- }
- public static int GetAvatarListLen() {
- return 7 + 24;
- }
- public const int NullAvatarID = int.MinValue;
- public static void SetAvatarToImage(Image image, int avatarID, string avatarUrl) {
- 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;
- if (image) image.sprite = Resources.Load<Sprite>(path);
- }
- }
- //缓存网络图片,避免重复加载
- private static Dictionary<string, Sprite> remoteAvatarMap = new Dictionary<string, Sprite>();
- private static IEnumerator LoadAvatar(string url, Image image)
- {
- if (string.IsNullOrWhiteSpace(url))
- {
- if (image) image.sprite = null;
- yield break;
- }
- if (remoteAvatarMap.ContainsKey(url))
- {
- if (image) image.sprite = remoteAvatarMap[url];
- yield break;
- }
- if (image) image.sprite = null;
- else yield break;
- using (UnityWebRequest uwr = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) {
- uwr.downloadHandler = new DownloadHandlerTexture();
- yield return uwr.SendWebRequest();
- if (uwr.result != UnityWebRequest.Result.Success) yield break;
- 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;
- if (image) image.sprite = sprite;
- }
- }
- }
|