// 存檔. public void Save() { int iCount = 0; foreach (KeyValuePair <GameObject, int> Itor in SysMain.pthis.Enemy) { if (Itor.Key) { AIEnemy EnemyTemp = Itor.Key.GetComponent <AIEnemy>(); if (EnemyTemp && EnemyTemp.iHP > 0) { SaveEnemy Temp = new SaveEnemy(); Vector3 Pos = EnemyPos(Itor.Key.transform.localPosition); Temp.iMonster = EnemyTemp.iMonster; Temp.iHP = EnemyTemp.iHP; Temp.fPosX = Pos.x; Temp.fPosY = Pos.y; PlayerPrefs.SetString(GameDefine.szSaveEnemy + iCount, Json.ToString(Temp)); ++iCount; } //if } //if } //for PlayerPrefs.SetInt(GameDefine.szSaveEnemyCount, iCount); }
public void SetState(ISerialDataStore state) { SaveEnemy past = (SaveEnemy)state; isMovingRight = past.isMovingRight; isGrounded = past.isGrounded; isAlive = past.isAlive; timeLeftInPlay = past.timeLeftInPlay; transform.position = new Vector3(past.positionX, past.positionY, 0); // Don't use rigid body to update or it will be jitters rb.velocity = Vector2.zero; rb.isKinematic = past.isKinematic; rb.angularVelocity = 0f; // Can't use rb.rotation due to it causing jittery movement // Have to convert normal 2d rotation to 3d to use transform.rotation // The 2D rotation value is actually along the Z-Axis Vector3 currentRot = transform.rotation.eulerAngles; Vector3 newRot = new Vector3(currentRot.x, currentRot.y, past.rotation); transform.rotation = Quaternion.Euler(newRot); }