@@ -12,24 +12,22 @@ namespace BP.TextMotion
1212 [ CreateAssetMenu ( fileName = "NewMotionProfile" , menuName = "TextMotionPro/MotionProfile" ) ]
1313 public class MotionProfile : ScriptableObject
1414 {
15+ [ SerializeField ] private List < TextEffect > textEffects = new ( ) ;
16+ private readonly Dictionary < string , TextEffect > cache = new ( ) ;
17+
1518 /// <summary>
16- /// List of all text effects in this profile .
19+ /// Invoked whenever the text effects list changes (added, removed, etc) .
1720 /// </summary>
18- [ Tooltip ( "Collection of text effects available in this profile" ) ]
19- public List < TextEffect > textEffects = new ( ) ;
20-
21- private readonly Dictionary < string , TextEffect > tagEffectCache = new ( ) ;
22- [ NonSerialized ] public bool isDirty = false ;
21+ public event Action TextEffectsChanged ;
2322
23+ /// <summary>
24+ /// Invoked whenever the text effects list changes (added, removed, etc).
25+ /// </summary>
2426 private void OnEnable ( )
2527 {
26- tagEffectCache . Clear ( ) ;
28+ cache . Clear ( ) ;
2729 textEffects . RemoveAll ( x => x == null ) ;
2830 }
29- public void Reset ( )
30- {
31- isDirty = true ;
32- }
3331
3432 /// <summary>
3533 /// Adds a new text effect of the specified type to the profile.
@@ -62,7 +60,7 @@ public TextEffect AddTextEffect(Type type)
6260
6361 // Add to effects list and mark as dirty
6462 textEffects . Add ( component ) ;
65- isDirty = true ;
63+ TextEffectsChanged ? . Invoke ( ) ;
6664 return component ;
6765 }
6866
@@ -78,8 +76,17 @@ public TextEffect AddTextEffect(Type type)
7876 /// <param name="type">The type of text effect to remove.</param>
7977 public void RemoveTextEffect ( Type type )
8078 {
81- textEffects . RemoveAll ( x => x . GetType ( ) . Equals ( type ) ) ;
82- isDirty = true ;
79+ var textEffectsToRemove = textEffects . Where ( x => x . GetType ( ) . Equals ( type ) ) . ToList ( ) ;
80+ foreach ( var effect in textEffectsToRemove )
81+ {
82+ textEffects . Remove ( effect ) ;
83+ if ( cache . ContainsKey ( effect . Tag ) )
84+ {
85+ cache . Remove ( effect . Tag ) ;
86+ }
87+ }
88+ if ( textEffectsToRemove . Count > 0 )
89+ TextEffectsChanged ? . Invoke ( ) ;
8390 }
8491
8592 /// <summary>
@@ -104,10 +111,10 @@ public void RemoveTextEffect(Type type)
104111 public bool HasTextEffectWithTag ( string tag )
105112 {
106113 // Check cache first for performance
107- if ( tagEffectCache . TryGetValue ( tag , out var cachedEffect ) )
114+ if ( cache . TryGetValue ( tag , out var cachedEffect ) )
108115 return cachedEffect != null ;
109116
110- return textEffects . Any ( x => x . EffectTag == tag ) ;
117+ return textEffects . Any ( x => x . Tag == tag ) ;
111118 }
112119
113120 public bool TryGetTextEffectWithTag ( string tag , out TextEffect textEffect )
@@ -131,13 +138,13 @@ public bool TryGetTextEffectWithTag(string tag, out TextEffect textEffect)
131138 public TextEffect GetTextEffectWithTag ( string tag )
132139 {
133140 // Looks up the tag in cache
134- if ( tagEffectCache . TryGetValue ( tag , out var cached ) )
141+ if ( cache . TryGetValue ( tag , out var cached ) )
135142 return cached ;
136143
137144 // Find effect and cache it
138- var effect = textEffects . FirstOrDefault ( x => x . EffectTag == tag ) ;
145+ var effect = textEffects . FirstOrDefault ( x => x . Tag == tag ) ;
139146 if ( effect != null )
140- tagEffectCache [ tag ] = effect ;
147+ cache [ tag ] = effect ;
141148
142149 return effect ;
143150 }
0 commit comments