Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Last active December 11, 2015 04:55
Show Gist options
  • Save smallrice45/9682ac5cc2175155581d to your computer and use it in GitHub Desktop.
Save smallrice45/9682ac5cc2175155581d to your computer and use it in GitHub Desktop.
UnityC#_DataSaveSystem
using UnityEngine;
using System.Collections;
public class DataSaveController : MonoBehaviour {
public int m_InstanceID;
// Use this for initialization
void Reset () {
m_InstanceID = gameObject.GetInstanceID();
}
void OnValidate() {
m_InstanceID = gameObject.GetInstanceID();
}
/// <summary>
/// 取得目前的物件資料
/// </summary>
/// <returns>The current object data.</returns>
public GameData.ObjectSaveData GetCurrentObjectData ()
{
GameData.ObjectSaveData currentObjectData = new GameData.ObjectSaveData();
currentObjectData = GetTransformInfo(currentObjectData);
currentObjectData = GetColliderInfo(currentObjectData);
return currentObjectData;
}
GameData.ObjectSaveData GetTransformInfo(GameData.ObjectSaveData currentObjectData)
{
if (gameObject.GetComponent<Transform>() != null)
{
currentObjectData.objectInstanceID = this.gameObject.GetInstanceID();
currentObjectData.objectName = this.gameObject.name;
currentObjectData.transformPosition = this.transform.localPosition;
currentObjectData.transformRotation = this.transform.localEulerAngles;
currentObjectData.transformScale = this.transform.localScale;
}
return currentObjectData;
}
GameData.ObjectSaveData GetColliderInfo(GameData.ObjectSaveData currentObjectData)
{
if (gameObject.GetComponent<Collider>() != null)
{
Collider currentCollider = gameObject.GetComponent<Collider>();
currentObjectData.colliderIsTrigger = currentCollider.isTrigger;
if (gameObject.GetComponent<BoxCollider>() != null)
{
BoxCollider currentBoxCollider = gameObject.GetComponent<BoxCollider>();
currentObjectData.colliderCenter = currentBoxCollider.center;
currentObjectData.colliderSize = currentBoxCollider.size;
}
if (gameObject.GetComponent<SphereCollider>() != null)
{
SphereCollider currentSphereCollider = gameObject.GetComponent<SphereCollider>();
currentObjectData.colliderCenter = currentSphereCollider.center;
currentObjectData.colliderRadius = currentSphereCollider.radius;
}
}
return currentObjectData;
}
/// <summary>
/// 設定目前的物件資料
/// </summary>
/// <param name="theObjectData">The object data.</param>
public void SetCurrentObjectData (GameData.ObjectSaveData theObjectData) {
SetObjectTransform(theObjectData);
SetPhysical(theObjectData);
}
/// <summary>
/// 將讀取資料設定回物件上
/// </summary>
/// <param name="theObjectData">The object data.</param>
private void SetObjectTransform(GameData.ObjectSaveData theObjectData){
this.transform.localPosition = theObjectData.transformPosition;
this.transform.localEulerAngles = theObjectData.transformRotation;
this.transform.localScale = theObjectData.transformScale;
}
private void SetObjectAnimator(){
if (gameObject.GetComponent<Animator>() != null){
}
}
private void SetNPCDialogueData(){
}
private void SetPhysical(GameData.ObjectSaveData theObjectData){
// 設置 RigidBody
// 設置 Collider
if (gameObject.GetComponent<Collider>() != null)
{
Collider currentCollider = gameObject.GetComponent<Collider>();
currentCollider.isTrigger = theObjectData.colliderIsTrigger;
if (gameObject.GetComponent<BoxCollider>() != null)
{
BoxCollider currentBoxCollider = gameObject.GetComponent<BoxCollider>();
currentBoxCollider.center = theObjectData.colliderCenter;
currentBoxCollider.size = theObjectData.colliderSize;
}
if (gameObject.GetComponent<SphereCollider>() != null)
{
SphereCollider currentSphereCollider = gameObject.GetComponent<SphereCollider>();
currentSphereCollider.center = theObjectData.colliderCenter;
currentSphereCollider.radius = theObjectData.colliderRadius;
}
}
}
}
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using LitJson;
public class DataSaveManager : MonoBehaviour {
public string loadDataString;
// Use this for initialization
void Start () {
//SaveCurrentSceneObjectData();
LoadCurrentSceneObjectData();
}
/// <summary>
/// 儲存所有遊戲物件資料,並加密儲存
/// </summary>
void SaveCurrentSceneObjectData () {
// 取得所有擁有DataSaveController需要存取的物件
DataSaveController[] allObjectController = FindObjectsOfType(typeof(DataSaveController)) as DataSaveController[];
JsonData allObjectJsonData = new JsonData();
// 遍尋所有 DataSaveController 物件,從中取得 DataSaveController 取得物件的資料
foreach (DataSaveController theObjectData in allObjectController){
string getObjectData = JsonUtility.ToJson( theObjectData.GetCurrentObjectData(), false);
JsonData jsonDataCache = new JsonData();
jsonDataCache = JsonMapper.ToObject(getObjectData);
allObjectJsonData.Add( jsonDataCache);
}
// 在 saveJsonString 存入 以JsonMapper使用allObjectJsonData轉換為Json格式的字串
string saveJsonString = JsonMapper.ToJson(allObjectJsonData);
// 加密儲存資料
string saveJsonStringEncrypt = GameFun.RijndaelEncrypt( saveJsonString, "AAAABBBBCCCCDDDDEEEEFFFFGGGGHH");
Debug.Log(saveJsonStringEncrypt);
loadDataString = saveJsonStringEncrypt;
}
/// <summary>
/// 讀取並解密所有遊戲物件資料,並設定物件資料
/// </summary>
void LoadCurrentSceneObjectData () {
// 取得所有擁有DataSaveController需要存取的物件並建立字典
DataSaveController[] allObjectController = FindObjectsOfType(typeof(DataSaveController)) as DataSaveController[];
Dictionary< int, DataSaveController> allObjectDirectory = new Dictionary< int, DataSaveController>();
// 遍尋所有 DataSaveController 物件,從中取得 DataSaveController 取得物件的資料
foreach (DataSaveController theObjectData in allObjectController){
allObjectDirectory.Add( theObjectData.m_InstanceID, theObjectData);
}
string loadJsonString = loadDataString;
// 解密讀取Json資料
string loadJsonStringDecrypt = GameFun.RijndaelDecrypt( loadJsonString, "AAAABBBBCCCCDDDDEEEEFFFFGGGGHH");
JsonData allObjectJsonData = JsonMapper.ToObject( loadJsonStringDecrypt);
int objectCount = allObjectJsonData.Count;
// 遍尋儲存資料,並找到對應Key的物件做讀取資料設定
for( int i = 0; i < objectCount; i++){
int objectKey = int.Parse( allObjectJsonData[i]["objectInstanceID"].ToString());
GameData.ObjectSaveData theLoadSata = JsonUtility.FromJson<GameData.ObjectSaveData>( allObjectJsonData[i].ToJson());
allObjectDirectory[objectKey].SetCurrentObjectData( theLoadSata);
}
}
}
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class GameData : MonoBehaviour {
[Serializable]
public class ObjectSaveData
{
public int objectInstanceID;
public string objectName;
// Transform
public Vector3 transformPosition;
public Vector3 transformRotation;
public Vector3 transformScale;
// Rigidbody
public bool useGravity;
public bool isKinematic;
public Vector3 freezePosition;
public Vector3 freezeRotation;
// Collider
public bool colliderIsTrigger;
public Vector3 colliderCenter;
public Vector3 colliderSize;
public float colliderRadius;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment