Skip to content

Instantly share code, notes, and snippets.

@karljj1
Created May 20, 2022 15:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karljj1/f34a483b035415418f8b7b3021d62640 to your computer and use it in GitHub Desktop.
Save karljj1/f34a483b035415418f8b7b3021d62640 to your computer and use it in GitHub Desktop.
Find localization entries that are not used
using System;
using System.Collections.Generic;
using System.Text;
using UnityEditor;
using UnityEditor.Localization;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.Localization.Tables;
using Object = UnityEngine.Object;
public class FindUsedKeys : ScriptableWizard
{
HashSet<(string collectionName, long entryId)> m_FoundEntries;
[MenuItem("Test/Find unused keys")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<FindUsedKeys>("Find unused keys", "Search");
}
void OnWizardCreate()
{
m_FoundEntries = new HashSet<(string collectionName, long entryId)> ();
var sceneGUIDs = AssetDatabase.FindAssets("t:Scene");
foreach (var sceneGUID in sceneGUIDs)
{
var path = AssetDatabase.GUIDToAssetPath(sceneGUID);
var scene = EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
{
foreach (var t in FindObjectsOfType<Transform>())
{
foreach (var c in t.GetComponents<Component>())
{
ExtractTableReferences(c);
}
}
}
}
FindMissingEntries();
}
void FindMissingEntries()
{
var sb = new StringBuilder();
foreach(var collection in LocalizationEditorSettings.GetStringTableCollections())
{
foreach(var entry in collection.SharedData.Entries)
{
if (m_FoundEntries.Contains((collection.TableCollectionName, entry.Id)))
{
// Entry is used
sb.AppendLine($"USED - {collection.TableCollectionName} / {entry.Key}");
}
else
{
// Entry is not used.
sb.AppendLine($"NOT USED - {collection.TableCollectionName} / {entry.Key}");
}
}
}
Debug.Log(sb.ToString());
}
void ExtractTableReferences(Object unityObject)
{
var so = new SerializedObject(unityObject);
var itr = so.GetIterator();
while (itr.Next(true))
{
if (itr.type == "LocalizedString")
{
var collectionNameProperty = itr.FindPropertyRelative("m_TableReference.m_TableCollectionName");
var collectionName = collectionNameProperty.stringValue;
TableReference tableReference;
if (collectionName.StartsWith("GUID:"))
tableReference = Guid.Parse(collectionName.Substring("GUID:".Length, collectionName.Length - "GUID:".Length));
else
tableReference = collectionName;
var key = itr.FindPropertyRelative("m_TableEntryReference.m_Key");
var keyId = itr.FindPropertyRelative("m_TableEntryReference.m_KeyId");
TableEntryReference tableEntryReference;
if (keyId.longValue == 0)
tableEntryReference = key.stringValue;
else
tableEntryReference = keyId.longValue;
var collection = LocalizationEditorSettings.GetStringTableCollection(tableReference);
var stringTableEntry = collection?.SharedData.GetEntryFromReference(tableEntryReference);
if (stringTableEntry != null)
{
m_FoundEntries.Add((collection.TableCollectionName, stringTableEntry.Id));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment