using Packages.Rider.Editor.Util;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using UnityEditor;
using UnityEngine;
public class AddComponents : Editor
{
private static string GetClickedDirFullPath()
{
string clickedAssetGuid = Selection.assetGUIDs[0];
string clickedPath = AssetDatabase.GUIDToAssetPath(clickedAssetGuid);
string clickedPathFull = Path.Combine(Directory.GetCurrentDirectory(), clickedPath);
FileAttributes attr = File.GetAttributes(clickedPathFull);
return attr.HasFlag(FileAttributes.Directory) ? clickedPathFull : Path.GetDirectoryName(clickedPathFull);
}
private static string GetPath()
{
string path = GetClickedDirFullPath();
int index = path.IndexOf("Assets");
string result = path.Substring(index);
return result;
}
static List<string> paths = new List<string>();
[MenuItem("Assets/Get Prefabs")]
private static void GetFolders()
{
string selectedPath = GetPath();
string[] assetsPaths = AssetDatabase.GetAllAssetPaths();
foreach (string assetPath in assetsPaths)
{
if (assetPath.Contains(selectedPath))
{
if (assetPath.Contains("Prefabs"))
{
paths.Add(assetPath);
}
}
}
}
[MenuItem("Assets/Get Prefabs")]
public static void GetPrefabs()
{
GetFolders();
for (int i = 0; i < paths.Count; i++)
{
if (File.Exists(paths[i]))
{
if (paths[i].Contains(".prefab"))
{
GameObject contentsRoot = PrefabUtility.LoadPrefabContents(paths[i]);
if (contentsRoot.GetComponent<Rigidbody>() != null)
{
// Modify Prefab contents.
contentsRoot.AddComponent<Rigidbody>();
// Save contents back to Prefab Asset and unload contents.
PrefabUtility.SaveAsPrefabAsset(contentsRoot, paths[i]);
PrefabUtility.UnloadPrefabContents(contentsRoot);
}
}
}
}
}
}
With code I can make a right clock with the mouse on any folder in the Assets and then clicking on Get Prefabs and it will loop over all the folders and sub folders get all the prefabs and add a Rigidbody to them.
The problem is it’s adding the Rigidbody only to some of the prefabs.
For example in this screenshot to this prefabs it will add a Rigidbody :
but to prefabs I created by dragging objects to the Assets it’s not adding the Rigidbody component.
for some reason it’s not considering my prefabs as prefabs types or maybe the problem is something else.
I’m not sure what is the problem and how to fix it.
Screenshot of two of my prefabs :

