UI update 02
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class mailSlotPrefab : MonoBehaviour
|
||||
{
|
||||
private static readonly Dictionary<string, Sprite> RewardPreviewCache = new Dictionary<string, Sprite>(System.StringComparer.Ordinal);
|
||||
private static readonly HashSet<string> RewardPreviewLoadsInFlight = new HashSet<string>(System.StringComparer.Ordinal);
|
||||
|
||||
[Header("buttons")]
|
||||
public Button openButton;
|
||||
public Button receivedButton;
|
||||
@@ -16,4 +22,172 @@ public class mailSlotPrefab : MonoBehaviour
|
||||
public Image hasGift_icon;
|
||||
[Header("select")]
|
||||
public Image selectedImage;
|
||||
[Header("gifts")]
|
||||
public Text rewards_with_mail_text;
|
||||
[Header("reward previews")]
|
||||
public Transform rewardPreviewRoot;
|
||||
public GameObject rewardPreviewPrefab;
|
||||
public Sprite fallbackRewardPreviewSprite;
|
||||
|
||||
public void RefreshRewardsWithMailText(mail_so mailData)
|
||||
{
|
||||
if (rewards_with_mail_text == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int rewardCount = GetRewardCount(mailData);
|
||||
rewards_with_mail_text.text = rewardCount <= 0
|
||||
? "\u4e0d\u5305\u542b\u9644\u4ef6"
|
||||
: $"\u5305\u542b{rewardCount}\u4e2a\u9644\u4ef6";
|
||||
}
|
||||
|
||||
public void RefreshRewardPreviews(mail_so mailData)
|
||||
{
|
||||
if (rewardPreviewRoot == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = rewardPreviewRoot.childCount - 1; i >= 0; i--)
|
||||
{
|
||||
Destroy(rewardPreviewRoot.GetChild(i).gameObject);
|
||||
}
|
||||
|
||||
if (mailData == null || mailData.rewardList == null || mailData.rewardList.Count == 0 || rewardPreviewPrefab == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < mailData.rewardList.Count; i++)
|
||||
{
|
||||
mail_so.rewardItem reward = mailData.rewardList[i];
|
||||
if (reward == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
GameObject go = Instantiate(rewardPreviewPrefab, rewardPreviewRoot);
|
||||
Image icon = go.GetComponent<Image>();
|
||||
if (icon == null)
|
||||
{
|
||||
icon = go.GetComponentInChildren<Image>(true);
|
||||
}
|
||||
|
||||
BindRewardPreviewIcon(icon, reward);
|
||||
}
|
||||
}
|
||||
|
||||
private void BindRewardPreviewIcon(Image icon, mail_so.rewardItem reward)
|
||||
{
|
||||
if (icon == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (reward != null && reward.reward_image != null)
|
||||
{
|
||||
icon.sprite = reward.reward_image;
|
||||
icon.enabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
string url = reward != null ? (reward.reward_icon_url ?? string.Empty).Trim() : string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(url))
|
||||
{
|
||||
icon.sprite = fallbackRewardPreviewSprite;
|
||||
icon.enabled = fallbackRewardPreviewSprite != null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!url.StartsWith("http://", System.StringComparison.OrdinalIgnoreCase)
|
||||
&& !url.StartsWith("https://", System.StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
NetworkManager network = NetworkManager.Instance;
|
||||
if (network != null && !string.IsNullOrWhiteSpace(network.ServerUrl))
|
||||
{
|
||||
string baseUrl = network.ServerUrl.TrimEnd('/');
|
||||
if (!url.StartsWith("/"))
|
||||
{
|
||||
url = "/" + url;
|
||||
}
|
||||
url = baseUrl + url;
|
||||
}
|
||||
}
|
||||
|
||||
if (RewardPreviewCache.TryGetValue(url, out Sprite cachedSprite) && cachedSprite != null)
|
||||
{
|
||||
icon.sprite = cachedSprite;
|
||||
icon.enabled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
icon.sprite = fallbackRewardPreviewSprite;
|
||||
icon.enabled = icon.sprite != null;
|
||||
|
||||
if (!RewardPreviewLoadsInFlight.Add(url))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StartCoroutine(LoadRewardPreviewSpriteRoutine(url, icon));
|
||||
}
|
||||
|
||||
private IEnumerator LoadRewardPreviewSpriteRoutine(string imageUrl, Image icon)
|
||||
{
|
||||
using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(imageUrl))
|
||||
{
|
||||
yield return request.SendWebRequest();
|
||||
try
|
||||
{
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
Texture2D texture = DownloadHandlerTexture.GetContent(request);
|
||||
if (texture == null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
Sprite sprite = Sprite.Create(
|
||||
texture,
|
||||
new Rect(0f, 0f, texture.width, texture.height),
|
||||
new Vector2(0.5f, 0.5f));
|
||||
RewardPreviewCache[imageUrl] = sprite;
|
||||
if (icon != null)
|
||||
{
|
||||
icon.sprite = sprite;
|
||||
icon.enabled = true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
RewardPreviewLoadsInFlight.Remove(imageUrl);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static int GetRewardCount(mail_so mailData)
|
||||
{
|
||||
if (mailData == null || mailData.rewardList == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < mailData.rewardList.Count; i++)
|
||||
{
|
||||
mail_so.rewardItem reward = mailData.rewardList[i];
|
||||
if (reward == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
count += Mathf.Max(0, reward.reward_ammount);
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user