質問編集履歴

3

進捗があったため

2017/03/16 06:56

投稿

K3E1N
K3E1N

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,385 +1,251 @@
1
- **【0315追加】**
1
+ **【0316追加】**
2
-
3
- ちょっと進んで、早速つみました・・・
2
+
4
-
5
-
6
-
7
- まずはsakura_hanaさんが指摘していただいたとおりに一つずつ解決してみよう。という感じで
8
-
9
- 0. uGUIでボタンを作ろう。
10
-
11
- 0. 変数で条件分岐しよう。アラート画面はuGUIで作ろう。
12
-
13
- 0. 変数を操作しよう。※ゲーム終了時に消えないように変数をどう保存する?
14
-
15
- 0. 質問文内にあるランダム生成スクリプトを利用しよう。※景品リストはどう持つ?
16
-
17
- 0. ※どんな方法で「アイテムの所持状態」を管理する?
18
-
19
-
20
-
21
- これの1番と2番に挑戦しました。
22
-
23
- - まず1番
24
-
25
- GachaBOX
26
-
27
- ├GachaButton //ガチャを押すボタン
28
-
29
- │└TEXT
30
-
31
- ├Coin //コインの画像とコイン枚数表示
32
-
33
- │└Coin(Text)
34
-
35
- └PopUP //コインがないときのメッセージウィンドウ
36
-
37
- │└text
38
-
39
- ├no(ボタン) //コインがないボタン
40
-
41
- └yes(ボタン) //コインがあるときの閉じるボタン
42
-
43
- こんな構成にしてみました。
44
-
45
- .
46
-
47
-
48
-
49
- - 次2番分岐スクリプトを作っした。
3
+ 進められるようなったで、最後にまとめ掲載しす!
4
+
50
-
5
+ ---
6
+
51
- ↓これでpublic int mycoin = 150いじればログとてはガチできるか、足りないかを出すことはきましたが・・・Getobjectしても呼び出すことができませんでした。
7
+ 以前、キャラクターがアイテム取得ラクターが発射されキャラクターの銃弾の色をえる。とう内容を皆様のお作ることができました。
8
+
9
+
10
+
52
-
11
+ 今回はクロッシーロードのように、ポイントをもってガチャできるシステムにチャレンジしております。
12
+
13
+
14
+
15
+ まず[コードを参考にしたサイトはこちら](http://kan-kikuchi.hatenablog.com/entry/ProbabilityCalclator)です。
16
+
17
+
18
+
19
+ unity のprojectに
20
+
21
+ - __ProbabilityCalclator.cs__と
22
+
53
- ```C#
23
+ ```ここに言語を入力
54
24
 
55
25
  using UnityEngine;
56
26
 
57
27
  using System.Collections;
58
28
 
59
- using UnityEngine.UI;
60
-
61
-
62
-
63
- public class GachaScript : MonoBehaviour {
64
-
65
-
66
-
67
- public Text mycoinText; //Textよう変数
68
-
69
- public int mycoin = 150; //コイン計算用変数
70
-
71
- public GameObject kaenai;
72
-
73
-
74
-
75
- void update(){
76
-
77
- mycoinText.text = "Coin: 0"; //初期所持コイン数
78
-
79
- }
80
-
81
-
82
-
83
- public void ButtonPush ()
84
-
85
- {
86
-
87
- if (mycoin >= 100) {
88
-
89
- Debug.Log ("ガチャできるで");
90
-
91
- }
92
-
93
- if (mycoin < 100) {
94
-
95
- Debug.Log ("ポップアップさせたいけど分からない!");
96
-
97
- }
98
-
99
- }
29
+ using System.Collections.Generic;
30
+
31
+
32
+
33
+ public static class ProbabilityCalclator {
34
+
35
+
36
+
37
+ public static bool DetectFromPercent(int percent){
38
+
39
+ return DetectFromPercent ((float)percent);
40
+
41
+ }
42
+
43
+
44
+
45
+ public static bool DetectFromPercent(float percent){
46
+
47
+ int digitNum = 0;
48
+
49
+ if(percent.ToString().IndexOf(".") > 0){
50
+
51
+ digitNum = percent.ToString ().Split('.')[1].Length;
52
+
53
+ }
54
+
55
+ int rate = (int)Mathf.Pow (10, digitNum);
56
+
57
+ int randomValueLimit = 100 * rate;
58
+
59
+ int border = (int)(rate * percent);
60
+
61
+
62
+
63
+ return Random.Range (0, randomValueLimit) < border;
64
+
65
+ }
66
+
67
+
68
+
69
+ public static T DetermineFromDict<T>(Dictionary<T, int> targetDict){
70
+
71
+ Dictionary<T, float> targetFloatDict = new Dictionary<T, float>();
72
+
73
+
74
+
75
+ foreach (KeyValuePair<T, int> pair in targetDict) {
76
+
77
+ targetFloatDict.Add(pair.Key, (float)pair.Value);
78
+
79
+ }
80
+
81
+
82
+
83
+ return DetermineFromDict (targetFloatDict);
84
+
85
+ }
86
+
87
+
88
+
89
+ public static T DetermineFromDict<T>(Dictionary<T, float> targetDict){
90
+
91
+
92
+
93
+ float totalPer = 0;
94
+
95
+ foreach (float per in targetDict.Values) {
96
+
97
+ totalPer += per;
98
+
99
+ }
100
+
101
+
102
+
103
+ float rand = Random.Range (0, totalPer);
104
+
105
+
106
+
107
+ foreach (KeyValuePair<T, float> pair in targetDict) {
108
+
109
+ rand -= pair.Value;
110
+
111
+
112
+
113
+ if(rand < 0){
114
+
115
+ return pair.Key;
116
+
117
+ }
118
+
119
+ }
120
+
121
+
122
+
123
+ Debug.LogError ("抽選ができませんでした");
124
+
125
+ return new List<T>(targetDict.Keys)[0];
126
+
127
+ }
128
+
129
+
100
130
 
101
131
  }
102
132
 
103
-
104
-
105
133
  ```
106
134
 
107
- ↓このコードは開いたパネル閉じるようにしたかった
135
+ - __GachaTEST.cs__作成まし
108
-
136
+
109
- ```c#
137
+ ```ここに言語を入力
138
+
139
+ using UnityEngine;
110
140
 
111
141
  using System.Collections;
112
142
 
113
143
  using System.Collections.Generic;
114
144
 
115
- using UnityEngine;
145
+
116
-
117
-
118
-
146
+
119
- public class closebutton : MonoBehaviour {
147
+ public class GachaTEST : MonoBehaviour {
148
+
149
+
150
+
151
+
152
+
120
-
153
+ [ContextMenu("Run")]
121
-
122
-
154
+
123
- public void ButtonPush (){
155
+ private void Test(){
124
-
156
+
125
- Debug.Log ("閉じ方が分からない!");
157
+ DetectFromPercent ();
158
+
126
-
159
+ DetermineFromDict ();
127
-
128
-
160
+
129
- }
161
+ }
162
+
163
+
164
+
130
-
165
+ private void DetectFromPercent(){
166
+
167
+
168
+
131
-
169
+ int trueCount = 0;
170
+
132
-
171
+ int calcCount = 1000000;
172
+
173
+ float per = 0.2f;
174
+
175
+
176
+
177
+ for (int i = 0; i < calcCount; i++) {
178
+
179
+ if (ProbabilityCalclator.DetectFromPercent (per))
180
+
181
+ trueCount++;
182
+
133
- }
183
+ }
184
+
185
+
186
+
134
-
187
+ Debug.Log ("真偽判定の精度確認\n試行回数 : " + calcCount + "回, 目標 : " + per + "%, 実際 : " + ((float)trueCount / (float)calcCount * 100f) + "%");
188
+
189
+
190
+
135
-
191
+ }
192
+
193
+
194
+
195
+ private void DetermineFromDict(){
196
+
197
+
198
+
199
+ Dictionary<Color, int> targetDict = new Dictionary<Color, int> () {
200
+
201
+ {Color.red, 25}, {Color.green, 15}, {Color.blue, 60}
202
+
203
+ };
204
+
205
+ Dictionary<Color, int> trueCountDict = new Dictionary<Color, int> () {
206
+
207
+ {Color.red, 0}, {Color.green, 0}, {Color.blue, 0}
208
+
209
+ };
210
+
211
+
212
+
213
+ int calcCount = 1000000;
214
+
215
+
216
+
217
+ for (int i = 0; i < calcCount; i++) {
218
+
219
+ trueCountDict [ProbabilityCalclator.DetermineFromDict<Color> (targetDict)]++;
220
+
221
+ }
222
+
223
+
224
+
225
+ string logText = "抽選の精度確認\n試行回数 : " + calcCount.ToString() + "回\n";
226
+
227
+
228
+
229
+ foreach (KeyValuePair<Color, int> pair in targetDict) {
230
+
231
+ logText += pair.Key.ToString() + " 目標 : " + pair.Value.ToString() + "%, 実際 : "
232
+
233
+ + ((float)trueCountDict[pair.Key] / (float)calcCount * 100f).ToString() + "%\n";
234
+
235
+ }
236
+
237
+
238
+
239
+ Debug.Log (logText);
240
+
241
+ }
242
+
243
+
244
+
245
+ }
136
246
 
137
247
  ```
138
248
 
139
- ---
140
-
141
- 以前、キャラクターがアイテムを取得しキャラクターが発射されるキャラクターの銃弾の色をかえる。という内容を皆様のおかげで作ることができました。
142
-
143
-
144
-
145
- 今回はクロッシーロードのように、ポイントをもってガチャできるシステムにチャレンジしております。
146
-
147
-
148
-
149
- まず[コードを参考にしたサイトはこちら](http://kan-kikuchi.hatenablog.com/entry/ProbabilityCalclator)です。
150
-
151
-
152
-
153
- unity のprojectに
154
-
155
- - __ProbabilityCalclator.cs__と
156
-
157
- ```ここに言語を入力
158
-
159
- using UnityEngine;
160
-
161
- using System.Collections;
162
-
163
- using System.Collections.Generic;
164
-
165
-
166
-
167
- public static class ProbabilityCalclator {
168
-
169
-
170
-
171
- public static bool DetectFromPercent(int percent){
172
-
173
- return DetectFromPercent ((float)percent);
174
-
175
- }
176
-
177
-
178
-
179
- public static bool DetectFromPercent(float percent){
180
-
181
- int digitNum = 0;
182
-
183
- if(percent.ToString().IndexOf(".") > 0){
184
-
185
- digitNum = percent.ToString ().Split('.')[1].Length;
186
-
187
- }
188
-
189
- int rate = (int)Mathf.Pow (10, digitNum);
190
-
191
- int randomValueLimit = 100 * rate;
192
-
193
- int border = (int)(rate * percent);
194
-
195
-
196
-
197
- return Random.Range (0, randomValueLimit) < border;
198
-
199
- }
200
-
201
-
202
-
203
- public static T DetermineFromDict<T>(Dictionary<T, int> targetDict){
204
-
205
- Dictionary<T, float> targetFloatDict = new Dictionary<T, float>();
206
-
207
-
208
-
209
- foreach (KeyValuePair<T, int> pair in targetDict) {
210
-
211
- targetFloatDict.Add(pair.Key, (float)pair.Value);
212
-
213
- }
214
-
215
-
216
-
217
- return DetermineFromDict (targetFloatDict);
218
-
219
- }
220
-
221
-
222
-
223
- public static T DetermineFromDict<T>(Dictionary<T, float> targetDict){
224
-
225
-
226
-
227
- float totalPer = 0;
228
-
229
- foreach (float per in targetDict.Values) {
230
-
231
- totalPer += per;
232
-
233
- }
234
-
235
-
236
-
237
- float rand = Random.Range (0, totalPer);
238
-
239
-
240
-
241
- foreach (KeyValuePair<T, float> pair in targetDict) {
242
-
243
- rand -= pair.Value;
244
-
245
-
246
-
247
- if(rand < 0){
248
-
249
- return pair.Key;
250
-
251
- }
252
-
253
- }
254
-
255
-
256
-
257
- Debug.LogError ("抽選ができませんでした");
258
-
259
- return new List<T>(targetDict.Keys)[0];
260
-
261
- }
262
-
263
-
264
-
265
- }
266
-
267
- ```
268
-
269
- - __GachaTEST.cs__を作成しました。
270
-
271
- ```ここに言語を入力
272
-
273
- using UnityEngine;
274
-
275
- using System.Collections;
276
-
277
- using System.Collections.Generic;
278
-
279
-
280
-
281
- public class GachaTEST : MonoBehaviour {
282
-
283
-
284
-
285
-
286
-
287
- [ContextMenu("Run")]
288
-
289
- private void Test(){
290
-
291
- DetectFromPercent ();
292
-
293
- DetermineFromDict ();
294
-
295
- }
296
-
297
-
298
-
299
- private void DetectFromPercent(){
300
-
301
-
302
-
303
- int trueCount = 0;
304
-
305
- int calcCount = 1000000;
306
-
307
- float per = 0.2f;
308
-
309
-
310
-
311
- for (int i = 0; i < calcCount; i++) {
312
-
313
- if (ProbabilityCalclator.DetectFromPercent (per))
314
-
315
- trueCount++;
316
-
317
- }
318
-
319
-
320
-
321
- Debug.Log ("真偽判定の精度確認\n試行回数 : " + calcCount + "回, 目標 : " + per + "%, 実際 : " + ((float)trueCount / (float)calcCount * 100f) + "%");
322
-
323
-
324
-
325
- }
326
-
327
-
328
-
329
- private void DetermineFromDict(){
330
-
331
-
332
-
333
- Dictionary<Color, int> targetDict = new Dictionary<Color, int> () {
334
-
335
- {Color.red, 25}, {Color.green, 15}, {Color.blue, 60}
336
-
337
- };
338
-
339
- Dictionary<Color, int> trueCountDict = new Dictionary<Color, int> () {
340
-
341
- {Color.red, 0}, {Color.green, 0}, {Color.blue, 0}
342
-
343
- };
344
-
345
-
346
-
347
- int calcCount = 1000000;
348
-
349
-
350
-
351
- for (int i = 0; i < calcCount; i++) {
352
-
353
- trueCountDict [ProbabilityCalclator.DetermineFromDict<Color> (targetDict)]++;
354
-
355
- }
356
-
357
-
358
-
359
- string logText = "抽選の精度確認\n試行回数 : " + calcCount.ToString() + "回\n";
360
-
361
-
362
-
363
- foreach (KeyValuePair<Color, int> pair in targetDict) {
364
-
365
- logText += pair.Key.ToString() + " 目標 : " + pair.Value.ToString() + "%, 実際 : "
366
-
367
- + ((float)trueCountDict[pair.Key] / (float)calcCount * 100f).ToString() + "%\n";
368
-
369
- }
370
-
371
-
372
-
373
- Debug.Log (logText);
374
-
375
- }
376
-
377
-
378
-
379
- }
380
-
381
- ```
382
-
383
249
  というのを作りまして、説明とおりに"ContextMenu"のRUNで実行してログを出すことはできました。
384
250
 
385
251
 

2

内容追加

2017/03/16 06:56

投稿

K3E1N
K3E1N

スコア11

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,143 @@
1
+ **【0315追加】**
2
+
3
+ ちょっと進んで、早速つみました・・・
4
+
5
+
6
+
7
+ まずはsakura_hanaさんが指摘していただいたとおりに一つずつ解決してみよう。という感じで
8
+
9
+ 0. uGUIでボタンを作ろう。
10
+
11
+ 0. 変数で条件分岐しよう。アラート画面はuGUIで作ろう。
12
+
13
+ 0. 変数を操作しよう。※ゲーム終了時に消えないように変数をどう保存する?
14
+
15
+ 0. 質問文内にあるランダム生成スクリプトを利用しよう。※景品リストはどう持つ?
16
+
17
+ 0. ※どんな方法で「アイテムの所持状態」を管理する?
18
+
19
+
20
+
21
+ これの1番と2番に挑戦しました。
22
+
23
+ - まず1番
24
+
25
+ GachaBOX
26
+
27
+ ├GachaButton //ガチャを押すボタン
28
+
29
+ │└TEXT
30
+
31
+ ├Coin //コインの画像とコイン枚数表示
32
+
33
+ │└Coin(Text)
34
+
35
+ └PopUP //コインがないときのメッセージウィンドウ
36
+
37
+ │└text
38
+
39
+ ├no(ボタン) //コインがないボタン
40
+
41
+ └yes(ボタン) //コインがあるときの閉じるボタン
42
+
43
+ こんな構成にしてみました。
44
+
45
+ .
46
+
47
+
48
+
49
+ - 次に2番分岐のスクリプトを作ってみました。
50
+
51
+ ↓これでpublic int mycoin = 150をいじればログとしてはガチャできるか、足りないかを出すことはできましたが・・・Getobjectしても呼び出すことができませんでした。
52
+
53
+ ```C#
54
+
55
+ using UnityEngine;
56
+
57
+ using System.Collections;
58
+
59
+ using UnityEngine.UI;
60
+
61
+
62
+
63
+ public class GachaScript : MonoBehaviour {
64
+
65
+
66
+
67
+ public Text mycoinText; //Textよう変数
68
+
69
+ public int mycoin = 150; //コイン計算用変数
70
+
71
+ public GameObject kaenai;
72
+
73
+
74
+
75
+ void update(){
76
+
77
+ mycoinText.text = "Coin: 0"; //初期所持コイン数
78
+
79
+ }
80
+
81
+
82
+
83
+ public void ButtonPush ()
84
+
85
+ {
86
+
87
+ if (mycoin >= 100) {
88
+
89
+ Debug.Log ("ガチャできるで");
90
+
91
+ }
92
+
93
+ if (mycoin < 100) {
94
+
95
+ Debug.Log ("ポップアップさせたいけど分からない!");
96
+
97
+ }
98
+
99
+ }
100
+
101
+ }
102
+
103
+
104
+
105
+ ```
106
+
107
+ ↓このコードは開いたパネルを閉じるようにしたかった
108
+
109
+ ```c#
110
+
111
+ using System.Collections;
112
+
113
+ using System.Collections.Generic;
114
+
115
+ using UnityEngine;
116
+
117
+
118
+
119
+ public class closebutton : MonoBehaviour {
120
+
121
+
122
+
123
+ public void ButtonPush (){
124
+
125
+ Debug.Log ("閉じ方が分からない!");
126
+
127
+
128
+
129
+ }
130
+
131
+
132
+
133
+ }
134
+
135
+
136
+
137
+ ```
138
+
139
+ ---
140
+
1
141
  以前、キャラクターがアイテムを取得しキャラクターが発射されるキャラクターの銃弾の色をかえる。という内容を皆様のおかげで作ることができました。
2
142
 
3
143
 

1

誤字修正

2017/03/15 09:20

投稿

K3E1N
K3E1N

スコア11

test CHANGED
File without changes
test CHANGED
@@ -248,4 +248,4 @@
248
248
 
249
249
 
250
250
 
251
- 先が名外と思いマスが・・・目標としているクロッシーロードのガチャのようなシステムを作る為に何からはじめればいいのか・・・教えていただけないでしょうか?
251
+ 先がながいと思いマスが・・・目標としているクロッシーロードのガチャのようなシステムを作る為に何からはじめればいいのか・・・教えていただけないでしょうか?