Skip to content

Instantly share code, notes, and snippets.

@lasagnaphil
Created August 20, 2016 18:11
Show Gist options
  • Save lasagnaphil/78d6b1f1a726c983702f913e2d3e2566 to your computer and use it in GitHub Desktop.
Save lasagnaphil/78d6b1f1a726c983702f913e2d3e2566 to your computer and use it in GitHub Desktop.
Testing out Unity's serialization system
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