LoginSignup
0
1

More than 3 years have passed since last update.

AssetDatabaseメモ

Last updated at Posted at 2020-07-15

ちょこちょこメモ

TextMeshProの付いているPrefabを探してフォント名・マテリアル名が欲しい

全ゲームオブジェクトを取得

 フィルタをObjectにするとなんでもかんでも取ってくるので
 GameObjectにしておく、Prefabにしかないなら"t: Prefab"でも良さそう

AssetDatabase.FindAssets("t: GameObject");

GUIDからパスに変換
var path = AssetDatabase.GUIDToAssetPath(guid);

パスからGameObjectに変換
GameObject currentObject = AssetDatabase.LoadAssetAtPath<UnityEngine.GameObject> (path);

子オブジェクトも含めてどこかにTextMeshProUGUIを持ってないか検索(複数形ありえるのでGetComponents側を使う)
GetComponentsInChildren(true)、trueを付ける非アクティブでも検索してくれる
currentObject.GetComponentsInChildren<TextMeshProUGUI>(true)

ラベルを追加する

 ラベル付けしてEditor拡張から何かに使えないか実験

string[] labels = new string[] { "TextMesh" };
AssetDatabase.SetLabels(currentObject,labels);

using TMPro;
using UnityEditor;
using UnityEngine;

public class TextMeshProTest : MonoBehaviour
{
    public void OnClickCheckAsset()
    {
        AssetDatabase.Refresh();

        int targetCount = 0;
        var allObjects = AssetDatabase.FindAssets("t: GameObject");
        List<string> setFontsList = new List<string>();

        string setFontAppendString = string.Empty;

        foreach (var guid in allObjects)
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);

            GameObject currentObject = AssetDatabase.LoadAssetAtPath<UnityEngine.GameObject>(path);

            var textMeshProUGUIArray = currentObject.GetComponentsInChildren<TextMeshProUGUI>();

            Debug.LogWarning($"basePath={ path}");

            foreach (var textMeshProUGUI in textMeshProUGUIArray)
            {
                if (textMeshProUGUI != null)
                {
                    Debug.Log($"**********");
                    Debug.Log($"path={ path}");

                    if (textMeshProUGUI.font == null)
                    {
                        Debug.LogError($"textMeshProUGUI.gameObject.name={ textMeshProUGUI.gameObject.name}");
                    }
                    else
                    {
                        Debug.Log($"textMeshProUGUI.font.name={textMeshProUGUI.font.name}");
                        if (textMeshProUGUI.fontSharedMaterial == null)
                        {
                            Debug.LogError($"textMeshProUGUI.gameObject.name={ textMeshProUGUI.gameObject.name}");
                        }
                        else
                        {
                            Debug.Log($"textMeshProUGUI.fontMaterial.name={textMeshProUGUI.fontMaterial.name}");

                            // フォント名とマテリアル名をセットで追加
                            setFontsList.Add(textMeshProUGUI.font.name + "," + textMeshProUGUI.fontMaterial.name);

                            setFontAppendString = setFontAppendString + textMeshProUGUI.font.name + "," + textMeshProUGUI.fontMaterial.name + ",";

                            targetCount++;
                            Debug.Log($"targetCount={targetCount}");

                            // ラベルの追加
                            string[] labels = new string[] { "TextMesh" };
                            AssetDatabase.SetLabels(currentObject, labels);


                        }
                    }
                }
            }
        }

        Debug.Log($"setFontAppendString={setFontAppendString}");

        Debug.Log("***** END *****");

        Debug.Log($"targetCount={targetCount}");
    }
}

GetAssetPathを使えばObjectであればパスを取得できる(空文字で返って来る時もあるけど)

string targetPath = AssetDatabase.GetAssetPath(textMeshProUGUI.fontMaterial.mainTexture);
Debug.Log($"targetPath={targetPath}");
string targetPath2 = AssetDatabase.GetAssetPath(textMeshProUGUI);
Debug.Log($"targetPath2={targetPath2}");

変更を保存する。

// TextMesh用のTagをセットする。
textMeshProUGUI.gameObject.tag = $"タグ名";

// ダーティーFLGを立てる
EditorUtility.SetDirty(textMeshProUGUI);

↓ 最終的にSaveAssetsする。

// ダーティーFLGのたっているものを変更ありとして保存する
AssetDatabase.SaveAssets();


タグを追加する

/// <summary>
/// TagManagerを経由してタグを追加
/// </summary>
/// <param name="tag"></param>
public static void AddTag(string tag)
{
    var asset = AssetDatabase.LoadMainAssetAtPath("ProjectSettings/TagManager.asset");
    if (asset != null)
    { // sanity checking
        var so = new SerializedObject(asset);
        var tags = so.FindProperty("tags");

        var numTags = tags.arraySize;
        // do not create duplicates
        for (int i = 0; i < numTags; i++)
        {
            var existingTag = tags.GetArrayElementAtIndex(i);
            if (existingTag.stringValue == tag) return;
        }

        tags.InsertArrayElementAtIndex(numTags);
        tags.GetArrayElementAtIndex(numTags).stringValue = tag;
        so.ApplyModifiedProperties();
        so.Update();
    }
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1