teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

key変更

2019/01/06 06:54

投稿

bochan2
bochan2

スコア2050

answer CHANGED
@@ -19,8 +19,8 @@
19
19
  a=PlayerPrefs.GetInt(key);
20
20
  }
21
21
  int m=0;
22
- if(PlayerPrefs.HasKey("ORB")){
22
+ if(PlayerPrefs.HasKey("SCORE")){
23
- m=PlayerPrefs.GetInt("ORB");
23
+ m=PlayerPrefs.GetInt("SCORE");
24
24
  }
25
25
  if(m<prices[id]){
26
26
  moneydialog.SetActive(true);
@@ -29,6 +29,6 @@
29
29
  m-=prices[id];
30
30
  a+=1;
31
31
  PlayerPrefs.SetInt(key,a);
32
- PlayerPrefs.SetInt("ORB",m);
32
+ PlayerPrefs.SetInt("SCORE",m);
33
33
  }
34
34
  ```

1

スクリプトを追加

2019/01/06 06:54

投稿

bochan2
bochan2

スコア2050

answer CHANGED
@@ -1,3 +1,34 @@
1
1
  質問いただきありがとうございます!
2
2
  大まかな方針としてはゲーム内通貨をPlayerPrefsに書き込んで処理するのが良いと思います。
3
+
4
+
5
+ ###コード
6
+ これを好きなクラス名にして(MonoBehaviorは継承)ショップのシーンに貼り付けて下さい
7
+ UIのボタンからBuyを指定するとイベントを登録出来ます。
3
- 得点の処理部分のスクリプト追記していただければ動くコードを載せれしい場合はお願いします。
8
+ pricesinspector画面から指定すと値段を設定きます(設定とエラー吐きます
9
+ moneydialogはお金が足りないときに表示されるダイアログのgameobjectです。使う場合は閉じるボタンにイベントを設定して下さい。いらなかったら消してください
10
+
11
+ アイテムの種類が多いのであればscriptableObjectを使った方がいいと思います
12
+ ```C#
13
+ public GameObject moneydialog;
14
+ public List<int> prices;
15
+ public void Buy(int id){
16
+ int a=0;
17
+ string key="ITEM"+id;
18
+ if(PlayerPrefs.HasKey(key)){
19
+ a=PlayerPrefs.GetInt(key);
20
+ }
21
+ int m=0;
22
+ if(PlayerPrefs.HasKey("ORB")){
23
+ m=PlayerPrefs.GetInt("ORB");
24
+ }
25
+ if(m<prices[id]){
26
+ moneydialog.SetActive(true);
27
+ return;
28
+ }
29
+ m-=prices[id];
30
+ a+=1;
31
+ PlayerPrefs.SetInt(key,a);
32
+ PlayerPrefs.SetInt("ORB",m);
33
+ }
34
+ ```