Skip to content

Commit

Permalink
Adjust
Browse files Browse the repository at this point in the history
  • Loading branch information
piti6 committed Feb 18, 2021
1 parent 6858891 commit 691cdf2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
7 changes: 7 additions & 0 deletions Scripts/InternalBridge/Data/Mutable/MutableWindowStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace UniSkin
internal class MutableWindowStyle
{
public string Name { get; set; }

public IEnumerable<string> CustomBackgroundIds()
{
yield return CustomBackgroundId;
yield return CustomBackgroundId2;
}

public string CustomBackgroundId { get; set; }
public string CustomBackgroundId2 { get; set; }
public Dictionary<string, MutableElementStyle> ElementStyles { get; set; }
Expand Down
59 changes: 39 additions & 20 deletions Scripts/InternalBridge/SkinEditorWindow/SkinEditorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
using UnityEditor;
using UnityEngine;

#if UNITY_2019_1_OR_NEWER
#else
using UnityEngine.Experimental.UIElements;
#endif

namespace UniSkin.UI
{
internal class SkinEditorWindow : EditorWindow
Expand Down Expand Up @@ -84,7 +79,6 @@ private void OnChangeCustomBackground(bool primary, string id, SerializableTextu
}

hasUnsavedChanges = true;
RemoveUnusedTextures();
UpdateCachedSkin(true);
_inspectedViewChunk.InspectedView.Repaint();
}
Expand Down Expand Up @@ -191,8 +185,6 @@ private void OnPropertyModify(bool colorChanged, PropertyModifyData modifyData)
_currentSkin.Textures[addedTexture.Id] = addedTexture;
}

RemoveUnusedTextures();

hasUnsavedChanges = true;

var recordUndo = false;
Expand All @@ -212,19 +204,30 @@ private void OnPropertyModify(bool colorChanged, PropertyModifyData modifyData)
_inspectedViewChunk.InspectedView?.Repaint();
}

private void RemoveUnusedTextures()
private readonly List<string> _disposeTargetKeys = new List<string>();

private void RemoveUnusedTextures(MutableSkin targetSkin)
{
// Remove Unused textures
var wholeTextureIds = _currentSkin.WindowStyles.Values
var customBackgroundTextureIds = targetSkin.WindowStyles.Values
.SelectMany(x => x.CustomBackgroundIds());

var styleBackgroundTextureIds = targetSkin.WindowStyles.Values
.SelectMany(x => x.ElementStyles.Values)
.SelectMany(x => x.StyleStates.Values)
.Select(x => x.BackgroundTextureId)
.Distinct();
.Select(x => x.BackgroundTextureId);

foreach (var textureId in wholeTextureIds.Where(x => !string.IsNullOrEmpty(x)).Where(x => !_currentSkin.Textures.ContainsKey(x)))
var wholeTextureIds = new HashSet<string>(Enumerable.Concat(customBackgroundTextureIds, styleBackgroundTextureIds));

var disposableTargets = targetSkin.Textures.Keys.Where(x => !wholeTextureIds.Contains(x));

_disposeTargetKeys.AddRange(disposableTargets);

foreach (var disposeTargetKey in _disposeTargetKeys)
{
_currentSkin.Textures.Remove(textureId);
targetSkin.Textures.Remove(disposeTargetKey);
}

_disposeTargetKeys.Clear();
}

private double _lastColorChangedSeconds;
Expand Down Expand Up @@ -311,19 +314,24 @@ private void UpdateCurrentSkin(Skin targetSkin)
#if UNITY_2020_2_OR_NEWER
public override void SaveChanges()
{
Save(_currentSkin.ToImmutable(grantNewId: false));
Save(_currentSkin);
}
#else
public void SaveChanges()
private void SaveChanges()
{
Save(_currentSkin.ToImmutable(grantNewId: false));
Save(_currentSkin);
}
#endif

private void Save(Skin skin)
private void Save(MutableSkin skin)
{
Undo.RecordObject(CachedSkin.instance, "CachedSkin");
RemoveUnusedTextures(skin);

Save(skin.ToImmutable(grantNewId: false));
}

private void Save(Skin skin)
{
CachedSkin.Update(skin);
CachedSkin.Save();

Expand Down Expand Up @@ -357,10 +365,21 @@ private void OnDisable()

GUIViewDebuggerHelper.onViewInstructionsChanged -= OnViewInstructionsChanged;

#if !UNITY_2020_2_OR_NEWER
if (hasUnsavedChanges && EditorUtility.DisplayDialog("Warning", saveChangesMessage, "Yes", "No"))
{
SaveChanges();
}
else
{
Save(_currentOriginalSkin);
}
#else
if (hasUnsavedChanges)
{
Save(_currentOriginalSkin);
}
#endif
}
}
}

0 comments on commit 691cdf2

Please sign in to comment.