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

64 lines
1.9 KiB
C#

#if UNITY_5_3_OR_NEWER
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using UnityEngine;
using UnityEngine.Events;
namespace Net.Project.B.WorldNode
{
public class UnityEnvironmentController : MonoBehaviour
{
[SerializeField] private GameObject[] searchGameObjects;
[SerializeField] private GameObject[] staticGameObjects;
[SerializeField] private UnityEvent onCompleted;
private void Start()
{
Execute();
onCompleted?.Invoke();
Destroy(this);
}
[ContextMenu("Build Index")]
public void Rebuild()
{
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();
}
Debug.Log($"已获取到{staticGameObjects.Length}个物体");
}
private void Execute()
{
var lost = 0;
foreach (var staticGameObject in staticGameObjects)
{
if (staticGameObject)
{
staticGameObject.isStatic = true;
continue;
}
lost++;
}
Debug.Log($"已设置{staticGameObjects.Length}个物体为isStatic,丢失了:{lost}个");
}
}
}
#endif