Files
ichni_Official/Assets/ThemeBundles/Basic/Scripts/EnvironmentObjects/Custom2DShape.cs

125 lines
4.9 KiB
C#
Raw Normal View History

2026-03-31 07:51:40 -04:00
using System;
using System.Collections.Generic;
using Ichni.RhythmGame.Beatmap;
using UnityEngine;
namespace Ichni.RhythmGame.ThemeBundles.Basic
{
/// <summary>
/// 自定义 2D 形状环境物体,支持从 AssetBundle 动态加载 Sprite
/// 并通过 ColorSubmodule 同步颜色。接入了 DirtyMark 集中刷新机制。
/// </summary>
public class Custom2DShape : EnvironmentObject
{
#region [] Exposed Fields
2026-04-03 10:53:11 -04:00
public string spriteThemeBundleName;
2026-03-31 07:51:40 -04:00
public string spriteName = "None";
public SpriteRenderer spriteRenderer;
#endregion
#region [] Lifecycle & Factory
public static Custom2DShape GenerateElement(string elementName, Guid id, List<string> tags,
bool isFirstGenerated, string themeBundleName, string objectName, GameElement parentElement,
2026-04-03 10:53:11 -04:00
bool isStatic, string spriteThemeBundleName, string spriteName)
2026-03-31 07:51:40 -04:00
{
// 通过 EnvironmentObject.GenerateElement 创建 GameObject 并获取组件
Custom2DShape obj = EnvironmentObject.GenerateElement(elementName, id, tags,
isFirstGenerated, themeBundleName, objectName, parentElement, isStatic).GetComponent<Custom2DShape>();
2026-04-03 10:53:11 -04:00
obj.spriteThemeBundleName = spriteThemeBundleName;
2026-03-31 07:51:40 -04:00
obj.spriteName = spriteName;
return obj;
}
2026-04-03 10:53:11 -04:00
public override void AfterInitialize()
2026-03-31 07:51:40 -04:00
{
UpdateSprite();
Refresh();
}
// 响应由 PropertyAnimation 触发的延迟刷新
public override void OnDirtyRefresh(Dictionary<string, bool> flags)
{
Refresh();
}
#endregion
#region [] Core Effect Logic
/// <summary>
/// 从当前 ThemeBundle 获取 Sprite 资源
/// </summary>
public void UpdateSprite()
{
if (spriteRenderer != null && !string.IsNullOrEmpty(spriteName) && spriteName != "None")
{
// 通过 ThemeBundleManager 获取 Sprite
2026-04-03 10:53:11 -04:00
Sprite sp = ThemeBundleManager.instance.GetObject<Sprite>(spriteThemeBundleName, spriteName);
if (sp == null)
{
// 如果没有找到 Sprite则从 Texture2D 列表中获取并动态创建 Sprite
Texture2D tex = ThemeBundleManager.instance.GetObject<Texture2D>(spriteThemeBundleName, spriteName);
if (tex != null)
{
sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
}
}
2026-03-31 07:51:40 -04:00
if (sp != null)
{
spriteRenderer.sprite = sp;
}
else
{
2026-04-03 10:53:11 -04:00
Debug.LogWarning($"[Custom2DShape] 无法在包 '{spriteThemeBundleName}' 中找到 Sprite/Texture: {spriteName}");
2026-03-31 07:51:40 -04:00
}
}
}
public override void Refresh()
{
// 同步 ColorSubmodule 的颜色状态
base.Refresh();
if (spriteRenderer != null)
{
spriteRenderer.color = colorSubmodule.currentBaseColor;
2026-04-03 10:53:11 -04:00
if (colorSubmodule.emissionEnabled)
{
// 如果启用了发光,尝试设置发光颜色(需要对应的 Shader 支持)
spriteRenderer.material.SetFloat("_EnableEmission", 1);
spriteRenderer.material.SetColor("_EmissionColor", colorSubmodule.GetCurrentEmissionColor());
}
2026-03-31 07:51:40 -04:00
}
}
#endregion
#region [] Inherited Logic overrides
// 如果有特定的子物体需要刷新逻辑,可在此重载
#endregion
}
namespace Beatmap
{
[System.Serializable]
public class Custom2DShape_BM : EnvironmentObject_BM
{
2026-04-03 10:53:11 -04:00
public string spriteThemeBundleName;
2026-03-31 07:51:40 -04:00
public string spriteName = "None";
public Custom2DShape_BM() { }
public Custom2DShape_BM(string elementName, Guid elementGuid, List<string> tags, GameElement_BM parentElement,
2026-04-03 10:53:11 -04:00
string themeBundleName, string objectName, bool isStatic, string spriteThemeBundleName, string spriteName)
2026-03-31 07:51:40 -04:00
: base(elementName, elementGuid, tags, parentElement, themeBundleName, objectName, isStatic)
{
2026-04-03 10:53:11 -04:00
this.spriteThemeBundleName = spriteThemeBundleName;
2026-03-31 07:51:40 -04:00
this.spriteName = spriteName;
}
public override void ExecuteBM()
{
matchedElement = Custom2DShape.GenerateElement(elementName, elementGuid, tags, false,
2026-04-03 10:53:11 -04:00
themeBundleName, objectName, GetElement(attachedElementGuid), isStatic, spriteThemeBundleName, spriteName);
2026-03-31 07:51:40 -04:00
}
}
}
}