用循环行为一一显示 10 个单独的网格来为空游戏对象创建 "Animation"?
Display 10 separate Meshes one by one with looping behavior to create "Animation" for empty GameObject?
我有 10 个网格,每个基本上都是鲸鱼游泳动画的冻结“帧”。
如果我要循环显示这些网格,那么它会创建一个伪动画,这正是我想要的。
如何实现?目前我有 GameObject AnimTest
,并且我已经将 10 个网格 .obj 文件作为子文件放入其中。我已经开始在 GameObject 的脚本 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();
}
}
我有 10 个网格,每个基本上都是鲸鱼游泳动画的冻结“帧”。
如果我要循环显示这些网格,那么它会创建一个伪动画,这正是我想要的。
如何实现?目前我有 GameObject AnimTest
,并且我已经将 10 个网格 .obj 文件作为子文件放入其中。我已经开始在 GameObject 的脚本 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();
}
}