103 lines
No EOL
3.4 KiB
C#
103 lines
No EOL
3.4 KiB
C#
using ShiroginSDK.Runtime.Modules.Events;
|
|
using ShiroginSDK.Runtime.Modules.UI.Theme.Enums;
|
|
using ShiroginSDK.Runtime.Modules.UI.Theme.Scripts.SO;
|
|
using ShiroginSDK.Runtime.Services.Base;
|
|
using ShiroginSDK.Runtime.Services.Interfaces;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
namespace ShiroginSDK.Runtime.Modules.UI.Theme.Scripts.UI
|
|
{
|
|
/// <summary>
|
|
/// Automatically updates its Image sprite when the theme changes.
|
|
/// Works with ShiroginSDK service architecture.
|
|
/// </summary>
|
|
[RequireComponent(typeof(Image))]
|
|
public class ThemeImage : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private ThemeAssetType assetType = ThemeAssetType.None;
|
|
|
|
private IEventService _eventService;
|
|
|
|
private Image _image;
|
|
private string _lastAppliedTheme;
|
|
|
|
private IThemeService _themeService;
|
|
|
|
// ------------------------------------------------------
|
|
// Unity Lifecycle
|
|
// ------------------------------------------------------
|
|
private void Awake()
|
|
{
|
|
_image = GetComponent<Image>();
|
|
|
|
// Resolve dependencies
|
|
_themeService = ServiceLocator.Get<IThemeService>();
|
|
_eventService = ServiceLocator.Get<IEventService>();
|
|
|
|
ApplyTheme(); // initial apply with current theme
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
_eventService?.Subscribe<ShiroginEvents.Theme.ThemeChanged>(OnThemeChanged);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_eventService?.Unsubscribe<ShiroginEvents.Theme.ThemeChanged>(OnThemeChanged);
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
// Editor-time preview for Default profile (for convenience)
|
|
private void OnValidate()
|
|
{
|
|
if (!Application.isPlaying && TryGetComponent(out Image img))
|
|
{
|
|
var repo = AssetDatabase.LoadAssetAtPath<ThemeRepository>(
|
|
"Assets/ShiroginSDK/Resources/ThemeRepository.asset"
|
|
);
|
|
var profile = repo != null ? repo.GetProfile("Default") : null;
|
|
var sprite = profile?.GetSprite(assetType);
|
|
if (sprite != null)
|
|
img.sprite = sprite;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// ------------------------------------------------------
|
|
// Theme Change Handler
|
|
// ------------------------------------------------------
|
|
private void OnThemeChanged(ShiroginEvents.Theme.ThemeChanged obj)
|
|
{
|
|
var newTheme = obj.ThemeName;
|
|
|
|
// Skip redundant updates
|
|
if (!string.IsNullOrEmpty(_lastAppliedTheme) && _lastAppliedTheme == newTheme)
|
|
return;
|
|
|
|
_lastAppliedTheme = newTheme;
|
|
ApplyTheme();
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// Apply Theme
|
|
// ------------------------------------------------------
|
|
public void ApplyTheme()
|
|
{
|
|
if (assetType == ThemeAssetType.None || _image == null || _themeService == null)
|
|
return;
|
|
|
|
var sprite = _themeService.GetSprite(assetType);
|
|
if (sprite != null)
|
|
_image.sprite = sprite;
|
|
#if UNITY_EDITOR
|
|
else
|
|
Debug.LogWarning(
|
|
$"[ThemeImage] Sprite not found for {assetType} (theme: {_themeService.CurrentThemeName}).");
|
|
#endif
|
|
}
|
|
}
|
|
} |