Net.Project.B/Src/WorldNode/UnityEnvironmentController.cs

64 lines
1.9 KiB
C#
Raw Normal View History

2025-03-15 21:13:46 +08:00
#if UNITY_5_3_OR_NEWER
2025-02-24 23:02:49 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using UnityEngine;
2025-03-15 21:13:46 +08:00
using UnityEngine.Events;
2025-02-24 23:02:49 +08:00
namespace Net.Project.B.WorldNode
{
public class UnityEnvironmentController : MonoBehaviour
{
2025-03-15 21:13:46 +08:00
[SerializeField] private GameObject[] searchGameObjects;
2025-02-24 23:02:49 +08:00
[SerializeField] private GameObject[] staticGameObjects;
2025-03-15 21:13:46 +08:00
[SerializeField] private UnityEvent onCompleted;
2025-02-24 23:02:49 +08:00
private void Start()
{
2025-03-03 18:43:55 +08:00
Execute();
2025-03-15 21:13:46 +08:00
onCompleted?.Invoke();
2025-03-03 18:43:55 +08:00
Destroy(this);
2025-02-24 23:02:49 +08:00
}
[ContextMenu("Build Index")]
public void Rebuild()
{
2025-03-15 21:13:46 +08:00
if (searchGameObjects is { Length: > 0 })
{
var hashSet = new HashSet<Transform>();
foreach (var searchGameObject in searchGameObjects)
{
if (!searchGameObject) continue;
hashSet.UnionWith(searchGameObject.GetComponentsInChildren<Transform>());
}
staticGameObjects = hashSet.Select(x => x.gameObject).ToArray();
}
else
{
staticGameObjects = GetComponentsInChildren<Transform>().Where(x=>x.gameObject.isStatic).Select(x=>x.gameObject).ToArray();
}
2025-02-24 23:02:49 +08:00
Debug.Log($"已获取到{staticGameObjects.Length}个物体");
}
2025-03-03 18:43:55 +08:00
private void Execute()
{
var lost = 0;
foreach (var staticGameObject in staticGameObjects)
{
if (staticGameObject)
{
staticGameObject.isStatic = true;
continue;
}
lost++;
}
Debug.Log($"已设置{staticGameObjects.Length}个物体为isStatic,丢失了:{lost}个");
}
2025-02-24 23:02:49 +08:00
}
}
2025-03-15 21:13:46 +08:00
#endif