Dras.biz Just another WordPress site

June 10, 2012

SaveIt How To save and load a GameObjects position

Filed under: SaveIt — mark @ 10:55

..save and load the position of a GameObject
To save or load the position you have several options:

All samples are based upon this:

C#:

1
var smart = new TableSerializer.Smart("MySaveData");
1
var loadContext = new LoadContext(smart);
1
var saveContext = new SaveContext(smart);

JS:

1
var smart : TableSerializer.Smart = TableSerializer.Smart("MySaveData");
1
var loadContext : LoadContext = LoadContext(smart);
1
var saveContext : SaveContext = SaveContext(smart);

1. Save only the position value from the transform component and load it when needed.
Saving:

C#:

1
saveContext.Save(myGameObject.transform.position);
1
saveContext.Save("ThePosition", myGameObject.transform.position); // an alternative

JS:

1
saveContext.Save(myGameObject.transform.position);
1
saveContext.Save("ThePosition", myGameObject.transform.position); // an alternative

Loading:

C#:

1
myGameObject.transform.position = loadContext.Load<Vector3>();
1
myGameObject.transform.position = loadContext.Load<Vector3>("ThePosition"); // an alternative

JS:

1
myGameObject.transform.position = loadContext.Load.<Vector3>();
1
myGameObject.transform.position = loadContext.Load.<Vector3>("ThePosition"); // an alternative

2. Save the whole Transform component (including, rotation, scale, eg.).
Saving:

C#:

1
saveContext.Save(myGameObject.transform);
1
saveContext.Save("TheTransformComponent", myGameObject.transform); // an alternative

JS:

1
saveContext.Save(myGameObject.transform);
1
saveContext.Save("TheTransformComponent", myGameObject.transform); // an alternative

Loading:

C#:

1
loadContext.LoadComponent<Transform>(myGameObject);
1
loadContext.LoadComponent<Transform>("TheTransformComponent", myGameObject); // an alternative
1
loadContext.LoadToInstance(myGameObject.transform); // an alternative
1
loadContext.LoadToInstance("TheTransformComponent", myGameObject.transform); // an alternative

JS:

1
loadContext.LoadComponent.<Transform>(myGameObject);
1
loadContext.LoadComponent.<Transform>("TheTransformComponent", myGameObject); // an alternative
1
loadContext.LoadToInstance(myGameObject.transform); // an alternative
1
loadContext.LoadToInstance("TheTransformComponent", myGameObject.transform); // an alternative

3. Save the whole GameObject.
Saving:

C#:

1
saveContext.Save(myGameObject);
1
saveContext.Save("TheGameObject", myGameObject); // an alternative

JS:

1
saveContext.Save(myGameObject);
1
saveContext.Save("TheGameObject", myGameObject); // an alternative

Loading:

C#:

1
var myGameObject = loadContext.Load<GameObject>();
1
var myGameObject = loadContext.Load<GameObject>("TheGameObject"); // an alternative

JS:

1
var myGameObject : GameObject = loadContext.Load.<GameObject>();
1
var myGameObject : GameObject = loadContext.Load.<GameObject>("TheGameObject"); // an alternative

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress