sk-client/Assets/Scripts/Editor/GameEditor/PlayerBuilder/FindItemAddScript.cs

56 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
namespace GameEditor.Core
{
public class FindItemAddScript
{
[MenuItem("Tools/预制批量根据某一组件添加另外组件")]
public static void Find()
{
//查找所有预制
var resourcesPath = Application.dataPath;
var absolutePaths = System.IO.Directory.GetFiles(resourcesPath, "*.prefab", System.IO.SearchOption.AllDirectories);
for (int i = 0; i < absolutePaths.Length; i++)
{
EditorUtility.DisplayProgressBar("查找添加", "查找添加中:" + (float)i + "/" + absolutePaths.Length, (float)i / absolutePaths.Length);
string path = "Assets" + absolutePaths[i].Remove(0, resourcesPath.Length);
path = path.Replace("\\", "/");
GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
if (prefab != null)
{
//使用修改Button类型
List<Button> ifList = new List<Button>();
GetComponentsInChildren<Button>(prefab, ref ifList);
for (int k = 0; k < ifList.Count; k++)
{
Debug.Log(ifList[k].gameObject.name);
//使用修改InputField类型
ifList[k].gameObject.AddComponent<InputField>();
}
}
}
EditorUtility.ClearProgressBar();
}
public static void GetComponentsInChildren<T>(GameObject gameObject, ref List<T> itemList)
{
T t = gameObject.GetComponent<T>();
if (t != null)
{
itemList.Add(t);
}
for (int i = 0; i < gameObject.transform.childCount; i++)
{
GetComponentsInChildren<T>(gameObject.transform.GetChild(i).gameObject, ref itemList);
}
}
}
}