Author Topic: Batch Character Set Extration for Unity Editor  (Read 2485 times)

hadesfury

  • Beta User
  • Full Member
  • *
  • Posts: 133
Batch Character Set Extration for Unity Editor
« on: March 17, 2017, 10:18:12 AM »
Hello,

here's a script that parse all TextAsset from folders in Unity Project View to extract all character that is contained in your files (it's recursive and work on multiple selection)

Have fun

Code: [Select]
using System.Collections.Generic;
using TMPro.EditorUtilities;
using UnityEngine;
using UnityEditor;

public class ExtractCharacterSet : ScriptableObject
{
    [MenuItem("Assets/TextMeshPro/ExtractCharacterSet")]
    public static void TextMeshProExtractCharacterSet()
    {
        List<string> locAssetDictionary = new List<string>();
        List<int> locCharacterSet = new List<int>();

        foreach (string locAssetGuid in Selection.assetGUIDs)
        {
            string locAssetPath = AssetDatabase.GUIDToAssetPath(locAssetGuid);

            if (!AssetDatabase.IsValidFolder(locAssetPath))
            {
                locAssetDictionary.Add(AssetDatabase.AssetPathToGUID(locAssetPath));
            }
            else
            {
                string[] locObjectGuids = AssetDatabase.FindAssets("t:TextAsset", new[] {locAssetPath});

                foreach (string locObjectGuid in locObjectGuids)
                {
                    if (!locAssetDictionary.Contains(locObjectGuid))
                    {
                        locAssetDictionary.Add(locObjectGuid);
                    }
                }
            }
        }

        foreach (string locAssetDictionaryGuid in locAssetDictionary)
        {
            string locCurrentAssetPath = AssetDatabase.GUIDToAssetPath(locAssetDictionaryGuid);
            TextAsset locTextAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(locCurrentAssetPath);
            string locText = locTextAsset.text;

            foreach (char locCharacter in locText)
            {
                if (!locCharacterSet.Contains(locCharacter))
                {
                    locCharacterSet.Add(locCharacter);
                }
            }
        }

        locCharacterSet.Sort();

        string locUnicodeCharacterSet = TMP_EditorUtility.GetUnicodeCharacterSequence(locCharacterSet.ToArray());

        EditorGUIUtility.systemCopyBuffer = locUnicodeCharacterSet;

        Debug.Log(locUnicodeCharacterSet);
    }
}