1
- using System . Linq ;
1
+ using System . Collections . Generic ;
2
2
using UnityEngine ;
3
3
4
4
namespace Unity . Theme . Binders
@@ -8,24 +8,28 @@ public abstract partial class BaseColorBinder : MonoBehaviour
8
8
{
9
9
[ SerializeField ] protected ColorBinderData data ;
10
10
11
+ protected virtual IEnumerable < Object > ColorTargets { get ; } = null ;
12
+
11
13
protected virtual void Awake ( )
12
14
{
13
15
if ( data == null )
14
16
{
15
17
if ( Theme . Instance ? . debugLevel <= DebugLevel . Error )
16
- Debug . LogError ( $ "ColorBinderData is null at gameObject { name } ", gameObject ) ;
17
- data = new ColorBinderData ( )
18
- {
19
- colorGuid = Theme . Instance ? . GetColorFirst ( ) . Guid
20
- } ;
18
+ Debug . LogError ( $ "ColorBinderData is null at <b>{ GameObjectPath ( ) } </b>, replacing it by first color", gameObject ) ;
19
+
20
+ data = new ColorBinderData ( ) { colorGuid = Theme . Instance ? . GetColorFirst ( ) . Guid } ;
21
+ SetDirty ( ) ;
21
22
}
22
23
if ( ! data . IsConnected )
23
24
{
24
25
if ( Theme . Instance ? . debugLevel <= DebugLevel . Error )
25
- Debug . LogError ( $ "Color not found in database. Guid={ data . colorGuid } ", gameObject ) ;
26
+ Debug . LogError ( $ "Color not found in database at <b> { GameObjectPath ( ) } </b> Guid={ data . colorGuid } ", gameObject ) ;
26
27
var colorData = Theme . Instance ? . GetColorFirst ( ) ;
27
28
if ( colorData != null )
29
+ {
28
30
data . colorGuid = colorData . Guid ;
31
+ SetDirty ( ) ;
32
+ }
29
33
else
30
34
{
31
35
if ( Theme . Instance ? . debugLevel <= DebugLevel . Error )
@@ -47,6 +51,15 @@ protected virtual void OnDisable()
47
51
Theme . Instance . onThemeChanged -= TrySetColor ;
48
52
Theme . Instance . onThemeColorChanged -= OnThemeColorChanged ;
49
53
}
54
+ protected virtual void SetDirty ( )
55
+ {
56
+ SetDirty ( this ) ;
57
+ if ( ColorTargets != null )
58
+ {
59
+ foreach ( var target in ColorTargets )
60
+ SetDirty ( target ) ;
61
+ }
62
+ }
50
63
protected virtual void TrySetColor ( ThemeData theme )
51
64
{
52
65
if ( theme == null )
@@ -74,18 +87,21 @@ protected virtual void TrySetColor(ThemeData theme)
74
87
#if UNITY_EDITOR
75
88
private void OnValidate ( )
76
89
{
77
- // Attaching to first color
78
90
if ( string . IsNullOrEmpty ( data . colorGuid ) )
79
91
{
80
92
if ( Theme . Instance ? . debugLevel <= DebugLevel . Error )
81
93
Debug . LogError ( $ "colorGuid is null at: <b>{ GameObjectPath ( ) } </b>. Taking the first one available.", gameObject ) ;
94
+
82
95
data . colorGuid = Theme . Instance ? . GetColorFirst ( ) . Guid ;
96
+ SetDirty ( ) ;
83
97
}
84
98
if ( ! data . IsConnected )
85
99
{
86
100
if ( Theme . Instance ? . debugLevel <= DebugLevel . Error )
87
101
Debug . LogError ( $ "colorGuid='{ data . colorGuid } ' doesn't match to any existed colors at: <b>{ GameObjectPath ( ) } </b>. Taking the first one available.", gameObject ) ;
102
+
88
103
data . colorGuid = Theme . Instance ? . GetColorFirst ( ) . Guid ;
104
+ SetDirty ( ) ;
89
105
}
90
106
91
107
TrySetColor ( Theme . Instance . CurrentTheme ) ;
@@ -102,10 +118,16 @@ protected virtual Color GetColor(ColorData colorData)
102
118
}
103
119
protected abstract void SetColor ( Color color ) ;
104
120
105
- private void OnThemeColorChanged ( ThemeData themeData , ColorData colorData )
121
+ private void SetDirty ( Object obj )
106
122
{
107
- TrySetColor ( Theme . Instance . CurrentTheme ) ;
123
+ #if UNITY_EDITOR
124
+ if ( UnityEditor . PrefabUtility . IsPartOfAnyPrefab ( obj ) )
125
+ UnityEditor . PrefabUtility . RecordPrefabInstancePropertyModifications ( obj ) ;
126
+ else
127
+ UnityEditor . EditorUtility . SetDirty ( obj ) ;
128
+ #endif
108
129
}
130
+ private void OnThemeColorChanged ( ThemeData themeData , ColorData colorData ) => TrySetColor ( Theme . Instance . CurrentTheme ) ;
109
131
// UTILS ---------------------------------------------------------------------------//
110
132
protected string GameObjectPath ( ) => GameObjectPath ( transform ) ; //
111
133
protected static string GameObjectPath ( Transform trans , string path = "" ) //
0 commit comments