Skip to content

Commit

Permalink
Add generating TMP sheets for additional collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Orange-Panda committed Jun 9, 2024
1 parent ea3f422 commit 7d59ca1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
- Added `collectionKey` string field to `GlyphCollection` for distinctly identifying and referencing collections at runtime
- ⚠️ **[Breaking]** - Added `collectionKey` optional parameter to all `InputGlyphs` methods for referencing secondary (non-default) collections
- Added `additionalCollections` field to `RewiredGlyphManager` for additively loading additional collection for reference by their `collectionKey`
- This field also supports generating TMP sprite sheets (generates for default collection and collections included in additional collections)
- Note: Make sure the names of the sprite sheets containing glyphs are unique since they are referenced by name in TextMeshPro. An error message has been added to notify about such collisions.
- Added optional specifier `set=collectionname` for `GlyphRichTextFormatter` to target secondary glyph collections
- Example: `<glyph Jump set=dark>` where 'dark' is the `collectionKey` on some `GlyphCollection` that is loaded into InputGlyphs

### Changed

- Rewrote the way glyph collections are loaded into memory internally
- Switching the active glyph collection is now much more performant
- Loading a glyph collection now only dispatch a glyph update if it may have changed the output of glyph queries
- Loading a glyph collection now only dispatches a glyph update if it may have changed the output of glyph queries

### Fixed

Expand Down
65 changes: 34 additions & 31 deletions Editor/RewiredGlyphManagerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,60 @@ namespace LMirman.RewiredGlyphs.Editor
[CustomEditor(typeof(RewiredGlyphManager))]
public class RewiredGlyphManagerEditor : UnityEditor.Editor
{
private readonly HashSet<string> usedSpriteSheetNames = new HashSet<string>();

public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Generate TMP Sprite Sheet"))
{
usedSpriteSheetNames.Clear();
// Get the collection to generate from
GlyphCollection collection = serializedObject.FindProperty("glyphCollection").objectReferenceValue as GlyphCollection;
if (collection == null)
{
Debug.LogError("A glyph collection is required.");
return;
Debug.LogError("No glyph collection defined on Rewired Glyph Manager.");
}

// Get all glyphs
List<Glyph> glyphs = new List<Glyph>()
{
collection.NullGlyph,
collection.UnboundGlyph
};
foreach (GlyphCollection.GuidEntry guidEntry in collection.GuidMaps)
else
{
foreach (Glyph glyph in guidEntry.glyphMap.Glyphs)
{
glyphs.Add(glyph);
}
GenerateForCollection(collection);
}

foreach (GlyphCollection.HardwareEntry hardwareEntry in collection.HardwareMaps)
SerializedProperty additionalCollections = serializedObject.FindProperty("additionalCollections");
for (int i = 0; i < additionalCollections.arraySize; i++)
{
foreach (Glyph glyph in hardwareEntry.glyphMap.Glyphs)
GlyphCollection additionalCollection = additionalCollections.GetArrayElementAtIndex(i).objectReferenceValue as GlyphCollection;
if (additionalCollection == null)
{
glyphs.Add(glyph);
Debug.LogError($"Invalid additional glyph collection on Rewired Glyph Manager at index {i}.");
}
}

foreach (GlyphCollection.TemplateEntry templateEntry in collection.TemplateMaps)
{
foreach (Glyph glyph in templateEntry.glyphMap.Glyphs)
else
{
glyphs.Add(glyph);
GenerateForCollection(additionalCollection);
}
}
}

return;

void GenerateForCollection(GlyphCollection collection)
{
// Get all glyphs
List<Glyph> glyphs = new List<Glyph>
{
collection.NullGlyph,
collection.UnboundGlyph,
collection.UninitializedGlyph
};
glyphs.AddRange(collection.GuidMaps.SelectMany(guidEntry => guidEntry.glyphMap.Glyphs));
glyphs.AddRange(collection.HardwareMaps.SelectMany(hardwareEntry => hardwareEntry.glyphMap.Glyphs));
glyphs.AddRange(collection.TemplateMaps.SelectMany(templateEntry => templateEntry.glyphMap.Glyphs));

// Get all unique asset names from all glyphs.
// Note: We naively check the FullSprite only because exclusively having Positive or Negative sprites on a separate sprite sheet is impractical
HashSet<string> spriteAssetPaths = new HashSet<string>();
foreach (Glyph glyph in glyphs)
foreach (string assetPath in from glyph in glyphs select glyph.FullSprite into sprite where sprite != null select AssetDatabase.GetAssetPath(sprite))
{
Sprite sprite = glyph.FullSprite;
if (sprite == null)
{
continue;
}

string assetPath = AssetDatabase.GetAssetPath(sprite);
spriteAssetPaths.Add(assetPath);
}

Expand All @@ -75,6 +74,10 @@ public override void OnInspectorGUI()
{
Texture2D assetAtPath = AssetDatabase.LoadAssetAtPath<Texture2D>(spriteAssetPath);
SpriteSheetOutput sheetOutput = GenerateSpriteAsset(assetAtPath);
if (usedSpriteSheetNames.Add(sheetOutput.spriteAssetName) == false)
{
Debug.LogError($"Generated multiple sprite sheets with name \"{sheetOutput.spriteAssetName}\". Multiple sprite sheets can't have the same name, please rename.");
}
spriteSheetOutputs.Add(sheetOutput.spriteAssetPath, sheetOutput);
}

Expand Down

0 comments on commit 7d59ca1

Please sign in to comment.