我有带蓝色框符号的预制件资产:代码将其视为预制件并对其进行更改:
这是我通过将它们拖到素材资源而创建的预制件:
此代码将在第一个屏幕快照中将刚体添加到预制件,但不会在第二个屏幕快照中将刚性体添加到两个预制件:
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);
}
}
}
}
}
}
这是一个有效的解决方案脚本:我更改了什么:也许我可以使这一切变得更容易和更短,但是可以正常工作!
[代替包含我现在正在使用EndsWith:
如果(paths [i] .EndsWith(“。prefab”))
添加了局部变量以获取Rigidbody,然后检查它是否为NULL,而不是它是否为NULL:
var rb = contentsRoot.GetComponent();如果(rb == null)
完整脚本:
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].EndsWith(".prefab"))
{
GameObject contentsRoot = PrefabUtility.LoadPrefabContents(paths[i]);
var rb = contentsRoot.GetComponent<Rigidbody>();
if (rb == null)
{
// Modify Prefab contents.
contentsRoot.AddComponent<Rigidbody>();
// Save contents back to Prefab Asset and unload contents.
PrefabUtility.SaveAsPrefabAsset(contentsRoot, paths[i]);
PrefabUtility.UnloadPrefabContents(contentsRoot);
}
}
}
}
}
}