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

104 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[ExecuteInEditMode]
public class PlaneManager : MonoBehaviour
{
public Sprite[] sprites1;
public Sprite[] sprites2;
private int currentSpriteArrayIndex = 0;
public int i;
public List<GameObject> gameObjects = new List<GameObject>();
void Start()
{
// 确保在开始时填充gameObjects
if (gameObjects.Count == 0)
{
foreach (Transform child in transform)
{
gameObjects.Add(child.gameObject);
}
}
switch (transform.name)
{
case "DingA":
SetRandomSprite(sprites1);
break;
case "DingB":
SetRandomSprite(sprites2);
break;
case "BianA":
SetRandomSprite(sprites1);
break;
case "BianB":
SetRandomSprite(sprites2);
break;
default:
SetRandomSprite(sprites1);
break;
}
}
public void Update()
{
}
private void SetRandomSprite(Sprite[] sprites)
{
if (sprites == null || sprites.Length == 0) return;
foreach (var item in gameObjects)
{
MyPlane myPlane = item.GetComponent<MyPlane>();
if (myPlane != null)
{
int n = Random.Range(0, sprites.Length);
myPlane.rawImageGet.texture = sprites[n].texture;
}
else
{
Debug.LogWarning($"MyPlane component is missing on {item.name}");
}
}
}
public void ChangColor(float n)
{
foreach (var item in gameObjects)
{
MyPlane myPlane = item.GetComponent<MyPlane>();
if (myPlane != null)
{
RawImage rawImage = myPlane.rawImageGet;
Color color = rawImage.color;
color.a = n;
rawImage.color = color;
}
else
{
Debug.LogWarning($"MyPlane component is missing on {item.name}");
}
}
}
public void PaiXu()
{
}
void OnValidate() // 添加这个方法
{
// 确保在编辑器模式下也能获取所有子物体
if (gameObjects.Count == 0)
{
foreach (Transform child in transform)
{
gameObjects.Add(child.gameObject);
}
}
}
}