使用循环行为逐个显示 10 个单独的网格体,为空游戏对象创建"Animation"?



我有 10 个网格,每个网格基本上都是鲸鱼游泳动画的冻结"帧"。

如果我循环显示这些网格,那么它将创建一个伪动画,这就是我想要的。

如何实现这一点?目前我有游戏对象AnimTest,我已经将 10 个网格 .obj 文件作为子文件放置到它上面。我从游戏对象的脚本changeMeshes上的以下代码开始:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class changeMeshes : MonoBehaviour
{
//Create array of the meshes
public float[] currentMesh; 
// Start is called before the first frame update
void Start()
{

}
// Update is called once per frame
void Update()
{
for (int i = 0; i < 10; i++)
{
currentMesh[i].gameObject.GetComponent<MeshRenderer>().enabled = true;
}
}
}

这显然是非常错误的,因为我是新手和困惑,所以我遇到像float does not contain a definition for gameObject这样的脚本错误。谁能帮助我走上正确的道路?

编辑:这是我最好的尝试。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class changeMeshes : MonoBehaviour
{
//Create array of the meshes
public GameObject[] whaleMeshes;
void FindWhales()
{    
whaleMeshes = GameObject.FindGameObjectsWithTag("WhaleMesh");    
print(whaleMeshes.Length);
}
void CycleWhales()
{
for (int i = 0; i < whaleMeshes.Length; i++)
{
if (i > 0)
{
whaleMeshes[i - 1].gameObject.GetComponentInChildren<MeshRenderer>().enabled = false;
}
whaleMeshes[i].gameObject.GetComponentInChildren<MeshRenderer>().enabled = true;
}
}
// Start is called before the first frame update
void Start()
{
FindWhales();
}
// Update is called once per frame
void Update()
{
CycleWhales();

}
}

现在的结果是,只有最后一个网格被永久渲染,而不是脚本循环遍历网格。如何让网格循环?

好吧..浮点数没有属性.gameObject.甚至Mesh也不会

既然你说对象是这个组件的子对象,你可以简单地遍历它们。

然后我会简单地激活和停用整个GameObject.

听起来你想做的是这样的

public class changeMeshes : MonoBehaviour
{
// How long should one mesh be visible before switching to the next one
[SerializeField] private float timePerMesh = 0.2f;
public UnityEvent whenDone;
private IEnumerator Start()
{
// Get amount of direct children of this object
var childCount = transform.childCount;
// Get the first child 
var currentChild = transform.GetChild(0);
// Iterate through all direct children
foreach(Transform child in transform)
{
// Set all objects except the first child to inactive
child.gameObject.SetActive(child == currentChild);
}
// Iterate though the rest of direct children
for(var i = 1; i < childCount; i++)
{
// Wait for timePerMesh seconds
yield return new WaitForSeconds(timePerMesh);
// Set the current child to inactive
currentChild.gameObject.SetActive(false);
// Get the next child
currentChild = transform.GetChild(i);
// Set this one to active  
currentChild.gameObject.SetActive(true);
}
// Optionally do something when done like e.g. destroy this GameObject etc
yield return new WaitForSeconds(timePerMesh);
whenDone.Invoke();
}
}

最新更新