unity限定概率抽卡的脚本
时间: 2023-09-04 15:09:59 浏览: 80
以下是一个简单的 Unity C# 脚本,用于限定概率抽卡:
```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CardGame : MonoBehaviour {
public List<Card> cards = new List<Card>();
public GameObject cardPrefab;
public Transform cardSpawnPoint;
private float rareProbability = 0.2f;
private float epicProbability = 0.05f;
private float legendaryProbability = 0.01f;
void Start () {
for (int i = 0; i < 100; i++) {
float roll = Random.Range(0f, 1f);
Card card = null;
if (roll <= legendaryProbability) {
card = GetRandomCardByRarity(Rarity.Legendary);
} else if (roll <= epicProbability) {
card = GetRandomCardByRarity(Rarity.Epic);
} else if (roll <= rareProbability) {
card = GetRandomCardByRarity(Rarity.Rare);
} else {
card = GetRandomCardByRarity(Rarity.Common);
}
GameObject newCard = Instantiate(cardPrefab, cardSpawnPoint.position, Quaternion.identity) as GameObject;
newCard.GetComponent<CardDisplay>().card = card;
}
}
Card GetRandomCardByRarity(Rarity rarity) {
List<Card> cardsWithRarity = new List<Card>();
foreach (Card card in cards) {
if (card.rarity == rarity) {
cardsWithRarity.Add(card);
}
}
int randomIndex = Random.Range(0, cardsWithRarity.Count);
return cardsWithRarity[randomIndex];
}
}
public enum Rarity {
Common,
Rare,
Epic,
Legendary
}
[System.Serializable]
public class Card {
public string name;
public Rarity rarity;
public int attack;
public int defense;
}
```
这个脚本假设你有一些卡牌对象和一个卡牌预制体。在 Start() 函数中,我们循环 100 次来模拟抽 100 张卡牌。在每次循环中,我们生成一个随机数,然后根据概率分配卡牌的稀有度。如果随机数小于等于传奇稀有度的概率,我们就从传奇稀有度的所有卡牌中随机获取一张。如果随机数小于等于史诗稀有度的概率,我们就从史诗稀有度的所有卡牌中随机获取一张,以此类推。获取到卡牌后,我们实例化一个卡牌预制体,将其显示在屏幕上。
你可以根据自己的需求来修改卡牌稀有度的概率分配和抽卡次数。
阅读全文