geometry-tower-defense/Assets/GameMain/Scripts/UI/General/View/IconArea.cs

95 lines
2.4 KiB
C#

using System;
using GeometryTD.Definition;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public class IconArea : MonoBehaviour
{
[SerializeField] private Image _board;
[SerializeField] private Image _icon;
private IconAreaContext _context;
public Sprite CurrentIconSprite => _icon != null ? _icon.sprite : null;
public Color CurrentIconColor => _icon != null ? _icon.color : Color.white;
public Vector2 CurrentIconSize => _icon != null ? _icon.rectTransform.rect.size : Vector2.zero;
public Material CurrentIconMaterial => _icon != null ? _icon.material : null;
public void OnInit(IconAreaContext context)
{
if (context == null)
{
Log.Error("Icon area need IconAreaContext.");
return;
}
_context = context;
switch (context.ComponentSlotType)
{
case TowerCompSlotType.Base:
case TowerCompSlotType.Bearing:
case TowerCompSlotType.Muzzle:
GameEntry.SpriteCache.GetSprite(context.ComponentSlotType.ToString(), SetIcon);
break;
default:
SetIcon(_context.Icon);
break;
}
SetRarity(_context.Rarity);
SetIconColor(_context.Color);
}
public void SetIcon(Sprite sprite)
{
if (_icon != null)
{
_icon.sprite = sprite;
}
}
public void SetRarity(RarityType rarity)
{
if (_board == null)
{
return;
}
_board.color = rarity switch
{
RarityType.White => Color.white,
RarityType.Green => Color.green,
RarityType.Blue => Color.blue,
RarityType.Purple => Color.magenta,
RarityType.Red => Color.red,
_ => Color.clear,
};
}
public void SetIconColor(Color color)
{
if (_icon == null)
{
return;
}
_icon.color = color;
}
public void OnReset()
{
SetIcon(null);
SetRarity(RarityType.None);
SetIconColor(Color.clear);
}
}
}