【0316追加】
進められるようになったので、最後にまとめて掲載します!
以前、キャラクターがアイテムを取得しキャラクターが発射されるキャラクターの銃弾の色をかえる。という内容を皆様のおかげで作ることができました。
今回はクロッシーロードのように、ポイントをもってガチャできるシステムにチャレンジしております。
まずコードを参考にしたサイトはこちらです。
unity のprojectに
- __ProbabilityCalclator.cs__と
using UnityEngine; using System.Collections; using System.Collections.Generic; public static class ProbabilityCalclator { public static bool DetectFromPercent(int percent){ return DetectFromPercent ((float)percent); } public static bool DetectFromPercent(float percent){ int digitNum = 0; if(percent.ToString().IndexOf(".") > 0){ digitNum = percent.ToString ().Split('.')[1].Length; } int rate = (int)Mathf.Pow (10, digitNum); int randomValueLimit = 100 * rate; int border = (int)(rate * percent); return Random.Range (0, randomValueLimit) < border; } public static T DetermineFromDict<T>(Dictionary<T, int> targetDict){ Dictionary<T, float> targetFloatDict = new Dictionary<T, float>(); foreach (KeyValuePair<T, int> pair in targetDict) { targetFloatDict.Add(pair.Key, (float)pair.Value); } return DetermineFromDict (targetFloatDict); } public static T DetermineFromDict<T>(Dictionary<T, float> targetDict){ float totalPer = 0; foreach (float per in targetDict.Values) { totalPer += per; } float rand = Random.Range (0, totalPer); foreach (KeyValuePair<T, float> pair in targetDict) { rand -= pair.Value; if(rand < 0){ return pair.Key; } } Debug.LogError ("抽選ができませんでした"); return new List<T>(targetDict.Keys)[0]; } }
- __GachaTEST.cs__を作成しました。
using UnityEngine; using System.Collections; using System.Collections.Generic; public class GachaTEST : MonoBehaviour { [ContextMenu("Run")] private void Test(){ DetectFromPercent (); DetermineFromDict (); } private void DetectFromPercent(){ int trueCount = 0; int calcCount = 1000000; float per = 0.2f; for (int i = 0; i < calcCount; i++) { if (ProbabilityCalclator.DetectFromPercent (per)) trueCount++; } Debug.Log ("真偽判定の精度確認\n試行回数 : " + calcCount + "回, 目標 : " + per + "%, 実際 : " + ((float)trueCount / (float)calcCount * 100f) + "%"); } private void DetermineFromDict(){ Dictionary<Color, int> targetDict = new Dictionary<Color, int> () { {Color.red, 25}, {Color.green, 15}, {Color.blue, 60} }; Dictionary<Color, int> trueCountDict = new Dictionary<Color, int> () { {Color.red, 0}, {Color.green, 0}, {Color.blue, 0} }; int calcCount = 1000000; for (int i = 0; i < calcCount; i++) { trueCountDict [ProbabilityCalclator.DetermineFromDict<Color> (targetDict)]++; } string logText = "抽選の精度確認\n試行回数 : " + calcCount.ToString() + "回\n"; foreach (KeyValuePair<Color, int> pair in targetDict) { logText += pair.Key.ToString() + " 目標 : " + pair.Value.ToString() + "%, 実際 : " + ((float)trueCountDict[pair.Key] / (float)calcCount * 100f).ToString() + "%\n"; } Debug.Log (logText); } }
というのを作りまして、説明とおりに"ContextMenu"のRUNで実行してログを出すことはできました。
が、しかし・・・これを実際ガチャとして動かすにはどうすればいいのかがまったく分からず、検索してもunityでガチャを作るといった初歩が参考できるサイトも無くこまっております。
先がながいと思いマスが・・・目標としているクロッシーロードのガチャのようなシステムを作る為に何からはじめればいいのか・・・教えていただけないでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/14 01:33