我在 Internet 上找到了一個類似于 Python 的代碼 random.choices():
static class RandomUtils
{
public static string Choice(this Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
{
var cumulativeWeight = new List<int>();
int last = 0;
foreach (var cur in weights)
{
last = cur;
cumulativeWeight.Add(last);
}
int choice = rnd.Next(last);
int i = 0;
foreach (var cur in choices)
{
if (choice < cumulativeWeight[i])
{
return cur;
}
i ;
}
return null;
}
}
添加到我的代碼中:
using UnityEngine;
using System.Linq;
using System.Collections.Generic;
public class GroundTile : MonoBehaviour
{
GroundSpawner groundSpawner;
public GameObject player;
private bool doDoubleSpawn;
private int chanceForDoubleSpawn = 45;
void Start()
{
groundSpawner = GameObject.FindObjectOfType<GroundSpawner>();
player = GameObject.Find("Tractor");
SpawnObstacle();
chanceForDoubleSpawn = GroundSpawner.levelObstaclesMultiplier;
}
// Spawn weight values for obstacles
private int boxWeightValue = 40;
private int antitankWeightValue = 40;
private int barricadeWeightValue = 40;
private int wheelsWeightValue = 40;
private int molotovItemWeightValue = 5;
// Prefabs
public GameObject obstaclePrefab;
public GameObject boxPrefab;
public GameObject antitankPrefab;
public GameObject barricadePrefab;
public GameObject wheelsPrefab;
public GameObject tankLevel1Prefab;
public GameObject molotovItemPrefab;
// Destroy if player too far away
void Update()
{
if(transform.position.z < player.transform.position.z - 30)
{
Destroy(gameObject);
}
}
}
static class RandomUtils
{
public static string Choice(this Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
{
var cumulativeWeight = new List<int>();
int last = 0;
foreach (var cur in weights)
{
last = cur;
cumulativeWeight.Add(last);
}
int choice = rnd.Next(last);
int i = 0;
foreach (var cur in choices)
{
if (choice < cumulativeWeight[i])
{
return cur;
}
i ;
}
return null;
}
}
但這給了我一個錯誤:
Assets\Scripts\GroundGenerator\GroundTile.cs(61,30): 錯誤 CS0721: 'Random': 靜態型別不能用作引數
我該如何解決?也許還有另一種方法來撰寫這個函式?
UPD:實體化 GroundTile 的代碼:
using UnityEngine;
public class GroundSpawner : MonoBehaviour
{
public GameObject groundTile;
public GameObject groundTileEnd;
public GameObject player;
Vector3 nextSpawnPoint;
private int numOfTiles = 10;
private int plusTiles = 5;
private int count = 0;
public static int level = 1;
public static int levelObstaclesMultiplier = 45;
// Weight values for tanks and items
public static int tankWeightValue = 100;
public static int molotovItemWeightValue = 10;
public static int molotovItemWeightValuePlus = 3;
public void SpawnTile()
{
GameObject temp = Instantiate(groundTile, nextSpawnPoint, Quaternion.identity);
nextSpawnPoint = temp.transform.GetChild(1).transform.position;
}
public void SpawnEndTile()
{
GameObject temp = Instantiate(groundTileEnd, nextSpawnPoint, Quaternion.identity);
nextSpawnPoint = temp.transform.GetChild(1).transform.position;
}
void Start()
{
for (int i = 0; i < 10; i )
{
SpawnTile();
}
SpawnEndTile();
InvokeRepeating("Spawn", 1.5f, 1.5f);
}
public void Spawn()
{
if (Time.timeScale != 0 && PlayerController.forwardSpeed != 0)
{
if (count != numOfTiles)
{
SpawnTile();
count = 1;
}
else
{
SpawnEndTile();
numOfTiles = plusTiles;
count = 0;
if (levelObstaclesMultiplier <= 65)
{
levelObstaclesMultiplier = 1;
}
}
}
}
}
uj5u.com熱心網友回復:
這段代碼似乎是為 C#System.Random而不是 Unity 特定的UnityEngine.Random類撰寫的。System.Random您可以通過在引數中指定來解決錯誤:
public static string Choice(this System.Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
uj5u.com熱心網友回復:
在引數上,只需將“Random”替換為“System.Random”,錯誤是因為它認為您使用的是UnityEngine.Random,這是一個靜態型別。
public static string Choice(this System.Random rnd, IEnumerable<string> choices, IEnumerable<int> weights)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/462697.html
