You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

45 lines
1.3 KiB

using UnityEngine;
using System.Collections.Generic;
//------------------------------------------------------------------
public class RandomProbInfo
{
public string m_value = "";
public float m_start = 0;
public float m_end = 0;
public float m_weight = 0;
}
//------------------------------------------------------------------
public class RandomProb
{
List<RandomProbInfo> m_values = new List<RandomProbInfo>();
float m_maximum = 0;
//------------------------------------------------------------------
public void AddValue(string value, float prob )
{
RandomProbInfo randomProbInfo = new RandomProbInfo();
randomProbInfo.m_value = value;
randomProbInfo.m_weight = prob;
randomProbInfo.m_start = m_maximum;
randomProbInfo.m_end = randomProbInfo.m_start + prob;
m_maximum = randomProbInfo.m_end;
m_values.Add(randomProbInfo);
}
//------------------------------------------------------------------
public string GetRandomValue()
{
float random = Random.Range(0f, 1f) * m_maximum;
string randomValue = "";
for (int i = 0; i < m_values.Count; ++i )
if (random >= m_values[i].m_start && random < m_values[i].m_end)
return m_values[i].m_value;
return randomValue;
}
}