Skip to content

Commit

Permalink
Merge pull request #9 from badawe/feature/tweaks_and_optimizations
Browse files Browse the repository at this point in the history
Feature/tweaks and optimizations
  • Loading branch information
brunomikoski authored May 16, 2020
2 parents 74019c6 + 7a5531e commit b9dee23
Show file tree
Hide file tree
Showing 22 changed files with 788 additions and 150 deletions.
23 changes: 22 additions & 1 deletion CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.2.0] - 23-05-2020
### Added
- Selection tools
- Batch actions
- Select all objects on project view
- Move selected sprites to Atlas
- Remove sprite from atlas
- Added option to tweak the framerate of the capture (for heavy projects)
- Added option to manually capture frames on demand

### Changed
- Fixed issues while loading all sprites from Atlas
- Fixed issues where result view would not update properly after changes
- Fixed size detection issues
- Fixed better handling of dirty management
- Fixed issue when tweaking allowed size variation was not been updated
- Fixed an issue related to ReflectionTypeLoadException


## [0.1.0] - 10-05-2020

### Added
Expand All @@ -25,9 +45,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Basic implementation

## [Unreleased]
- Test devices builds to make sure its not adding anything to the project.
-


[0.2.0]: https://github.com/badawe/SpriteAuditor/releases/tag/v0.2.0
[0.1.0]: https://github.com/badawe/SpriteAuditor/releases/tag/v0.1.0
[0.0.1]: https://github.com/badawe/SpriteAuditor/releases/tag/v0.0.1

Expand Down
34 changes: 34 additions & 0 deletions Scripts/Editor/Extensions/DefaultAssetExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;

