僅供參考,我對編程和 c# 很陌生。對不起,如果這看起來很簡單的問題。
我有點難以解釋我想做什么。所以基本上,有陣列/數字串列。將使用 Random.Range() 選擇數字。每個號碼都會有自己的動作,他們會主動。例如,數字 1 將啟動場景 1,數字 2 將啟動場景 2,依此類推。你如何將這個隨機選擇的數字分配給某個動作?
我什至不知道如何將其放入代碼中(使用陣列/字串?),但基本想法可能看起來像這樣:
int randomNumber = Random.Range(1, 10);
randomNumber[1] = SceneManager.LoadScene(Level1);
randomNumber[2] = SceneManager.LoadScene(Level2);
...
uj5u.com熱心網友回復:
請注意,這個答案并不準確;)
的上限Random.Range(int,int)是排他的所以
Random.Range(0, 10)
永遠不會回傳10,而是介于0和之間的值9。
實際上更簡單的是將所有場景存盤在一個陣列中,例如
public List<string> scenes = new List<string>();
然后使用
var randomIndex = Random.Range(0, scene.Length);
SceneManager.LoadScene(scenes[randomIndex]);
不需要長且不動態的if-elseor switch-case。陣列/串列可以在 Inspector 中甚至在運行時完全動態調整,而無需添加更多案例。
uj5u.com熱心網友回復:
The easiest way of going about this is:
int randomNumber = Random.Range(1, 10);
if(randomNumber == 1) {
// Load Scene 1
} else if (randomNumber == 2) {
// Load Scene 2
}
EDIT:
You don't assign the random number in this case. You check if the random number is a specific number. You can check if numbers are equal using ==.
The random number has a value between 1 and 9 and this number is random everytime you run the program.
If you want to do it more dynamically you can look at the answer from @derHugo. Depends on how you use it and how many if-else statements you have to write.
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/457159.html
上一篇:分數未更新
