close
  • 此篇重點

此篇重點主要是著重在Unity遊戲中的數據存取,以及如何做資料的處理

  • 此篇效果 -  利用物件JSON格式做到檔案的存取

我們先來假設我們今天要做的一個場景是人物基本資訊,而人物的資訊如下面所示

  • 名稱 (name)
  • 等級 (level)
  • 攻擊力 (attack)
  • 防禦力 (defence)

 

為此我們要新增一個class,裡面包含這些物件屬性的類別 

player.cs


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

[System.Serializable]

public class player {

 

    public string name;

    public int level;

    public float attack;

    public float defence;

 

}


接下來就是要做建立資料的動作,先開新的一個script

using System.Collections;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

 

public class SaveData : MonoBehaviour {

 

    void Start(){

        //建立一個player物件

        player p1 = new player();

 

        //設置p1內的各屬性

        p1.name = "Tom";

        p1.level = 50;

        p1.attack = 600;

        p1.defence = 200;

       

        //將此player裡面的屬性轉成string(json格式)

        string saveString = JsonUtility.ToJson(p1);

    }

}


做好此動作後就是要將json格式的string存到asset裡面,我們先在unity 專案內部新增一個GameJSONData的資料夾以供儲存地使用

 

接者將剛剛的code多加這幾行

        //將字串saveString存到硬碟中

        StreamWriter file = new StreamWriter(System.IO.Path.Combine("Assets/GameJSONData", "Player1.json"));

        file.Write(saveString);

        file.Close();


整個code架構會變成

using System.Collections;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

 

public class SaveData : MonoBehaviour {

 

    void Start(){

        //建立一個player物件

        player p1 = new player();

 

        //設置p1內的各屬性

        p1.name = "Tom";

        p1.level = 50;

        p1.attack = 600;

        p1.defence = 200;

       

        //將此player裡面的屬性轉成string(json格式)

        string saveString = JsonUtility.ToJson(p1);

       

        //將字串saveString存到硬碟中

        StreamWriter file = new StreamWriter(System.IO.Path.Combine("Assets/GameJSONData", "Player1.json"));

        file.Write(saveString);

        file.Close();

    }

}


另外一方面,若是要在人物身上增加他所擁有的裝備我們可以在新增一個class叫做equipment(甚麼名字都行)

 

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

 

[System.Serializable]

public class equipment{

    public string equipmentName;

    public int limitLevel;

    public int attack;

}


而在人物player.cs這邊,我們新增一個List屬性

public List<equipment> equipment = new List<equipment>();


在SaveData的code我們新增下面幾段

        equipment eq1 = new equipment();

        eq1.attack = 87;

        eq1.limitLevel = 50;

        eq1.equipmentName = "Excalibur";

        p1.equipment.Add(eq1);


這樣就可以在GameJSONData的資料夾中發現有一個新的json檔,裡面的文字如下

{"name":"Tom","level":50,"attack":600.0,"defence":200.0,"equipment":[{"equipmentName":"Excalibur","limitLevel":50,"attack":87}]}

可以發現很順利的把角色資訊存進去了


除此之外因為這只是一個簡單的資料結構寫法

當你的檔案變大之後,資料的存取變得複雜

有時候可以採用getter and setter的寫法

像是下面所示

    public void setName(string name){

        this.name = name;

    }

 

    public string getName(){

        return this.name;

    }


寫完儲存我們就來進行讀取的動作了

using System.Collections;

using System.Collections.Generic;

using System.IO;

using UnityEngine;

 

public class ReadData : MonoBehaviour {

 

    // Use this for initialization

    void Start () {

       

        player p1 = new player();

 

        //做一個讀取器

        StreamReader fileReader = new StreamReader(System.IO.Path.Combine("Assets/GameJSONData", "Player1.json"));

        string stringJson = fileReader.ReadToEnd();

        fileReader.Close();

 

        //將讀取的string改成player物件型態

        p1 = JsonUtility.FromJson<player>(stringJson);

 

        print("Name:" + p1.name);

 

    }

}

 

這樣就可以方便的讀取json的內容,看之後要進行刪除或修改都很方便

 

這此的code,若有興趣的人可以在連結下載練習看看

-------------------------------------------------------------------------------------------------------------------------------------

 

我們有粉絲專頁囉!!!       

如果怕有問題連絡不到我們,歡迎隨時私訊FB粉專哦!!!

https://www.facebook.com/Straying2018/

 

●     如果這篇文章有幫助到您,希望您能留言給予我們鼓勵 !  

●     對於文章有任何的建議,非常歡迎留言告訴我們哦 !!!  

●     有任何想知道的功能也歡迎告訴我們,我們會盡快寫成部落格分享給大家!

 

arrow
arrow

    twilighthook 發表在 痞客邦 留言(0) 人氣()