2016-07-17 2 views
0

그래서 내가 작업하고있는 프로젝트는 복셀 기반 게임이며 큰 3D 배열 청크가있을 것입니다. 문제는 그것을 저장하는 것입니다. 배열의 여러 덩어리를 하나의 파일로 저장하는 방법을 생각할 수 없습니다. 필요할 때만 파일에서 필요한 청크를로드하십시오. 이 작업을 수행하는 방법에 대한 아이디어가 있습니까? 이상적으로는 여러 개의 작은 파일을 만들 수 있지만 불편할 수 있습니다.여러 개의 청크를 하나의 파일에 저장 /로드 하시겠습니까?

답변

0

당신은 덩어리가

뭔가 등등

Chunk1=256,Chunk2=512 

와 같은 파일에있는 프로그램을 알려 파일의 시작에 헤더를 사용할 수 있습니다.

또는 경우 BinaryFormatter

using System; 
using System.IO; 
using System.Runtime.Serialization.Formatters.Binary; 

namespace Helpr 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int[] Chunks = new int[10 * 10]; //Created this dummy chunk array to show that it works 
      for(int x = 0; x < 10; ++x)  
      { 
       for(int y = 0; y < 10; ++y) 
       { 
        Chunks[x + (10 * y)] = x * y;  //Set the data 
       } 
      } 

      SaveChunks(Chunks, "Chunks.world"); //Save the chunks with a file name to refer too later 

      for(int i = 0; i < 100; ++i) 
      { 
       Console.WriteLine(Chunks[i] + " "); //Write the data to the console so we can compare 
      } 

      Console.WriteLine(" "); 

      Chunks = (int[])LoadChunks("Chunks.world"); //Load the file back into the chunk 

      for (int i = 0; i < 100; ++i) 
      { 
       Console.WriteLine(Chunks[i] + " "); //Log to see if it has worked 
      } 

      Console.ReadKey();      //Pause so you can even read it! 
     } 

     public static void SaveChunks(object Chunks, string filePath) 
     { 
      BinaryFormatter bf = new BinaryFormatter();        //Create a BinaryFormatter to change the chunks into a binary string 
      FileStream fs = new FileStream(filePath, FileMode.Create);    //Create a filestream to create a new file to store the string 

      try 
      { 
       bf.Serialize(fs, Chunks);           //Serialize the data into a file 
      } 
      catch (Exception e)              //if an error occurs Post it! 
      { 
       Console.WriteLine("Failed to create Chunk file! " + e); 
      } 
      finally 
      { 
       fs.Close();               //Then close the filestream to prevent memory leaks 
      } 
     } 

     public static object LoadChunks(string filePath) 
     { 
      BinaryFormatter bf = new BinaryFormatter();        //Create a BinaryFormatter to convert the binary string to an object 
      FileStream fs = new FileStream(filePath, FileMode.Open);    //Create a filestream to create a new file to provide the string 

      try 
      { 
       return bf.Deserialize(fs);           //Deserialize the data into an object 
      } 
      catch (Exception e)              //Catch any errors if they occur 
      { 
       Console.WriteLine("Chunk load failed! " + e); 
       return null; 
      } 
      finally 
      { 
       fs.Close();               //Then close the filestream to prevent memory leaks 
      } 
     } 
    } 
} 

핵심 로딩 덩어리를 저장하고 저장하는 부분은 여기이 도움이

public static void SaveChunks(object Chunks, string filePath) 
     { 
      BinaryFormatter bf = new BinaryFormatter();        //Create a BinaryFormatter to change the chunks into a binary string 
      FileStream fs = new FileStream(filePath, FileMode.Create);    //Create a filestream to create a new file to store the string 

      try 
      { 
       bf.Serialize(fs, Chunks);           //Serialize the data into a file 
      } 
      catch (Exception e)              //if an error occurs Post it! 
      { 
       Console.WriteLine("Failed to create Chunk file! " + e); 
      } 
      finally 
      { 
       fs.Close();               //Then close the filestream to prevent memory leaks 
      } 
     } 

     public static object LoadChunks(string filePath) 
     { 
      BinaryFormatter bf = new BinaryFormatter();        //Create a BinaryFormatter to convert the binary string to an object 
      FileStream fs = new FileStream(filePath, FileMode.Open);    //Create a filestream to create a new file to provide the string 

      try 
      { 
       return bf.Deserialize(fs);           //Deserialize the data into an object 
      } 
      catch (Exception e)              //Catch any errors if they occur 
      { 
       Console.WriteLine("Chunk load failed! " + e); 
       return null; 
      } 
      finally 
      { 
       fs.Close();               //Then close the filestream to prevent memory leaks 
      } 
     } 

희망입니다!

+0

그러나 BinaryFormatter는 larrrrrge 파일 크기를 만듭니다. – Sid

관련 문제