quality-control/Assets/Scripts/Products/ProductDescription.cs

36 lines
825 B
C#
Raw Normal View History

using System;
using UnityEngine;
using Random = UnityEngine.Random;
[CreateAssetMenu(fileName = "New Product Description", menuName = "Data/Product Description", order = 0)]
public class ProductDescription : ScriptableObject
{
[Serializable]
public struct Product
{
public GameObject Prefab;
[Range(0,100)]
public int Probability;
}
public Product[] Products;
public GameObject GetRandomProduct()
{
2024-08-17 19:07:25 +00:00
float randomValue = Random.Range(0, 100);
float sum = 0;
for (var i = 0; i < Products.Length; i++)
{
var product = Products[i];
2024-08-17 19:07:25 +00:00
sum += product.Probability;
if (randomValue <= sum)
{
return product.Prefab;
}
}
return null;
}
}