Skip to content

Instantly share code, notes, and snippets.

@StillManic
Created May 27, 2016 20:59
Show Gist options
  • Save StillManic/bc2a7ca1dd0c4839c400faad1f68eba8 to your computer and use it in GitHub Desktop.
Save StillManic/bc2a7ca1dd0c4839c400faad1f68eba8 to your computer and use it in GitHub Desktop.
SerializationException: Type UnityEngine.MonoBehaviour in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.
System.Runtime.Serialization.FormatterServices.GetSerializableMembers (System.Type type, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization/FormatterServices.cs:101)
System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataTypeInternal (System.Type type, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/CodeGenerator.cs:78)
System.Runtime.Serialization.Formatters.Binary.CodeGenerator.GenerateMetadataType (System.Type type, StreamingContext context) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/CodeGenerator.cs:64)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.CreateMemberTypeMetadata (System.Type type) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:442)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.GetObjectData (System.Object obj, System.Runtime.Serialization.Formatters.Binary.TypeMetadata& metadata, System.Object& data) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:430)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter writer, Int64 id, System.Object obj) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:306)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectInstance (System.IO.BinaryWriter writer, System.Object obj, Boolean isValueObject) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:293)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQueuedObjects (System.IO.BinaryWriter writer) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:271)
System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer, System.Object obj, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:256)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph, System.Runtime.Remoting.Messaging.Header[] headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:232)
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:211)
SaveLoad.Save () (at Assets/Scripts/SaveLoad.cs:41)
InputManager.Update () (at Assets/Scripts/InputManager.cs:156)
using UnityEngine;
[System.Serializable]
public class Metadata : MonoBehaviour {
public int primary = 0;
public int secondary = 0;
public int ring = 0;
public int x, y, z;
public Vector3 position = Vector3.zero;
public void SetIndex(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public void GetIndex(out int x, out int y, out int z) {
x = this.x;
y = this.y;
z = this.z;
}
public void SetPosition(float x, float y, float z) {
position.x = x;
position.y = y;
position.z = z;
}
public void SetPosition(Vector3 position) {
this.position = position;
}
public Vector3 GetPosition() {
return position;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveLoad : MonoBehaviour {
[System.Serializable]
public struct PuzzleState {
public int gridSize, selectedRing;
public bool scrambled, started;
public Metadata[,,] metadatas;
public Stack<Scrambler.Move> undoStack, redoStack;
public PuzzleState(int gridSize, int selectedRing, bool scrambled, bool started, Metadata[,,] metadatas, Stack<Scrambler.Move> undoStack, Stack<Scrambler.Move> redoStack) {
this.gridSize = gridSize;
this.selectedRing = selectedRing;
this.scrambled = scrambled;
this.started = started;
this.metadatas = metadatas;
this.undoStack = undoStack;
this.redoStack = redoStack;
}
}
public static List<PuzzleState> savedPuzzles = new List<PuzzleState>();
public static Dictionary<int, PuzzleState> savedStates = new Dictionary<int, PuzzleState>();
[System.NonSerialized] private StateManager stateManager;
[System.NonSerialized] private Scrambler scrambler;
void Start() {
stateManager = GetComponent<StateManager>();
scrambler = GetComponent<Scrambler>();
}
public void Save() {
PuzzleState state = new PuzzleState(stateManager.gridSize, stateManager.selectedRing, stateManager.scrambled, stateManager.started, stateManager.GetMetadatas(), scrambler.GetUndoStack(), scrambler.GetRedoStack());
savedStates.Add(stateManager.gridSize, state);
BinaryFormatter formatter = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/savedStates.hemicube");
formatter.Serialize(file, savedStates);
file.Close();
Debug.LogFormat("Saved puzzle state to '{0}'", Application.persistentDataPath + "/savedStates.hemicube");
}
public void Load() {
}
}
using UnityEngine;
using UnityEngine.Serialization;
using System.Collections;
using System.Collections.Generic;
public class Scrambler : MonoBehaviour {
[System.Serializable]
public struct Move {
public Enums.Combination combination;
public int ring;
public Move(Enums.Combination combination, int ring = -1) {
this.combination = combination;
this.ring = ring;
}
}
private StateManager stateManager;
private InputManager inputManager;
private List<Move> moves = new List<Move>(8);
private Dictionary<Enums.Combination, int> counts = new Dictionary<Enums.Combination, int>(8);
private Stack<Move> undoStack = new Stack<Move>(100);
private Stack<Move> redoStack = new Stack<Move>(100);
void Start() {
stateManager = GetComponent<StateManager>();
inputManager = GetComponent<InputManager>();
}
public IEnumerator ScramblePuzzle1(int numMoves, int gridSize) {
yield return stateManager.ShowSelectedRing(false);
counts.Clear();
List<Enums.Combination> combinations = Enums.GetCombinations();
foreach (Enums.Combination c in combinations) counts.Add(c, 0);
int numChoices = combinations.Count;
int maxShifts = gridSize / 2;
int totalShifts = 0;
int totalRotations = 0;
for (int i = 0; i < numMoves; i++) {
choose: Enums.Combination choice = combinations[Random.Range(0, i == 0 ? numChoices - 2 : numChoices)];
ring: int ring = Random.Range(0, gridSize - 1);
if (i == 0) goto save;
bool eqLast = AreEqual(choice, moves[i - 1].combination);
bool opLast = AreEqual(choice, Enums.GetOpposite(moves[i - 1].combination));
switch (choice.operation) {
case Enums.Operation.SHIFT:
if (opLast) goto choose;
if (eqLast) {
if (counts[choice] < maxShifts) goto save;
goto choose;
}
break;
case Enums.Operation.ROTATE:
if (ring == moves[i - 1].ring) goto ring;
break;
}
if (!eqLast && !opLast) counts[moves[i - 1].combination] = 0;
save: counts[choice] += 1;
moves.Add(new Move(choice, choice.operation == Enums.Operation.SHIFT ? -1 : ring));
if (choice.operation == Enums.Operation.SHIFT) totalShifts++;
if (choice.operation == Enums.Operation.ROTATE) totalRotations++;
}
Debug.LogFormat("Total Shifts: {0}, Total Rotations: {1}", totalShifts, totalRotations);
yield return PerformMoves();
yield return stateManager.ShowSelectedRing(true);
}
//public IEnumerator ScramblePuzzle(int numMoves, int gridSize) {
// //Debug.LogFormat("numMoves: {0}, gridSize: {1}", numMoves, gridSize);
// counts.Clear();
// List<Enums.Combination> combinations = Enums.GetCombinations();
// //Debug.LogFormat("combinations: {0}", combinations.Count);
// foreach (Enums.Combination c in combinations) counts.Add(c, 0);
// int choices = combinations.Count;
// int maxShifts = gridSize / 2;
// for (int i = 0; i < numMoves; i++) {
// //Debug.LogFormat("i: {0}", i);
// choose: Enums.Combination choice = combinations[Random.Range(0, i == 0 ? choices - 2 : choices)];
// ring: int ring = Random.Range(0, gridSize - 1);
// //Debug.LogFormat("choice: {0}, ring: {1}", choice, ring);
// if (i == 0) goto save;
// bool opLast = AreEqual(choice, Enums.GetOpposite(moves[i - 1].combination));
// bool eqLast = AreEqual(choice, moves[i - 1].combination);
// //Debug.LogFormat("eqLast: {0}, opLast: {1}", eqLast, opLast);
// if (opLast) {
// if (choice.operation == Enums.Operation.ROTATE) {
// if (ring == moves[i - 1].ring) goto ring;
// goto save;
// } else goto choose;
// }
// if (eqLast) {
// if (choice.operation == Enums.Operation.ROTATE) {
// if (ring == moves[i - 1].ring) goto ring;
// goto save;
// } else goto save;
// }
// save: counts[choice] += 1;
// //Debug.LogFormat("Saved {0}. Count: {1}", choice, counts[choice]);
// moves.Add(new Move(choice, ring));
// }
// yield return PerformMoves();
//}
IEnumerator PerformMoves() {
//gameObject.GetComponent<StateManager>().scaleSelectedRing = false;
foreach (Move m in moves) {
switch (m.combination.operation) {
case Enums.Operation.SHIFT: yield return inputManager.TriggerShift(m.combination.axis, m.combination.direction, false); break;
case Enums.Operation.ROTATE: inputManager.selectedRing = m.ring; yield return inputManager.TriggerRotation(m.combination.direction, toggleShow: false); break;
}
}
//gameObject.GetComponent<StateManager>().scaleSelectedRing = true;
yield return null;
}
bool AreEqual(Enums.Combination left, Enums.Combination right) {
return (left.operation == right.operation) && (left.axis == right.axis) && (left.direction == right.direction);
}
public void AddMoveToUndoStack(Move move) {
undoStack.Push(move);
}
public void ResetUndoRedoStacks() {
undoStack.Clear();
redoStack.Clear();
}
public bool Undo(out Move m) {
if (undoStack.Count == 0) {
m = new Move();
return false;
}
m = undoStack.Pop();
redoStack.Push(m);
return true;
}
public bool Redo(out Move m) {
if (redoStack.Count == 0) {
m = new Move();
return false;
}
m = redoStack.Pop();
undoStack.Push(m);
return true;
}
public Stack<Move> GetUndoStack() {
return undoStack;
}
public Stack<Move> GetRedoStack() {
return redoStack;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment