169 lines
3.9 KiB
C#
169 lines
3.9 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Game
|
|||
|
// Author : Kimch
|
|||
|
// Created : 2021-12-24
|
|||
|
// Description :
|
|||
|
// Last Modified By :
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "IconProxy" company="Kunpo"></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
|
|||
|
using System.Collections;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
using UnityEngine.Networking;
|
|||
|
using UnityEngine.U2D;
|
|||
|
using UnityEngine.UI;
|
|||
|
|
|||
|
namespace G
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 图标管理
|
|||
|
/// </summary>
|
|||
|
public class IconProxy : F.GameProxy
|
|||
|
{
|
|||
|
#region Field
|
|||
|
|
|||
|
private Dictionary<string, SpriteAtlas> _spriteAtlasDict = new Dictionary<string, SpriteAtlas>();
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region API
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="source"></param>
|
|||
|
/// <param name="spriteName"></param>
|
|||
|
public async void SetSpriteAsync(Image source, string spriteName)
|
|||
|
{
|
|||
|
if (source)
|
|||
|
{
|
|||
|
source.overrideSprite = null;
|
|||
|
var sprite = await AssetProxy.Instance.TryGetTemporaryAssetAsync<Sprite>(spriteName).Task;
|
|||
|
if (source)
|
|||
|
source.overrideSprite = sprite;
|
|||
|
}
|
|||
|
else
|
|||
|
source.overrideSprite = null;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="source"></param>
|
|||
|
/// <param name="spriteInfo"></param>
|
|||
|
public void SetSprite(Image source, string[] spriteInfo)
|
|||
|
{
|
|||
|
if (spriteInfo != null && spriteInfo.Length >= 2)
|
|||
|
{
|
|||
|
SetSprite(source, spriteInfo[0], spriteInfo[1]);
|
|||
|
return;
|
|||
|
}
|
|||
|
else
|
|||
|
source.overrideSprite = null;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="source"></param>
|
|||
|
/// <param name="spriteName"></param>
|
|||
|
/// <param name="atlasName"></param>
|
|||
|
public async void SetSprite(Image source, string spriteName, string atlasName)
|
|||
|
{
|
|||
|
if (source)
|
|||
|
{
|
|||
|
if (_spriteAtlasDict.TryGetValue(atlasName, out SpriteAtlas atlas))
|
|||
|
{
|
|||
|
source.overrideSprite = atlas.GetSprite(spriteName);
|
|||
|
return;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
source.overrideSprite = null;
|
|||
|
|
|||
|
atlas = await AssetProxy.Instance.TryGetTemporaryAssetAsync<SpriteAtlas>(atlasName).Task;
|
|||
|
if (source && atlas)
|
|||
|
source.overrideSprite = atlas.GetSprite(spriteName);
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
source.overrideSprite = null;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="source"></param>
|
|||
|
/// <param name="spriteInfo"></param>
|
|||
|
public async void SetSprite(SpriteRenderer source, string[] spriteInfo)
|
|||
|
{
|
|||
|
if (source && spriteInfo != null && spriteInfo.Length >= 2)
|
|||
|
{
|
|||
|
if (_spriteAtlasDict.TryGetValue(spriteInfo[1], out SpriteAtlas atlas))
|
|||
|
{
|
|||
|
source.sprite = atlas.GetSprite(spriteInfo[0]);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
atlas = await AssetProxy.Instance.TryGetTemporaryAssetAsync<SpriteAtlas>(spriteInfo[1]).Task;
|
|||
|
if (source && atlas)
|
|||
|
source.sprite = atlas.GetSprite(spriteInfo[0]);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void SetSpriteWithURL(Image source, string url)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(url))
|
|||
|
StartCoroutine(SetSpriteWithURLCO(source, url));
|
|||
|
}
|
|||
|
|
|||
|
IEnumerator SetSpriteWithURLCO(Image source, string url)
|
|||
|
{
|
|||
|
var request = UnityWebRequestTexture.GetTexture(url);
|
|||
|
yield return request.SendWebRequest();
|
|||
|
if (string.IsNullOrEmpty(request.error))
|
|||
|
{
|
|||
|
if (source)
|
|||
|
{
|
|||
|
var tex = DownloadHandlerTexture.GetContent(request);
|
|||
|
if (tex)
|
|||
|
{
|
|||
|
var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), Vector2.one * 0.5f);
|
|||
|
source.overrideSprite = sprite;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void LoadAtlas(IList<SpriteAtlas> spriteAtlas)
|
|||
|
{
|
|||
|
foreach (var item in spriteAtlas)
|
|||
|
{
|
|||
|
_spriteAtlasDict.Add(item.name, item);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Method
|
|||
|
|
|||
|
public void PreLoadCommon()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Unity
|
|||
|
|
|||
|
public static IconProxy Instance => GetInstance<IconProxy>();
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|