using UnityEngine;
public class SyncActiveState : MonoBehaviour
{
public GameObject sourceObject; // 源物体
public GameObject targetObject; // 目标物体
// 更新时同步状态
void Update()
{
if (sourceObject != null && targetObject != null)
{
SyncState(sourceObject.transform, targetObject.transform);
}
}
// 递归同步物体及其子物体的激活状态
void SyncState(Transform source, Transform target)
{
if (source.childCount != target.childCount)
{
Debug.LogError("子物体数量不匹配");
return;
}
for (int i = 0; i < source.childCount; i++)
{
Transform sourceChild = source.GetChild(i);
Transform targetChild = target.GetChild(i);
targetChild.gameObject.SetActive(sourceChild.gameObject.activeSelf);
SyncState(sourceChild, targetChild); // 递归同步子物体
}
}
}