feat: Allow specifying a preset as the target for UIEffectReplica

This commit is contained in:
mob-sakai
2025-03-16 13:33:05 +09:00
parent def49c34f5
commit cb0e8d283b
10 changed files with 116 additions and 32 deletions
+33 -1
View File
@@ -1,4 +1,5 @@
using UnityEditor; using UnityEditor;
using UnityEngine;
namespace Coffee.UIEffects.Editors namespace Coffee.UIEffects.Editors
{ {
@@ -14,6 +15,7 @@ namespace Coffee.UIEffects.Editors
private SerializedProperty _samplingScale; private SerializedProperty _samplingScale;
private SerializedProperty _allowToModifyMeshShape; private SerializedProperty _allowToModifyMeshShape;
private SerializedProperty _flip; private SerializedProperty _flip;
private SerializedProperty _customRoot;
private Editor _uiEffectEditor; private Editor _uiEffectEditor;
private void OnEnable() private void OnEnable()
@@ -23,13 +25,43 @@ namespace Coffee.UIEffects.Editors
_samplingScale = serializedObject.FindProperty("m_SamplingScale"); _samplingScale = serializedObject.FindProperty("m_SamplingScale");
_allowToModifyMeshShape = serializedObject.FindProperty("m_AllowToModifyMeshShape"); _allowToModifyMeshShape = serializedObject.FindProperty("m_AllowToModifyMeshShape");
_flip = serializedObject.FindProperty("m_Flip"); _flip = serializedObject.FindProperty("m_Flip");
_customRoot = serializedObject.FindProperty("m_CustomRoot");
} }
public override void OnInspectorGUI() public override void OnInspectorGUI()
{ {
serializedObject.Update(); serializedObject.Update();
EditorGUILayout.PropertyField(_target);
var r = EditorGUILayout.GetControlRect();
r.width -= 60;
EditorGUI.PropertyField(r, _target);
// Preset button.
r.x += r.width;
r.width = 60;
if (GUI.Button(r, EditorGUIUtility.TrTempContent("Preset"), "MiniPopup"))
{
UIEffectEditor.DropDownPreset(r, x => !x.builtin, p =>
{
_target.objectReferenceValue = p;
_target.serializedObject.ApplyModifiedProperties();
});
}
// Preset mode warning.
if (_target.objectReferenceValue is UIEffect targetEffect && !targetEffect.gameObject.scene.IsValid())
{
r = EditorGUILayout.GetControlRect();
r.width -= EditorGUIUtility.labelWidth;
r.x += EditorGUIUtility.labelWidth;
EditorGUI.HelpBox(r, "The target effect is not in the scene. (Preset mode)", MessageType.Warning);
}
EditorGUILayout.PropertyField(_useTargetTransform); EditorGUILayout.PropertyField(_useTargetTransform);
EditorGUI.BeginDisabledGroup(_useTargetTransform.boolValue);
EditorGUILayout.PropertyField(_customRoot);
EditorGUI.EndDisabledGroup();
EditorGUILayout.PropertyField(_samplingScale); EditorGUILayout.PropertyField(_samplingScale);
EditorGUILayout.PropertyField(_allowToModifyMeshShape); EditorGUILayout.PropertyField(_allowToModifyMeshShape);
EditorGUILayout.PropertyField(_flip); EditorGUILayout.PropertyField(_flip);
+1 -1
View File
@@ -1339,7 +1339,7 @@ namespace Coffee.UIEffects
SetVerticesDirty(); SetVerticesDirty();
} }
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
c.toneFilter = m_ToneFilter; c.toneFilter = m_ToneFilter;
c.toneIntensity = m_ToneIntensity; c.toneIntensity = m_ToneIntensity;
+1 -1
View File
@@ -211,7 +211,7 @@ namespace Coffee.UIEffects
MaterialRepository.Release(ref _material); MaterialRepository.Release(ref _material);
} }
protected abstract void UpdateContext(UIEffectContext c); internal abstract void UpdateContext(UIEffectContext c);
public virtual void ApplyContextToMaterial(Material material) public virtual void ApplyContextToMaterial(Material material)
{ {
+53 -11
View File
@@ -16,6 +16,9 @@ namespace Coffee.UIEffects
[SerializeField] [SerializeField]
protected bool m_AllowToModifyMeshShape = true; protected bool m_AllowToModifyMeshShape = true;
[SerializeField]
protected RectTransform m_CustomRoot = null;
[SerializeField] [SerializeField]
protected Flip m_Flip = 0; protected Flip m_Flip = 0;
@@ -45,6 +48,17 @@ namespace Coffee.UIEffects
} }
} }
public RectTransform customRoot
{
get => m_CustomRoot;
set
{
if (m_CustomRoot == value) return;
m_CustomRoot = value;
SetVerticesDirty();
}
}
public float samplingScale public float samplingScale
{ {
get => m_SamplingScale; get => m_SamplingScale;
@@ -84,11 +98,25 @@ namespace Coffee.UIEffects
public override bool canModifyShape => m_AllowToModifyMeshShape; public override bool canModifyShape => m_AllowToModifyMeshShape;
public override uint effectId => target ? target.effectId : 0; public override uint effectId => target ? target.effectId : 0;
public override UIEffectContext context => target && target.isActiveAndEnabled ? target.context : null;
public override RectTransform transitionRoot => useTargetTransform && target public override UIEffectContext context
{
get
{
if (!target) return null;
if (!isTargetInScene) return base.context;
return target.isActiveAndEnabled ? target.context : null;
}
}
public override RectTransform transitionRoot => useTargetTransform && isTargetInScene
? target.transitionRoot ? target.transitionRoot
: transform as RectTransform; : m_CustomRoot
? m_CustomRoot
: transform as RectTransform;
private bool isTargetInScene => target && target.gameObject.scene.IsValid();
protected override void OnEnable() protected override void OnEnable()
{ {
@@ -117,7 +145,7 @@ namespace Coffee.UIEffects
internal override void SetEnablePreviewIfSelected(GameObject[] selection) internal override void SetEnablePreviewIfSelected(GameObject[] selection)
{ {
if (!target) return; if (!isTargetInScene) return;
target.SetEnablePreviewIfSelected(selection); target.SetEnablePreviewIfSelected(selection);
} }
#endif #endif
@@ -130,10 +158,17 @@ namespace Coffee.UIEffects
_currentTarget.replicas.Remove(this); _currentTarget.replicas.Remove(this);
} }
_currentTarget = newTarget; if (newTarget)
if (_currentTarget)
{ {
_currentTarget.replicas.Add(this); _currentTarget = newTarget;
if (isTargetInScene)
{
_currentTarget.replicas.Add(this);
}
}
else
{
_currentTarget = null;
} }
} }
@@ -141,19 +176,24 @@ namespace Coffee.UIEffects
{ {
base.OnBeforeCanvasRebuild(); base.OnBeforeCanvasRebuild();
if (useTargetTransform && target && CheckTransformChangedWith(target.transform)) if (useTargetTransform && isTargetInScene && CheckTransformChangedWith(target.transform))
{ {
SetVerticesDirty(); SetVerticesDirty();
} }
} }
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
if (target && !isTargetInScene)
{
target.UpdateContext(c);
}
} }
public override void ApplyContextToMaterial(Material material) public override void ApplyContextToMaterial(Material material)
{ {
if (!isActiveAndEnabled || !target || !target.isActiveAndEnabled) return; if (!isActiveAndEnabled || !target) return;
if (isTargetInScene && !target.isActiveAndEnabled) return;
base.ApplyContextToMaterial(material); base.ApplyContextToMaterial(material);
} }
@@ -164,7 +204,9 @@ namespace Coffee.UIEffects
public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera) public override bool IsRaycastLocationValid(Vector2 sp, Camera eventCamera)
{ {
return !target || target.IsRaycastLocationValid(sp, eventCamera); if (!isActiveAndEnabled || !isTargetInScene) return true;
return target.IsRaycastLocationValid(sp, eventCamera);
} }
} }
} }
+23 -13
View File
@@ -219,11 +219,6 @@ namespace Coffee.UIEffects
s_RegisteredInstances.Add(textMeshProUGUI); s_RegisteredInstances.Add(textMeshProUGUI);
var effect = textMeshProUGUI.GetComponent<UIEffectBase>(); var effect = textMeshProUGUI.GetComponent<UIEffectBase>();
var target = effect is UIEffect uiEffect
? uiEffect
: effect is UIEffectReplica parentReplica
? parentReplica.target
: null;
var subMeshes = InternalListPool<TMP_SubMeshUI>.Rent(); var subMeshes = InternalListPool<TMP_SubMeshUI>.Rent();
var modifiers = InternalListPool<IMeshModifier>.Rent(); var modifiers = InternalListPool<IMeshModifier>.Rent();
var subModifiers = InternalListPool<IMeshModifier>.Rent(); var subModifiers = InternalListPool<IMeshModifier>.Rent();
@@ -256,16 +251,31 @@ namespace Coffee.UIEffects
var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, i - 1); var subMeshUI = GetSubMeshUI(subMeshes, meshInfo.material, i - 1);
if (!subMeshUI) break; if (!subMeshUI) break;
if (target) var replica = subMeshUI.GetOrAddComponent<UIEffectReplica>();
if (effect is UIEffect pEffect && pEffect.isActiveAndEnabled)
{ {
var replica = subMeshUI.GetOrAddComponent<UIEffectReplica>(); replica.target = pEffect;
replica.target = target; replica.useTargetTransform = true;
replica.customRoot = null;
replica.flip = pEffect.flip;
}
else if (effect is UIEffectReplica pReplica && pReplica.isActiveAndEnabled)
{
replica.target = pReplica.target;
replica.useTargetTransform = pReplica.useTargetTransform;
replica.customRoot = pReplica.customRoot;
replica.flip = pReplica.flip;
}
else
{
replica.target = null;
replica.customRoot = null;
}
subMeshUI.GetComponents(subModifiers); subMeshUI.GetComponents(subModifiers);
foreach (var modifier in subModifiers) foreach (var modifier in subModifiers)
{ {
modifier.ModifyMesh(s_VertexHelper); modifier.ModifyMesh(s_VertexHelper);
}
} }
s_VertexHelper.FillMesh(s_Mesh); s_VertexHelper.FillMesh(s_Mesh);
@@ -211,7 +211,7 @@ namespace Coffee.UIEffects
effectPlayer.OnDisable(); effectPlayer.OnDisable();
} }
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
c.transitionFilter = TransitionFilter.Dissolve; c.transitionFilter = TransitionFilter.Dissolve;
c.transitionRate = m_EffectFactor; c.transitionRate = m_EffectFactor;
@@ -181,7 +181,7 @@ namespace Coffee.UIEffects
} }
#endif #endif
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
(c.toneFilter, c.samplingFilter) = (m_EffectMode, m_BlurMode).Convert(); (c.toneFilter, c.samplingFilter) = (m_EffectMode, m_BlurMode).Convert();
if (context.samplingFilter == SamplingFilter.Pixelation) if (context.samplingFilter == SamplingFilter.Pixelation)
@@ -130,7 +130,7 @@ namespace Coffee.UIEffects
return true; return true;
} }
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
c.colorFilter = ColorFilter.HsvModifier; c.colorFilter = ColorFilter.HsvModifier;
c.colorIntensity = 1; c.colorIntensity = 1;
@@ -166,7 +166,7 @@ namespace Coffee.UIEffects
effectPlayer.Stop(reset); effectPlayer.Stop(reset);
} }
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
c.transitionFilter = TransitionFilter.Shiny; c.transitionFilter = TransitionFilter.Shiny;
c.transitionRate = m_EffectFactor; c.transitionRate = m_EffectFactor;
@@ -237,7 +237,7 @@ namespace Coffee.UIEffects
return !passRayOnHidden || effectFactor < 1; return !passRayOnHidden || effectFactor < 1;
} }
protected override void UpdateContext(UIEffectContext c) internal override void UpdateContext(UIEffectContext c)
{ {
c.transitionFilter = m_EffectMode.Convert(); c.transitionFilter = m_EffectMode.Convert();
c.transitionRate = m_EffectFactor; c.transitionRate = m_EffectFactor;