using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using BPMobile.Common.Extensions;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
public class StationPrefabComponentInjector : MonoBehaviour {
[SerializeField] private string _pathToPrefabs;
private void Start() {
for (int i = 1; i < 7; i++) {
string currentPath = _pathToPrefabs + i.ToString();
ModifyPrefabsInFolder(currentPath);
}
}
private void ModifyPrefabsInFolder(string folderPath) {
Debug.Log($"Modifying prefabs in folder: {folderPath}");
AssetDatabase.StartAssetEditing();
string[] guids = AssetDatabase.FindAssets(" t:GameObject", new string[] {folderPath});
foreach (string g in guids) {
string path = AssetDatabase.GUIDToAssetPath(g);
Object prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject));
GameObject prefabAsObject = prefab as GameObject;
PrefabModification(prefabAsObject);
}
AssetDatabase.StopAssetEditing();
AssetDatabase.SaveAssets();
}
private void PrefabModification(GameObject prefab) {
Regex prefabNameRegular = new Regex("^Station_");
if (!prefabNameRegular.IsMatch(prefab.name)) return;
Regex gameobjectToInjectRegular = new Regex("^StationWill");
ForAllChildrenRecursive(prefab.transform, x => {
if (!gameobjectToInjectRegular.IsMatch(x.name)) return;
if (x.GetComponent<StationBonusFXController>() == null) {
Debug.Log($"Modifying {prefab.name} prefab");
x.AddComponent<StationBonusFXController>();
}
});
}
private void ForAllChildrenRecursive(Transform parent, Action<GameObject> action) {
action(parent.gameObject);
foreach (Transform child in parent) {
ForAllChildrenRecursive(child, action);
}
}
}