static readonly string[] PREFAB_PATHS = new string[]
{
"Assets/GameRes/UIPanel",
};
//预制体存放路径
思路 通过文件夹得到所有的预制体,生成预制体,然后遍历所有预制体以及子物体是否存在 主键T如果存在进行callback操作,操作完之后根据savePrefab 进行保存,然后销毁
static void ScanPrefabComponment<T>(Action<string, T> callback, bool savePrefab = false)
{
for (int i = 0; i < PREFAB_PATHS.Length; ++i)
{
DirectoryInfo dir = new DirectoryInfo(PREFAB_PATHS[i]);
FileInfo[] files = dir.GetFiles("*.prefab", SearchOption.AllDirectories);
for (int j = 0; j < files.Length; ++j)
{
string prefabPath = files[j].FullName.Substring(files[j].FullName.IndexOf("Assets"));
try
{
GameObject target = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)) as GameObject;
target = PrefabUtility.InstantiatePrefab(target) as GameObject;
_ScanPrefabComponment<T>(prefabPath, target.transform, callback);
if (savePrefab)
{
PrefabUtility.SaveAsPrefabAsset(target, prefabPath);
}
GameObject.DestroyImmediate(target);
}
catch (Exception e)
{
Debug.LogError($"Analyze prefab: {prefabPath} failed: " + e.Message + " StackTrace: " + e.StackTrace);
}
}
}
}
static void _ScanPrefabComponment<T>(string prefabPath, Transform transform, Action<string, T> callback)
{
T comp = transform.GetComponent<T>();
if (comp != null)
{
callback(prefabPath, comp);
}
for (int i = 0; i < transform.childCount; ++i)
{
_ScanPrefabComponment(prefabPath, transform.GetChild(i), callback);
}
}
//编辑器按钮
[MenuItem("Tools/AddCanvas")]
public static void AddButtonScaleEffect()
{
parent = GameObject.Find("Canvas").gameObject;
ScanPrefabComponment<UIPanel>((prefabPath, text) =>
{
if (text.gameObject.GetComponent<GraphicRaycaster>() == null)
{
GraphicRaycaster graphicRaycaster = text.gameObject.AddComponent<GraphicRaycaster>();
isChange = true;
}
if (text.gameObject.GetComponent<Canvas>() == null)
{
Canvas canvas = text.gameObject.AddComponent<Canvas>();
canvas.overrideSorting = true;
canvas.additionalShaderChannels = AdditionalCanvasShaderChannels.TexCoord1 | AdditionalCanvasShaderChannels.TexCoord2 | AdditionalCanvasShaderChannels.TexCoord3;
isChange = true;
}
if (text.gameObject.GetComponent<CanvasScaler>() == null)
{
CanvasScaler canvasScaler = text.gameObject.AddComponent<CanvasScaler>();
isChange = true;
}
});
}
文章来源:https://www.toymoban.com/news/detail-778259.html
文章来源地址https://www.toymoban.com/news/detail-778259.html
到了这里,关于Unity 编辑器功能之查找预制体的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!