Skip to content

Commit 1942d7f

Browse files
committed
Marking as dirty after applying changes
1 parent 42d4ea6 commit 1942d7f

10 files changed

+48
-74
lines changed

Assets/_PackageRoot/Editor/Scripts/ThemeDataEditor.cs

-37
This file was deleted.

Assets/_PackageRoot/Editor/Scripts/ThemeDataEditor.cs.meta

-11
This file was deleted.

Assets/_PackageRoot/Scripts/Binders/BaseColorBinder.Public.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public bool SetColor(ColorData colorData)
1616
return false;
1717
}
1818
data.colorGuid = colorData.Guid;
19+
SetDirty();
1920
var color = GetColor(colorData);
2021

2122
if (Theme.Instance?.debugLevel <= DebugLevel.Log)

Assets/_PackageRoot/Scripts/Binders/BaseColorBinder.cs

+32-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Linq;
1+
using System.Collections.Generic;
22
using UnityEngine;
33

44
namespace Unity.Theme.Binders
@@ -8,24 +8,28 @@ public abstract partial class BaseColorBinder : MonoBehaviour
88
{
99
[SerializeField] protected ColorBinderData data;
1010

11+
protected virtual IEnumerable<Object> ColorTargets { get; } = null;
12+
1113
protected virtual void Awake()
1214
{
1315
if (data == null)
1416
{
1517
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();
2122
}
2223
if (!data.IsConnected)
2324
{
2425
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);
2627
var colorData = Theme.Instance?.GetColorFirst();
2728
if (colorData != null)
29+
{
2830
data.colorGuid = colorData.Guid;
31+
SetDirty();
32+
}
2933
else
3034
{
3135
if (Theme.Instance?.debugLevel <= DebugLevel.Error)
@@ -47,6 +51,15 @@ protected virtual void OnDisable()
4751
Theme.Instance.onThemeChanged -= TrySetColor;
4852
Theme.Instance.onThemeColorChanged -= OnThemeColorChanged;
4953
}
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+
}
5063
protected virtual void TrySetColor(ThemeData theme)
5164
{
5265
if (theme == null)
@@ -74,18 +87,21 @@ protected virtual void TrySetColor(ThemeData theme)
7487
#if UNITY_EDITOR
7588
private void OnValidate()
7689
{
77-
// Attaching to first color
7890
if (string.IsNullOrEmpty(data.colorGuid))
7991
{
8092
if (Theme.Instance?.debugLevel <= DebugLevel.Error)
8193
Debug.LogError($"colorGuid is null at: <b>{GameObjectPath()}</b>. Taking the first one available.", gameObject);
94+
8295
data.colorGuid = Theme.Instance?.GetColorFirst().Guid;
96+
SetDirty();
8397
}
8498
if (!data.IsConnected)
8599
{
86100
if (Theme.Instance?.debugLevel <= DebugLevel.Error)
87101
Debug.LogError($"colorGuid='{data.colorGuid}' doesn't match to any existed colors at: <b>{GameObjectPath()}</b>. Taking the first one available.", gameObject);
102+
88103
data.colorGuid = Theme.Instance?.GetColorFirst().Guid;
104+
SetDirty();
89105
}
90106

91107
TrySetColor(Theme.Instance.CurrentTheme);
@@ -102,10 +118,16 @@ protected virtual Color GetColor(ColorData colorData)
102118
}
103119
protected abstract void SetColor(Color color);
104120

105-
private void OnThemeColorChanged(ThemeData themeData, ColorData colorData)
121+
private void SetDirty(Object obj)
106122
{
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
108129
}
130+
private void OnThemeColorChanged(ThemeData themeData, ColorData colorData) => TrySetColor(Theme.Instance.CurrentTheme);
109131
// UTILS ---------------------------------------------------------------------------//
110132
protected string GameObjectPath() => GameObjectPath(transform); //
111133
protected static string GameObjectPath(Transform trans, string path = "") //

Assets/_PackageRoot/Scripts/Binders/ImageColorBinder.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using UnityEngine;
23
using UnityEngine.UI;
34

@@ -8,6 +9,8 @@ public class ImageColorBinder : BaseColorBinder
89
{
910
[SerializeField] Image image;
1011

12+
protected override IEnumerable<Object> ColorTargets { get { yield return image; } }
13+
1114
protected override void Awake()
1215
{
1316
if (image == null) image = GetComponent<Image>();

Assets/_PackageRoot/Scripts/Binders/SpriteRendererColorBinder.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Generic;
12
using UnityEngine;
23

34
namespace Unity.Theme.Binders
@@ -7,6 +8,8 @@ public class SpriteRendererColorBinder : BaseColorBinder
78
{
89
[SerializeField] SpriteRenderer spriteRenderer;
910

11+
protected override IEnumerable<Object> ColorTargets { get { yield return spriteRenderer; } }
12+
1013
protected override void Awake()
1114
{
1215
if (spriteRenderer == null) spriteRenderer = GetComponent<SpriteRenderer>();

Assets/_PackageRoot/Scripts/Binders/TextMeshProColorBinder.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using UnityEngine;
22
using TMPro;
3+
using System.Collections.Generic;
34

45
namespace Unity.Theme.Binders
56
{
@@ -8,6 +9,8 @@ public class TextMeshProColorBinder : BaseColorBinder
89
{
910
[SerializeField] TextMeshProUGUI textMeshPro;
1011

12+
protected override IEnumerable<Object> ColorTargets { get { yield return textMeshPro; } }
13+
1114
protected override void Awake()
1215
{
1316
if (textMeshPro == null) textMeshPro = GetComponent<TextMeshProUGUI>();

Assets/_PackageRoot/Scripts/Config/Theme.Singleton.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ public static Theme Instance
1010
{
1111
get
1212
{
13-
if (instance == null)
14-
instance = GetOrCreateInstance();
13+
instance ??= GetOrCreateInstance();
1514

1615
if (instance == null)
1716
Debug.LogError("Theme instance is null");

Assets/_PackageRoot/Scripts/Config/Theme.Theme.cs

+5-12
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,12 @@ public string CurrentThemeName
3434

3535
public ThemeData AddTheme(string themeName, bool setCurrent = false)
3636
{
37-
List<ColorData> colors;
38-
if (themes.Count > 0)
39-
{
40-
colors = themes[0].colors
37+
var colors = themes.Count == 0
38+
? new List<ColorData>()
39+
: themes[0].colors
4140
.Select(c => new ColorData(c))
4241
.ToList();
43-
}
44-
else
45-
{
46-
colors = new List<ColorData>();
47-
}
42+
4843
var theme = new ThemeData(Guid.NewGuid().ToString())
4944
{
5045
themeName = themeName,
@@ -59,9 +54,7 @@ public ThemeData AddTheme(string themeName, bool setCurrent = false)
5954
}
6055
public ThemeData SetOrAddTheme(string themeName, bool setCurrent = false)
6156
{
62-
var theme = themes.FirstOrDefault(x => x.themeName == themeName);
63-
if (theme == null)
64-
theme = AddTheme(themeName, setCurrent);
57+
var theme = themes.FirstOrDefault(x => x.themeName == themeName) ?? AddTheme(themeName, setCurrent);
6558
if (setCurrent)
6659
CurrentThemeIndex = themes.IndexOf(theme);
6760
return theme;

Assets/_PackageRoot/Scripts/Config/Theme.Tools.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Linq;
42
using UnityEngine;
53

0 commit comments

Comments
 (0)