生成游戏对象统一 (C#) [英] Spawning game object unity (C#)

查看:29
本文介绍了生成游戏对象统一 (C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写简单的跑步游戏.

I writing simple runner game.

我有游戏对象(四边形).

I have game object (quad).

我希望它生成.

我写了生成脚本:

    using UnityEngine;
using System.Collections;

public class SpawnScript : MonoBehaviour {


    public GameObject[] obj;
    //public GameObject obj;
    public float spawnMin = 1f;
    public float spawnMax = 2f;

    // Use this for initialization
    void Start () {


        Spawn();


    }

    void Spawn()

    {
        //for (int i = 0; i < 10; i++)
          //  Instantiate(obj, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
         Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
        Invoke("Spawn", Random.Range(spawnMin, spawnMax)); 

    }
}

但是我的四边形只生成一次.我需要它生成多次.

But my quad spawning one time.I need it to spawn multiple times.

我的问题在哪里?

推荐答案

start 事件中使用 InvokeRepeating 而不是 Invoke :

Use InvokeRepeating instead of Invoke in start event:

// Invokes the method methodName in time seconds, then repeatedly every   repeatRate seconds.
InvokeRepeating("Spawn", 3, 3);

如果您执行 InvokeRepeating("Function", 1.0f,1.0f),它将在 InvokeRepeating 调用后一秒调用 Function,然后每隔一秒调用一次.因此,您可以控制生成时间.

If you do InvokeRepeating("Function", 1.0f, 1.0f), it will call Function one second after the InvokeRepeating call and then every one second thereafter. Hence, you can control the spawn timings.

更新

如评论中所问:

您也可以随时通过调用以下代码取消InvokeRepeating.更多信息在这里.

You can also cancel InvokeRepeating any time by calling below code. More information here.

CancelInvoke("Spawn");

这篇关于生成游戏对象统一 (C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