LoginSignup
0
0

More than 3 years have passed since last update.

Unity 子オブジェクトのチェック(非アクティブ含む)・Component削除

Posted at

全Prefabの子オブジェクトからTextMeshProを含むものを探し出す

// Resources内からPrefabを全検索
var allPrefabs = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Resources" });

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

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

    // trueを入れると非アクティブも含めて全て取得
    var textMeshProUGUIArray = currentObject.GetComponentsInChildren<TextMeshProUGUI>(true);

    if (textMeshProUGUIArray.Length > 0)
    {
            // ※0より大きければデータ存在する。
    }
}



指定したComponentが親オブジェクトにアタッチされている場合、削除する。


// Resources内からPrefabを全検索
var allPrefabs = AssetDatabase.FindAssets("t:Prefab", new string[] { "Assets/Resources" });


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

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

    // アタッチされているかをチェック
    if (currentObject.GetComponent<アタッチ済みクラス名>() != null)
    {
        // Trueを入れると削除可能になる。
        DestroyImmediate(currentObject.GetComponent<アタッチ済みクラス名>(),true);
    }
}

// 変更を保存する
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();


0
0
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
0