Files
Continentis/Assets/OtherPlugins/Modern UI Pack/Scripts/Icon/IconManager.cs

103 lines
3.5 KiB
C#
Raw Normal View History

2025-10-03 00:02:43 -04:00
using UnityEngine;
using UnityEngine.UI;
namespace Michsky.MUIP
{
[ExecuteInEditMode]
[DisallowMultipleComponent]
[AddComponentMenu("Modern UI Pack/Image/Icon Manager")]
[RequireComponent(typeof(Image))]
public class IconManager : MonoBehaviour
{
// Resources
public IconLibrary iconLibrary;
// Info
public string selectedIconID;
public int selectedIconIndex;
[Range(0, 3)] public int spriteSize;
[HideInInspector] public string currentSize;
[HideInInspector] public bool size32;
[HideInInspector] public bool size64;
[HideInInspector] public bool size128;
[HideInInspector] public bool size256;
2026-03-20 11:56:50 -04:00
private Image imageObject;
private void Awake()
2025-10-03 00:02:43 -04:00
{
try
{
2026-03-20 11:56:50 -04:00
if (iconLibrary == null) iconLibrary = Resources.Load<IconLibrary>("Icon Library");
if (imageObject == null) imageObject = gameObject.GetComponent<Image>();
2025-10-03 00:02:43 -04:00
2026-03-20 11:56:50 -04:00
enabled = true;
2025-10-03 00:02:43 -04:00
UpdateElement();
}
2026-03-20 11:56:50 -04:00
catch
{
Debug.LogWarning("<b>Icon Library</b> is missing, but it should be assigned.", this);
}
2025-10-03 00:02:43 -04:00
}
2026-03-20 11:56:50 -04:00
private void Update()
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
if (iconLibrary.alwaysUpdate) UpdateElement();
if (Application.isPlaying && iconLibrary.optimizeUpdates) enabled = false;
2025-10-03 00:02:43 -04:00
}
public void UpdateElement()
{
if (iconLibrary == null)
{
2026-03-20 11:56:50 -04:00
enabled = false;
2025-10-03 00:02:43 -04:00
return;
}
2026-03-20 11:56:50 -04:00
for (var i = 0; i < iconLibrary.icons.Count; i++)
if (selectedIconID == iconLibrary.icons[i].iconTitle && gameObject.activeInHierarchy)
2025-10-03 00:02:43 -04:00
{
2026-03-20 11:56:50 -04:00
if (spriteSize == 0)
imageObject.sprite = iconLibrary.icons[i].iconSprite32;
else if (spriteSize == 1)
imageObject.sprite = iconLibrary.icons[i].iconSprite64;
else if (spriteSize == 2)
imageObject.sprite = iconLibrary.icons[i].iconSprite128;
else if (spriteSize == 3) imageObject.sprite = iconLibrary.icons[i].iconSprite256;
2025-10-03 00:02:43 -04:00
break;
}
2026-03-20 11:56:50 -04:00
if (!iconLibrary.alwaysUpdate)
enabled = false;
2025-10-03 00:02:43 -04:00
}
public void UpdateSpriteSize(int spriteIndex, int newSize)
{
2026-03-20 11:56:50 -04:00
if (newSize == 0)
imageObject.sprite = iconLibrary.icons[spriteIndex].iconSprite32;
else if (newSize == 1)
imageObject.sprite = iconLibrary.icons[spriteIndex].iconSprite64;
else if (newSize == 2)
imageObject.sprite = iconLibrary.icons[spriteIndex].iconSprite128;
else if (newSize == 3) imageObject.sprite = iconLibrary.icons[spriteIndex].iconSprite256;
2025-10-03 00:02:43 -04:00
}
public void ChangeIcon(string newSprite, int preferredSize)
{
2026-03-20 11:56:50 -04:00
var selectedSpriteIndex = -1;
2025-10-03 00:02:43 -04:00
2026-03-20 11:56:50 -04:00
for (var i = 0; i < iconLibrary.icons.Count; i++)
2025-10-03 00:02:43 -04:00
if (newSprite == iconLibrary.icons[i].iconTitle)
{
selectedSpriteIndex = i;
break;
}
2026-03-20 11:56:50 -04:00
if (selectedSpriteIndex != -1)
UpdateSpriteSize(selectedSpriteIndex, preferredSize);
else
Debug.Log("<b>[Icon Manager]</b> Cannot find an icon named '" + newSprite + "'");
2025-10-03 00:02:43 -04:00
}
}
}