Unity3d - C# - How can I back up of values of an array?

Armamedia

I Defined an array of 67 elements in a script (C#). Each element is an object of a class that I defined that includes some variables. Like Below:

// the Definition of the array
public Pack[] packsInfo;

// the class 
[Serializable]
public class Pack
{
    public int number;
    public int angle;
    public float zPosition;
    public float beatCaseDistance;
    public float gunDistance;
}

Then, I Assign all the values in Inspector. like Below:

enter image description here

So ... if i suddenly change any of the values or even worse, ( the values gone for a any reason. like changing the class parameters or any thing ) then I must spend hours on setting up all the values. So i want to print all the values in a text, or a file and save them. for recovering them later. i want a backup solution.I want to do it whit Programming. ( I don't want to write them manually ;) .It is waste of time. )

nipercop

To save yor array you can use for example JsonUtillity.

It is simple! Good Luck!

 void Start () {
    /////////////////////// save to json
    JSONObject jsonSave = new JSONObject();
    for(int i=0; i < packsInfo.Length; i++) {
        JSONObject packJson = new JSONObject();
        packJson.AddField("number", packsInfo[i].number);
        packJson.AddField("angle", packsInfo[i].angle);
        packJson.AddField("z_position", packsInfo[i].zPosition);
        packJson.AddField("beat_case_distance", packsInfo[i].beatCaseDistance);
        packJson.AddField("gun_distance", packsInfo[i].gunDistance);
        jsonSave.Add(packJson);
    }

    System.IO.StreamWriter streamWritter = new System.IO.StreamWriter(@"C:\MyGameFiles\packs.json");
    streamWritter.WriteLine(jsonSave.Print(true));
    streamWritter.Close();
    streamWritter = null;
    /////////////////////// read json
    System.IO.StreamReader reader = new System.IO.StreamReader(@"C:\MyGameFiles\packs.json");
    string jsonString = reader.ReadToEnd();
    reader.Close();
    reader = null;

    JSONObject jsonRead = new JSONObject(jsonString);
    packsInfo = new Pack[jsonRead.Count];
    for(int i =0; i < jsonRead.Count; i ++) {
        Pack pack = new Pack();
        pack.number = (int)jsonRead[i]["number"].i;
        pack.angle = (int)jsonRead[i]["angle"].i;
        pack.zPosition = jsonRead[i]["z_position"].f;
        pack.beatCaseDistance =jsonRead[i]["beat_case_distance"].f;
        pack.gunDistance = jsonRead[i]["gun_distance"].f;
        packsInfo[i] = pack;
    }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I fully reallocate a 2D array in C?

分類Dev

How can I clean my react-native project to back it up?

分類Dev

How can I SUM values in a joined table without screwing up totals of values in the first table?

分類Dev

How can I combine consecutive keys of an array depending on their values?

分類Dev

How can I convert this object to an array and access values by key in React?

分類Dev

How can I sum values of an array with the same key sorted by category?

分類Dev

How can I set class properties when the values are specified as an array?

分類Dev

How can I determine if all the values in an array are different, in Matlab

分類Dev

Array results are not displayed when I am accessing the array,How can I access specific field values in this array

分類Dev

How can I convert an array name to an array pointer in C?

分類Dev

How can I access values from a C union in Rust?

分類Dev

How can i parse oracle tnsname values in c#?

分類Dev

How can I sum an array with D3?

分類Dev

How can I iterate through an object array and add matching values while pushing new values

分類Dev

How can I access a resource when it's declared as a Class in C# back end?

分類Dev

How can I filter rows if one array contains all values from another array using BigQuery?

分類Dev

C - How can I print random word inside game board (2d array)?

分類Dev

C#: How can I add an Object into Array?

分類Dev

How can I 'clean up' a virtualenv?

分類Dev

C# UDP messages from PLC can not be received in Unity3D, but is visible in Wireshark

分類Dev

How do I create a constructor for a custom array class that can take in any amount of arguments up until the size of the array?

分類Dev

How can I destructure this array?

分類Dev

C#: How can I rewrite my code for managing Game Data Serialization using an Array instead of a List in Unity?

分類Dev

How can i update an array of values to my modal through foreach in yii2

分類Dev

How can I print values inside an array made from a string? (Explode)

分類Dev

How can I transfer array values to another doing some calculations on the process?

分類Dev

How can I create array elements dynamically and assign values (from variable with the same names) to the elements

分類Dev

How can I check if the array of objects has duplicate property values and get the last value that is repeated?

分類Dev

How can I write a query to insert array values from a python dictionary in BigQuery?

Related 関連記事

  1. 1

    How can I fully reallocate a 2D array in C?

  2. 2

    How can I clean my react-native project to back it up?

  3. 3

    How can I SUM values in a joined table without screwing up totals of values in the first table?

  4. 4

    How can I combine consecutive keys of an array depending on their values?

  5. 5

    How can I convert this object to an array and access values by key in React?

  6. 6

    How can I sum values of an array with the same key sorted by category?

  7. 7

    How can I set class properties when the values are specified as an array?

  8. 8

    How can I determine if all the values in an array are different, in Matlab

  9. 9

    Array results are not displayed when I am accessing the array,How can I access specific field values in this array

  10. 10

    How can I convert an array name to an array pointer in C?

  11. 11

    How can I access values from a C union in Rust?

  12. 12

    How can i parse oracle tnsname values in c#?

  13. 13

    How can I sum an array with D3?

  14. 14

    How can I iterate through an object array and add matching values while pushing new values

  15. 15

    How can I access a resource when it's declared as a Class in C# back end?

  16. 16

    How can I filter rows if one array contains all values from another array using BigQuery?

  17. 17

    C - How can I print random word inside game board (2d array)?

  18. 18

    C#: How can I add an Object into Array?

  19. 19

    How can I 'clean up' a virtualenv?

  20. 20

    C# UDP messages from PLC can not be received in Unity3D, but is visible in Wireshark

  21. 21

    How do I create a constructor for a custom array class that can take in any amount of arguments up until the size of the array?

  22. 22

    How can I destructure this array?

  23. 23

    C#: How can I rewrite my code for managing Game Data Serialization using an Array instead of a List in Unity?

  24. 24

    How can i update an array of values to my modal through foreach in yii2

  25. 25

    How can I print values inside an array made from a string? (Explode)

  26. 26

    How can I transfer array values to another doing some calculations on the process?

  27. 27

    How can I create array elements dynamically and assign values (from variable with the same names) to the elements

  28. 28

    How can I check if the array of objects has duplicate property values and get the last value that is repeated?

  29. 29

    How can I write a query to insert array values from a python dictionary in BigQuery?

ホットタグ

アーカイブ