Create a gist now

Instantly share code, notes, and snippets.

Unityエディタ上で出現率、発生率を設定出来るプログラム
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SetIncidenceSample : MonoBehaviour {
[SerializeField]
int monster1Incidence;
[SerializeField]
int monster2Incidence;
[SerializeField]
int monster3Incidence;
string [] monsterNames = { "スライム", "ゴブリン", "ドラゴン" };
// Use this for initialization
void Start () {
print (GetRandomMonsterName ());
}
// Update is called once per frame
void Update () {
}
string GetRandomMonsterName () {
var incidenceDistoribution = GetIncidenceDistributionList (monster1Incidence, monster2Incidence, monster3Incidence);
int rdm = Random.Range (0, incidenceDistoribution.Count);
return monsterNames [incidenceDistoribution [rdm]];
}
List<int> GetIncidenceDistributionList (params int [] incidences) {
var incidenceList = new List<int> ();
int gcd = GCD (incidences);
for (int i = 0, len = incidences.Length; i < len; i++) {
int incidence = incidences [i] / gcd;
for (int j = 0; j <= incidence; j++) {
incidenceList.Add (i);
}
}
return incidenceList;
}
int GCD (int [] numbers) {
return numbers.Aggregate (GCD);
}
int GCD (int a, int b) {
return b == 0 ? a : GCD (b, a % b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment