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でネットゲームを作りたい【2】Json取入

Last updated at Posted at 2025-01-12

前回jsonファイル作成ソフト作成まで
なんかすげー遠回りしてるようだが、進んではいるはず

では、それを読み込まなければ、書き込んで変更できなければ!って事で再度ググってみます。そしてやっと気が付きます

C#、オブジェクト指向なんぞや?

色々見てまわりました
ん~なんとなく理解(できてない
今まで、順番にプログラムを書いてた者にとって、これはむずい


まずはノコノコさんの所でC#のお勉強
これはわかりやすい!!でも、普通のプログラムとほぼ変わらんね
オブジェクト指向が理解できん
で、さらに検索した結果

オブジェクト 
もの?おおまかなくくり的な...

クラス
Geminiさんに聞いてみた
・クラス: オブジェクトの属性(変数)と動作(メソッド)を定義したテンプレートです。
・インスタンス: クラスに基づいてメモリ上に生成されたオブジェクトの実体です。
・インスタンス化: クラスからインスタンスを生成することです。
基本この呪文でわからなくなりますよね

// 車のクラス
public class Car
{
    public string Color { get; set; }
    public string Maker { get; set; }
    public int Year { get; set; }

    public void Run()
    {
        Console.WriteLine("車が走ります");
    }
}

// インスタンスの生成
Car myCar = new Car();
myCar.Color = "赤";
myCar.Maker = "トヨタ";
myCar.Year = 2023;
myCar.Run(); // 出力: 車が走ります

このコードでは、Car クラスから myCar というインスタンスを生成し、その色、メーカー、年式を設定しています。そして、Run メソッドを呼び出すことで、車が走る動作を実行しています。

これが一番わかりやすかった
クラス内に書かれた動作をメソッドと言うみたい

public string Color { get; set; }

クラス外の myCar.Color = "赤"; これで指定?入力?どう使うのか?


public void Run()
    {
        Console.WriteLine("車が走ります");
    }

Car myCar = new Car();
myCar.Run(); // 出力: 車が走ります

これはなんとなく...
クラス内に記載した動作プログラムを他で使えるって事かな


よくわからんが、とりあえず進んでみよう!(前向き
本題 UnityでJsonを読込み使えるようにしたい

メモの穴こちらでjsonを読み込む方法を参照しました
ちょいと、自分のJsonに合わせた形で作り変えてみる
Asset>Resources>Item.json

{
"item": [
  {"name":"速い","HP":"0","ST":"0","DE":"0","SP":"1"},
  {"name":"固い","HP":"0","ST":"0","DE":"1","SP":"0"}
  ]
}

Itemallget.cs

using System;
using UnityEngine;

[Serializable]
public class InputJson
{
    public AAA[] item;
}

[Serializable]
public class AAA
{
    public string name;
    public string HP;
    public string ST;
    public string DE;
    public string SP;
}

public class ItemAllGet: MonoBehaviour
{
    void Start()
    {
        string inputString = Resources.Load<TextAsset>("ItemJson").ToString();
        InputJson inputJson = JsonUtility.FromJson<InputJson>(inputString);
        Debug.Log(inputJson.item[0].name);  // 速い
        Debug.Log(inputJson.item[1].DE);  // 1
    }
}

こちらは空のGameObjectに張り付けて使用します
するとAsset>Resources>Itemjsonを読み込み認識してくれます
itemという配列にname,HP...と書いてあるのを読み込んでitem[0]のnameは?と参照できるようになってます

using System;
using UnityEngine;

UnityEngineは必須なんだろねー
namespaseは無いのか

[Serializable]
public class InputJson
{
    public AAA[] item;
}

[Serializable]これを書くと「インスペクターに サブプロパティを埋め込むことができるようになります。」
ってよくわからんがまぁ次
InputJsonってクラス
ここで配列を指定してるみたいです

[Serializable]
public class AAA
{
    public string name;
    public string HP;
    public string ST;
    public string DE;
    public string SP;
}

ここで配列の中身を指定してるみたい
AAAクラス?さっきの配列のAAA[]とリンクしてる?
ならさっきのInputJsonクラスに一緒に書き込めないのか?
HPとかは数字だからintのがいいのか

public class ItemAllGet: MonoBehaviour
{

Unityに必須な書き方
クラス名はファイル名と同じでないとエラーが出る(大文字小文字まで判断)

    void Start()
    {
        string inputString = Resources.Load<TextAsset>("ItemJson").ToString();
        InputJson inputJson = JsonUtility.FromJson<InputJson>(inputString);
        Debug.Log(inputJson.item[0].name);  // 速い
        Debug.Log(inputJson.item[1].DE);  // 1
    }

Unityの動作コードはこのvoid start()に書き込むみたい

・string inputString = Resources.Load("ItemJson").ToString();
inputStringって変数にResourcesフォルダーの中のItemjsonを読み込めって事かな

・InputJson inputJson = JsonUtility.FromJson(inputString);
初見はこれが謎でした
大文字のInputJson、小文字のinputJsonで=???と
勉強したので↓
InputJsonクラスからinputJsonというインスタンスを生成?(むー....
さっきの変数inputStringのJson形式をinputJson配列にここで使える配列型に書きますよって事かな

でinputJson.item[0].nameと指定する

フムフムなんとなく理解できた気がします
勉強した甲斐あった
何も理解せずにコピペで済ませてたら、先で躓くしね

まぁこれでjson読み込める


Unityでは、基本的なデータ読み込みとかを空のGameObjectにクラスを配置して、画面に配置した画像、ボタン、テキスト等に個別に動作の仕方を.csにて配置すれば動く(表示とか画面移動)

画像(ボタン)をタップしたら、戦闘画面に切り替わって戦闘して結果データをjsonファイルに記載して、メイン画面戻ったら再度データ読み込みして表示ってのが出来るんでないかと考察

←前 【Unity初心者】Unityでネットゲームを作りたい【1】色々悩む~ExcelでJsonの配列を作る
【Unity初心者】Unityでネットゲームを作りたい【3】PlayFabログインと同時にやりたい 次→

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?