namespace UnityEngine
{
public static partial class DefaultAssetExtensions
{
public static List<T> GetChildrenObjectsOfType<T>(this DefaultAsset targetDefaultAsset) where T : Object
{
string targetFolder = AssetDatabase.GetAssetPath(targetDefaultAsset);
List<T> result = new List<T>();

string[] guids = AssetDatabase.FindAssets($"t:{typeof(T).Name}", new[] {targetFolder});
Uri rootFolder = new Uri(Path.GetFullPath(targetFolder));
for (int i = 0; i < guids.Length; i++)
{
string objectPath = AssetDatabase.GUIDToAssetPath(guids[i]);
Uri spriteUri = new Uri(Path.GetFullPath(objectPath));

if (rootFolder != spriteUri && rootFolder.IsBaseOf(spriteUri))
{
IEnumerable<T> foundObjects = AssetDatabase.LoadAllAssetsAtPath(objectPath).Where(o => o is T)
.Cast<T>();
result.AddRange(foundObjects);
}
}

return result;
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/Extensions/DefaultAssetExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.U2D;

namespace UnityEngine.U2D
{
public static partial class EditorSpriteAtlasExtensions
public static partial class SpriteAtlasExtensions
{
public static float GetVariantScale(this SpriteAtlas spriteAtlas)
{
Expand Down Expand Up @@ -56,40 +55,22 @@ public static List<Sprite> GetAllSprites(this SpriteAtlas spriteAtlas)

if (packable is DefaultAsset defaultAsset)
{
List<Sprite> sprites = GetAllSpritesFromFolder(AssetDatabase.GetAssetPath(defaultAsset));
resultSprites.AddRange(sprites);
resultSprites.AddRange(defaultAsset.GetChildrenObjectsOfType<Sprite>());
}
else if (packable is Sprite || packable is Texture2D)
else if (packable is Sprite sprite)
{
resultSprites.Add(sprite);
}
else if (packable is Texture2D)
{
string path = AssetDatabase.GetAssetPath(packable);

resultSprites.AddRange(AssetDatabase.LoadAllAssetsAtPath(path).Where(o => o is Sprite)
.Cast<Sprite>().ToArray());
}
}

return resultSprites;
}

private static List<Sprite> GetAllSpritesFromFolder(string targetFolder)
{
List<Sprite> result = new List<Sprite>();
string[] spritesGUIDs = AssetDatabase.FindAssets("t:Sprite", new[] {targetFolder});
Uri rootFolder = new Uri(Path.GetFullPath(targetFolder));
for (int i = 0; i < spritesGUIDs.Length; i++)
{
string spritesGUID = spritesGUIDs[i];
string spritePath = AssetDatabase.GUIDToAssetPath(spritesGUID);
Uri spriteUri = new Uri(Path.GetFullPath(spritePath));

if (rootFolder != spriteUri && rootFolder.IsBaseOf(spriteUri))
{
Sprite[] allSprites = AssetDatabase.LoadAllAssetsAtPath(spritePath).Where(o => o is Sprite)
.Cast<Sprite>().ToArray();
result.AddRange(allSprites);
}
}

return result;
}
}
}
17 changes: 17 additions & 0 deletions Scripts/Editor/Extensions/Texture2DExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Linq;
using UnityEditor;

namespace UnityEngine
{
public static partial class Texture2DExtensions
{
public static bool TryLoadSprites(this Texture2D texture, out Sprite[] sprites )
{
string texturePath = AssetDatabase.GetAssetPath(texture);
sprites = AssetDatabase.LoadAllAssetsAtPath(texturePath)
.Where(o => o is Sprite).Cast<Sprite>().ToArray();

return sprites.Length > 0;
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/Extensions/Texture2DExtensions.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Scripts/Editor/Results/AtlasResultView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.U2D;
using Object = System.Object;

namespace BrunoMikoski.SpriteAuditor
{
Expand Down Expand Up @@ -91,6 +92,8 @@ protected override void DrawResultsInternal(SpriteDatabase spriteDatabase)
DrawSpriteDataField(spriteData);
}

SpriteAuditorUtility.DrawDefaultSelectionOptions(atlasToNotUsedSprites[atlas]);

EditorGUI.indentLevel--;
}
}
Expand All @@ -108,11 +111,12 @@ protected override void DrawResultsInternal(SpriteDatabase spriteDatabase)
Sprite sprite = atlasToNotUsedSprites[atlas][j];
if (sprite == null)
continue;

SpriteAuditorGUIUtility.DrawObjectFoldout(sprite,
$"{VisualizationType.Atlas.ToString()}_{atlas.name}_{sprite.name}", false);
$"{VisualizationType.Atlas.ToString()}_{atlas.name}_{sprite.name}", false, true);
}

SpriteAuditorUtility.DrawDefaultSelectionOptions(atlasToNotUsedSprites[atlas]);
EditorGUI.indentLevel--;
}
}
Expand Down
21 changes: 1 addition & 20 deletions Scripts/Editor/Results/BaseResultView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace BrunoMikoski.SpriteAuditor
{
public abstract class BaseResultView
{
private SearchField searchField;
private Vector2 scrollPosition = Vector2.zero;

public abstract void DrawFilterOptions();
Expand All @@ -20,7 +19,6 @@ public abstract class BaseResultView

public void DrawResults(SpriteDatabase spriteDatabase)
{
DrawSearch();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, false, false);

DrawResultsInternal(spriteDatabase);
Expand All @@ -45,27 +43,10 @@ protected bool MatchSearch(string name)
return false;
}

private void DrawSearch()
{
Rect searchRect =
GUILayoutUtility.GetRect(1, 1, 18, 18, GUILayout.ExpandWidth(true));

if (searchField == null)
searchField = new SearchField();

EditorGUI.BeginChangeCheck();
string searchText = searchField.OnGUI(searchRect, SpriteAuditorUtility.SearchText);
if (EditorGUI.EndChangeCheck())
{
SpriteAuditorUtility.SearchText = searchText;
}
EditorGUILayout.Separator();
}

protected virtual void DrawSpriteDataField(SpriteData spriteData)
{
EditorGUILayout.BeginVertical("Box");
if (SpriteAuditorGUIUtility.DrawObjectFoldout(spriteData.Sprite, spriteData.Sprite.name))
if (SpriteAuditorGUIUtility.DrawObjectFoldout(spriteData.Sprite, spriteData.Sprite.name, true, true))
{
EditorGUI.indentLevel++;

Expand Down
31 changes: 19 additions & 12 deletions Scripts/Editor/Results/SceneResultView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine.U2D;
using Object = UnityEngine.Object;

namespace BrunoMikoski.SpriteAuditor
{
Expand Down Expand Up @@ -69,13 +71,13 @@ private bool MatchFilter(SpriteData data)

if (currentFilter.HasFlag(Filter.SingleSprites))
{
if (data.TextureImporter.spriteImportMode != SpriteImportMode.Single)
if (data.TextureImporter == null || data.TextureImporter.spriteImportMode != SpriteImportMode.Single)
return false;
}

if (currentFilter.HasFlag(Filter.MultipleSprites))
{
if (data.TextureImporter.spriteImportMode != SpriteImportMode.Multiple)
if (data.TextureImporter == null || data.TextureImporter.spriteImportMode != SpriteImportMode.Multiple)
return false;
}

Expand Down Expand Up @@ -200,9 +202,10 @@ protected override void DrawResultsInternal(SpriteDatabase spriteDatabase)
DrawSpriteDataField(spriteData);
}

SpriteAuditorUtility.DrawDefaultSelectionOptions(sceneToSingleSprites[sceneAsset]
.Select(spriteData => spriteData.Sprite).Cast<Object>().ToList());
EditorGUI.indentLevel--;
}

EditorGUILayout.EndVertical();
}

Expand All @@ -212,19 +215,23 @@ protected override void DrawResultsInternal(SpriteDatabase spriteDatabase)
{
EditorGUILayout.BeginVertical("Box");

if (SpriteAuditorGUIUtility.DrawObjectFoldout(atlasToUSedSprites.Key,
$"{VisualizationType.Scene.ToString()}_{atlasToUSedSprites.Key}"))
{
EditorGUI.indentLevel++;
foreach (SpriteData spriteData in sceneToAtlasToUsedSprites[sceneAsset][
atlasToUSedSprites.Key])
if (SpriteAuditorGUIUtility.DrawObjectFoldout(atlasToUSedSprites.Key,
$"{VisualizationType.Scene.ToString()}_{atlasToUSedSprites.Key}"))
{
DrawSpriteDataField(spriteData);
EditorGUI.indentLevel++;
foreach (SpriteData spriteData in sceneToAtlasToUsedSprites[sceneAsset][
atlasToUSedSprites.Key])
{
DrawSpriteDataField(spriteData);
}

SpriteAuditorUtility.DrawDefaultSelectionOptions(
sceneToAtlasToUsedSprites[sceneAsset][atlasToUSedSprites.Key]
.Select(spriteData => spriteData.Sprite).Cast<Object>().ToList());
EditorGUI.indentLevel--;
}

EditorGUI.indentLevel--;
}

EditorGUILayout.EndVertical();
}
}
Expand Down
42 changes: 29 additions & 13 deletions Scripts/Editor/Results/SpriteData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,32 @@ public SpriteData(Sprite targetSprite)
return;
}

