Created
August 20, 2016 18:11
-
-
Save lasagnaphil/78d6b1f1a726c983702f913e2d3e2566 to your computer and use it in GitHub Desktop.
Testing out Unity's serialization system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using System.Collections.Generic; | |
public class GameData : MonoBehaviour | |
{ | |
// | |
// Serialization rule for fields | |
// | |
public int playerHealth; | |
[NonSerialized] | |
public float playerReloadTime; | |
[HideInInspector] | |
public float playerJumpTime; | |
private int maxLife; | |
[SerializeField] | |
private int maxAmmoCount; | |
// Can't serialize auto-generated property fields | |
public float EnemyCount { get; set; } | |
// So serialize like this (blame Unity) | |
[SerializeField] | |
private float timeLeft; | |
public float TimeLeft | |
{ | |
get { return timeLeft; } | |
set { timeLeft = value; } | |
} | |
// | |
// Serialization rule for references | |
// | |
// UnityEngine.Object references can be serialized | |
public Sprite playerHitSprite; | |
public Camera camera; | |
// System.Object references need [Serializeable] attribute | |
[System.Serializable] | |
public class Song | |
{ | |
public string name; | |
public int bpm; | |
public AudioClip clip; | |
} | |
// Both arrays and lists are supported... | |
public Song[] songArray; | |
public List<Song> songList; | |
// but Dictionaries are not | |
public Dictionary<string, Song> songPerLevelDict; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment