2012-03-16 5 views
1

Xbox 응용 프로그램을 만들고 있는데 콘텐츠 파이프 라인에이 문제가 있습니다. .xnb 파일을로드하는 것은 문제가되지 않지만 콘텐츠 파이프 라인을 통해 작성하는 데 도움이되는 자습서를 찾지 못하는 것 같습니다. 사용자가 만든 "저장"버튼을 누를 때마다 XML을 작성하려고합니다. 웹에서 "saving game sate"등을 검색했지만 지금까지는 내 사례에 대한 해결책을 찾지 못했습니다.Xbox 게임 프로젝트의 콘텐츠 파이프 라인을 통해 작성하기

요약하면 : Save() 메서드를 호출하면 콘텐츠 파이프 라인을 통해 데이터를 XML 형식으로 작성하는 방법이 있습니까? XNA 게임 중

Cheerz에

답변

0

저장 및 로딩은 비동기 메서드 호출의 시리즈를 포함한다. 필요한 개체는 Microsoft.Xna.Framework.Storage 네임 스페이스에서 찾을 수 있습니다.

특히 StorageDevice와 StorageContainer가 필요합니다.

private static StorageDevice mStorageDevice; 
private static StorageContainer mStorageContainer; 

에 저장하기 :

public static void SaveGame() 
{ 
    // Call this static method to begin the process; SaveGameDevice is another method in your class 
    StorageDevice.BeginShowSelector(SaveGameDevice, null); 
} 

// this will be called by XNA sometime after your call to BeginShowSelector 
// SaveGameContainer is another method in your class 
private static void SaveGameDevice(IAsyncResult pAsyncResult) 
{  
    mStorageDevice = StorageDevice.EndShowSelector(pAsyncResult); 
    mStorageDevice.BeginOpenContainer("Save1", SaveGameContainer, null); 
} 

// this method does the actual saving 
private static void SaveGameContainer(IAsyncResult pAsyncResult) 
{ 
    mStorageContainer = mStorageDevice.EndOpenContainer(pAsyncResult); 

    if (mStorageContainer.FileExists("save.dat")) 
     mStorageContainer.DeleteFile("save.dat"); 

    // in my case, I have a BinaryWriter wrapper that I use to perform the save 
    BinaryWriter writer = new BinaryWriter(new System.IO.BinaryWriter(mStorageContainer.CreateFile("save.dat"))); 

    // I save the gamestate by passing the BinaryWriter 
    GameProgram.GameState.SaveBinary(writer); 

    // then I close the writer 
    writer.Close();  

    // clean up 
    mStorageContainer.Dispose(); 
    mStorageContainer = null; 
} 

로드와 매우 유사하다 :

public static void LoadGame() 
{ 
    StorageDevice.BeginShowSelector(LoadGameDevice, null); 
} 

private static void LoadGameDevice(IAsyncResult pAsyncResult) 
{ 
    mStorageDevice = StorageDevice.EndShowSelector(pAsyncResult); 
    mStorageDevice.BeginOpenContainer("Save1", LoadGameContainer, null); 
} 

private static void LoadGameContainer(IAsyncResult pAsyncResult) 
{ 
    mStorageContainer = mStorageDevice.EndOpenContainer(pAsyncResult)) 

    // this is my wrapper of BinaryReader which I use to perform the load 
    BinaryReader reader = null; 

    // the file may not exist 
    if (mStorageContainer.FileExists("save.dat")) 
    { 
     reader = new BinaryReader(new System.IO.BinaryReader(mStorageContainer.OpenFile("save.dat", FileMode.Open))); 

     // pass the BinaryReader to read the data 
     GameProgram.LoadGameState(reader); 
     reader.Close(); 
    } 

    // clean up 
    mStorageContainer.Dispose(); 
    mStorageContainer = null; 
} 
관련 문제