ar_tourism_flutter_unity/unity/VRProject2/Assets/zhl/zhlScripts/GetKuangparent.cs
2025-05-14 17:04:13 +08:00

82 lines
2.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GetKuangparent : MonoBehaviour
{
public GameObject myCamera; // 参考相机
public GameObject ding;
public List<GameObject> objectsToManage; // 需要管理的物体列表
public float minDistance; // 最小显示距离
public float maxDistance; // 最大显示距离
public float fanWei;
void Start()
{
Debug.Log("ding.transform.childCount:" + ding.transform.childCount);
for (int i = 0; i < ding.transform.childCount; i++)
{
objectsToManage.Add(ding.transform.GetChild(i).gameObject);
}
}
void Update()
{
if (myCamera != null)
{
foreach (GameObject obj in objectsToManage)
{
CheckDistance(obj);
}
}
}
// void CheckDistance(GameObject obj)
// {
// Vector3 cameraPosition = myCamera.transform.position;
// Vector3 myPosition = obj.transform.position;
// // 设置默认范围
// float rangeX = fanWei;
// float rangeZ = fanWei;
// // 如果物体名称是 BigPlaneManager增加检查范围
// if (obj.name == "BigPlaneManager")
// {
// rangeX = fanWei*3; // 增加 X 方向的范围
// rangeZ = fanWei*3; // 增加 Z 方向的范围
// }
// bool isWithinRange = Mathf.Abs(myPosition.x - cameraPosition.x) <= rangeX && Mathf.Abs(myPosition.z - cameraPosition.z) <= rangeZ;
// // 只在范围内时激活物体
// obj.SetActive(isWithinRange);
// }
void CheckDistance(GameObject obj)
{
Vector3 cameraPosition = myCamera.transform.position;
Vector3 myPosition = obj.transform.position;
float distance = Vector3.Distance(cameraPosition, myPosition);
// 同时设置最小和最大距离的特殊处理
float effectiveMin = minDistance;
float effectiveMax = maxDistance;
if (obj.name == "BigPlaneManager")
{
effectiveMin = 0f; // 最小距离设为0
}
bool inRange = distance >= effectiveMin && distance <= effectiveMax;
if (obj != null && obj.activeSelf != inRange)
{
obj.SetActive(inRange);
if(inRange)
{
StartCoroutine(GetData.Instance.PostRequest());
}
}
}
}