2 c# unity-game-engine gameobject
我试图让一个游戏对象在有限的时间内出现和消失(让我们暂时把时间函数放在一边)。
这是我得出的结论:
using UnityEngine;
using System.Collections;
public class Enemy1Behavior : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
this.gameObject.SetActive(false); // Making enemy 1 invisible
Debug.Log("Update called");
DisappearanceLogic(gameObject);
}
private static void DisappearanceLogic(GameObject gameObject)
{
int num = 0;
while (num >= 0)
{
if (num % 2 == 0)
{
gameObject.SetActive(false);
}
else
{
gameObject.SetActive(true);
}
num++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我单击程序中的播放按钮时Unity没有响应,我只能使用End Task.
(是的,我知道该方法中有一个无限循环)。
所以我想我做错了什么。在 中制作GameobjectBlink/Flash/appear-disappear的最佳方法是什么Unity?
谢谢你们。
您正在使用一个无限循环来完全锁定您的 Update(),因为 num 将始终大于 0。
所以你可以使用 InvokeRepeating ( http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html )
public GameObject gameobj;
void Start()
{
InvokeRepeating("DisappearanceLogic", 0, interval);
}
void DisappearanceLogic()
{
if(gameobj.activeSelf)
{
gameobj.SetActive(false);
}
else
{
gameobj.SetActive(true);
}
}
Run Code Online (Sandbox Code Playgroud)
间隔是一个浮点数——比如 1f 0.5f 等等。
| 归档时间: |
|
| 查看次数: |
2496 次 |
| 最近记录: |