當進行Json數據存檔、讀檔的操作的時候,Class Items爲父類、
Class Equipment爲子類繼承父類Items的部分變量、
Class Food爲子類繼承父類Items的部分變量
注意:數據不能繼承MonoBehaviour,否則會出現相關資料3的 錯誤。保存的數據僅僅保存instanceID。
JSONDemon.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Linq;//.OfType
using System;
public class JSONDemon : MonoBehaviour {
...
private void Start()
{
testData();
}//start
public void testData()
{
Test_2 test_2 = new Test_2();
test_2.name = "test_2";
test_2.num = 2;
test_2.score = 2;
test_2s.Add(test_2);
}//testData
...
}
[System.Serializable]
public class Test_1
{
public string name;
public int score;
}
[System.Serializable]
public class Test_2: Test_1
{
//public string name;
public int num;
}
類Test_1
[System.Serializable]
public class Test_1
{
public string name;
public int score;
}
類Test_2繼承類Test_1
[System.Serializable]
public class Test_2: Test_1
{
//public string name;
public int num;
}
class Test_2: Test_1
相當於是Test_2 把 類Test_1中的 聲明重新聲明一次。繼承的作用。
[System.Serializable]
public class Test_2
{
#region Test_1
public string name;
public int score;
#endregion
//public string name;
public int num;
}
相關資料:
1.
[Unity][存檔][JSONUnity]如何同時保存地圖數據和玩家數據
2.
[Unity][存檔][JSONUnity]存儲讀取方塊數據
3.
[Unity][存檔][JSONUnity]讀取方塊數據爲什麼只能保存instanceID
4.