CheckAtlasData();
}

public void CheckAtlasData()
{
if (Sprite == null)
return;

cachedSpriteAtlas = null;
spriteUsageFlags &= ~SpriteUsageFlags.UsingScaledAtlasSize;

if (AtlasCacheUtility.TryGetAtlasForSprite(cachedSprite, out SpriteAtlas spriteAtlas))
{
cachedSpriteAtlas = spriteAtlas;
spriteAtlasGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(spriteAtlas));
if (AtlasCacheUtility.TryGetAtlasScale(spriteAtlas, out atlasScale))
atlasScale = spriteAtlas.GetVariantScale();
if (atlasScale != 1.0f)
spriteUsageFlags |= SpriteUsageFlags.UsingScaledAtlasSize;
}
else
{
cachedSpriteAtlas = null;
spriteAtlasGUID = String.Empty;
}
}

public void ReportUse(GameObject instance, Vector3? size)
{
string usagePath = instance.transform.GetPath();
Expand All @@ -147,7 +164,9 @@ public void ReportUse(GameObject instance, Vector3? size)
private void ReportScene(Scene scene)
{
if (scene.buildIndex == -1 || string.IsNullOrEmpty(scene.path))
{
spriteUsageFlags |= SpriteUsageFlags.UsedOnDontDestroyOrUnknowScene;
}
else
{
if (scenesPath.Add(scene.path))
Expand Down Expand Up @@ -213,22 +232,19 @@ public void CheckForSizeFlags()
float differenceMagnitude = sizeDifference.magnitude / SpriteSize.magnitude;
if (Mathf.Abs(differenceMagnitude) > SpriteAuditorUtility.SpriteUsageSizeThreshold)
{
if (!SpriteAuditorUtility.CanTweakMaxSize(this))
return;

if (maximumUsageSize.Value.sqrMagnitude > SpriteSize.sqrMagnitude)
{
if (SpriteAuditorUtility.CanTweakMaxSize(this))
{
spriteUsageFlags |= SpriteUsageFlags.UsedBiggerThanSpriteRect;
}

spriteUsageFlags |= SpriteUsageFlags.UsedBiggerThanSpriteRect;
}
else
{
if (SpriteAuditorUtility.CanTweakMaxSize(this))
{
spriteUsageFlags |= SpriteUsageFlags.UsedSmallerThanSpriteRect;
}
spriteUsageFlags |= SpriteUsageFlags.UsedSmallerThanSpriteRect;
}
}}
}
}

private SpriteUseData GetOrCreateSpriteUsageData(GameObject instance, string usagePath)
{
Expand Down Expand Up @@ -289,7 +305,7 @@ public bool HasWarnings()
return true;

return false;
}
}

public void PrepareForRun()
{
Expand Down
Loading

0 comments on commit b9dee23

Please sign in to comment.