Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以随机方法作为参数调用随机方法_C#_Unity3d_Methods_Nested - Fatal编程技术网

C# 以随机方法作为参数调用随机方法

C# 以随机方法作为参数调用随机方法,c#,unity3d,methods,nested,C#,Unity3d,Methods,Nested,所以,我正在为我的统一游戏工作。我想在一个随机位置产生一个随机的敌人类型 目前,我有两种方法来随机化繁殖位置: 从屏幕顶部: void SpawnTop(IEnumerator spawnMethod) { //There's 4 positions on plane X where the enemy can spawn randomXSpawnSection[0] = Random.Range(-13f, -6.5f); randomXSpawnSection[1]

所以,我正在为我的统一游戏工作。我想在一个随机位置产生一个随机的敌人类型

目前,我有两种方法来随机化繁殖位置:

从屏幕顶部:

void SpawnTop(IEnumerator spawnMethod)
{
    //There's 4 positions on plane X where the enemy can spawn
    randomXSpawnSection[0] = Random.Range(-13f, -6.5f);
    randomXSpawnSection[1] = Random.Range(-6.5f, 0f);
    randomXSpawnSection[2] = Random.Range(0f, 6.5f);
    randomXSpawnSection[3] = Random.Range(6.5f, 13.1f);

    StartCoroutine(spawnMethod);
}
void SpawnSide(IEnumerator spawnMethod)
{
    //There's 4 positions on plane Z where the enemy can spawn
    randomZSpawnSection[0] = Random.Range(-7f, 1.5f);
    randomZSpawnSection[1] = Random.Range(1.5f, 10f);
    randomZSpawnSection[2] = Random.Range(10f, 18.5f);
    randomZSpawnSection[3] = Random.Range(18.5f, 27.1f);

    StartCoroutine(spawnMethod);
}
从屏幕的侧面:

void SpawnTop(IEnumerator spawnMethod)
{
    //There's 4 positions on plane X where the enemy can spawn
    randomXSpawnSection[0] = Random.Range(-13f, -6.5f);
    randomXSpawnSection[1] = Random.Range(-6.5f, 0f);
    randomXSpawnSection[2] = Random.Range(0f, 6.5f);
    randomXSpawnSection[3] = Random.Range(6.5f, 13.1f);

    StartCoroutine(spawnMethod);
}
void SpawnSide(IEnumerator spawnMethod)
{
    //There's 4 positions on plane Z where the enemy can spawn
    randomZSpawnSection[0] = Random.Range(-7f, 1.5f);
    randomZSpawnSection[1] = Random.Range(1.5f, 10f);
    randomZSpawnSection[2] = Random.Range(10f, 18.5f);
    randomZSpawnSection[3] = Random.Range(18.5f, 27.1f);

    StartCoroutine(spawnMethod);
}
这两种方法都使用另一种方法作为参数,该方法实例化了某种类型的敌人,如下所示:

IEnumerator SpawnEnemyGunner(float randXSpawn, float randTimeSpawn)
{
    yield return new WaitForSeconds(randTimeSpawn);

    randZSpawn = Random.Range(24, 30);
    Instantiate(enemyGunner, new Vector3(randXSpawn, 1.5f, randZSpawn), enemyGunner.transform.rotation);

    spawning = false;
}
我在游戏中有几个敌人,因此有几种方法

调用嵌套方法而不随机化的示例如下:

SpawnTop(SpawnEnemyGunner(Random.Range(0, randomXSpawnSection.Length), Random.Range(3, 6)));
我想随机化方法和他的方法参数。

应该尽可能简单

var x = randomXSpawnSection[Random.Range(0, randomXSpawnSection.Length)];
var y = randomZSpawnSection[Random.Range(0, randomZSpawnSection.Length)];
var enemy = Prefabs[Random.Range(0, Prefabs.Length)];

Instantiate(enemy, new Vector3(x, 1.5f, y), enemyGunner.transform.rotation);
应该简单到

var x = randomXSpawnSection[Random.Range(0, randomXSpawnSection.Length)];
var y = randomZSpawnSection[Random.Range(0, randomZSpawnSection.Length)];
var enemy = Prefabs[Random.Range(0, Prefabs.Length)];

Instantiate(enemy, new Vector3(x, 1.5f, y), enemyGunner.transform.rotation);

特定的通用方法可以如下所示:

IEnumerator Foo1(float someParam)
        { 
            yield return new WaitForSeconds(1);
            Debug.Log($"Foo1: {someParam}");
        }

        IEnumerator Foo2(float someParam2)
        {
            yield return new WaitForSeconds(1);
            Debug.Log($"Foo2: {someParam}");
        }

        void CallRandomFoo()
        {
            Func<float, IEnumerator>[] foos = { Foo1, Foo2 };
            var randomParam = Random.value;
            StartCoroutine(foos[Random.Range(0, foos.Length)](randomParam));
        }
IEnumerator Foo1(float-someParam)
{ 
返回新的WaitForSeconds(1);
Log($“Foo1:{someParam}”);
}
IEnumerator Foo2(浮点参数2)
{
返回新的WaitForSeconds(1);
Log($“Foo2:{someParam}”);
}
void CallRandomFoo()
{
Func[]foos={Foo1,Foo2};
var randomParam=Random.value;
start例程(foos[Random.Range(0,foos.Length)](randomParam));
}

特定的通用方法可以如下所示:

IEnumerator Foo1(float someParam)
        { 
            yield return new WaitForSeconds(1);
            Debug.Log($"Foo1: {someParam}");
        }

        IEnumerator Foo2(float someParam2)
        {
            yield return new WaitForSeconds(1);
            Debug.Log($"Foo2: {someParam}");
        }

        void CallRandomFoo()
        {
            Func<float, IEnumerator>[] foos = { Foo1, Foo2 };
            var randomParam = Random.value;
            StartCoroutine(foos[Random.Range(0, foos.Length)](randomParam));
        }
IEnumerator Foo1(float-someParam)
{ 
返回新的WaitForSeconds(1);
Log($“Foo1:{someParam}”);
}
IEnumerator Foo2(浮点参数2)
{
返回新的WaitForSeconds(1);
Log($“Foo2:{someParam}”);
}
void CallRandomFoo()
{
Func[]foos={Foo1,Foo2};
var randomParam=Random.value;
start例程(foos[Random.Range(0,foos.Length)](randomParam));
}

在这种情况下,我只是实例化一个随机预置,而不是我要找的。在这种情况下,我只是实例化一个随机预置,而不是我要找的。