0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Unity初心者】Unityでネットゲームを作りたい【17】パネル間でデータの引継ぎをしたい~どのボタンが押されてるか判断して次のパネルへ~Jsonファイルの読み込みが早すぎる?

Last updated at Posted at 2025-02-06

パネル間でデータの引継ぎをしたい

前は勝手に using static PlayFabLogin; とか出て形?を引き継いでくれたけど
a = 1 とかのデータが引き継がない
毎度おなじみ調べました
【Unity】変数の受け渡し まとめ【入門】こちら参照

 //Sousaku.csの値を引き継ぐ
 Sousaku sousaku;
 sousaku = Panel.GetComponent<Sousaku>();
 
 GameObject HP = GameObject.Find("HP7");
 Text HP_text = HP.GetComponent<Text>();
 HP_text.text = sousaku.hp;

こんな感じで テキスト HP7 に表示できた


どのボタンが押されてるか判断して次のパネルへ

ってのをやりたかった
これも調べまくったけど、なんかむずかしい...

そこで考えた
ボタン押したら、今これを押しましたと番号をどっかに保存して、次のパネルで読み込めば?と
PlayerPrefs これ使えるのかな? ログイン時に使ってたけど、ID上書きとかしたら大変よなーと触れなかったが、よくよく見ると普通に使えそう
5つのボタンを画像のバックに配置して、選択したら色が変わるようにして...

using UnityEngine;
using UnityEngine.UI;

public class Hanten : MonoBehaviour
{
    public int sentaku;
    public void Clickhere()
    {
        //このボタンの色を変える
        GameObject thisButton = GameObject.Find("Button (1)");
        Button btn = thisButton.GetComponent<Button>();
        btn.image.color = Color.cyan;

        // 他の色を白に変更
        GameObject Button_2 = GameObject.Find("Button (2)");
        Button btn_2 = Button_2.GetComponent<Button>();
        btn_2.image.color = Color.white;

        GameObject Button_3 = GameObject.Find("Button (3)");
        Button btn_3 = Button_3.GetComponent<Button>();
        btn_3.image.color = Color.white;

        GameObject Button_4 = GameObject.Find("Button (4)");
        Button btn_4 = Button_4.GetComponent<Button>();
        btn_4.image.color = Color.white;

        GameObject Button_5 = GameObject.Find("Button (5)");
        Button btn_5 = Button_5.GetComponent<Button>();
        btn_5.image.color = Color.white;

        sentaku = 0;
        PlayerPrefs.SetInt("Sentaku", sentaku);
    }
}

ボタンを押したら、そのボタンだけ色を変更し、ほかのボタンは白に
で、

sentaku = 0;
PlayerPrefs.SetInt("Sentaku", sentaku);

このボタンを押したから sentaku = 0 ですよと
これを次のパネルで PlayerPrefs.GetInt("Sentaku", sentaku); 呼び出せばOKだ!
他のボタンにも色を変えたのと、数字を1とかに変えたスクリプトをはめ込んだ

うまくいきました

あれ?
これ、さっきのデータ引継ぎに使えるんじゃ?
PlayerPrefs.SetString("NewHP", sousaku.hp);
これでできました

まぁお勉強ですね


Jsonファイルの読み込みが早すぎる?

配列を入れ替えてJsonファイルに上書き、んで読み込んでこうなりました とやりたいのだが、変更前のデータが表示されてしまう
遅らせたりいろいろやったのだが、どうもうまくいかない...

あぁこれ、Jsonファイル使わないでPlayFabのプレーヤーデータだけでやり取りしたほうがよさげだ
読込をPlayfabだけにしてみよう

ってやったら成功!

読込んで書き込んで再読み込みがスムーズにできた

そしてここで壁にぶちあたる

←前 【Unity初心者】Unityでネットゲームを作りたい【16】謎のエラーで悩む~パネルでボタンを押したら、このパネルを消して別パネル表示

【Unity初心者】Unityでネットゲームを作りたい【18】Unityで正常、ビルドしてandroidで異常 次→

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?