Unity3d遊戲項目客戶端配置數據編輯器實現步驟

客戶端編輯器主要的做用是把配置文件(格式是Excel)序列化生成二進制文件,在遊戲中進行加載使用。


經過對別的項目的編輯器的學習,瞭解到一種實現方式:
    1.使用Flex生成桌面程序,利用as3xls操做Excel得到數據
    2.生成前端的vo.cs和對應的xml,後端文件
    3.生成C#的桌面程序
    4.加載vo.cs,利用反射生成對應的對象
    5.加載xml,把xml的值經過反射設置進對象中
    6.經過序列化生成文件

經過這種實現方式,每次生成配置的時候,就須要操做兩個工具,我的以爲不如直接使用C#桌面程序讀取Excel。
    1.生成C#桌面程序
    2.使用Oledb讀取讀取Excel(C#讀取Excel有幾種方式,百度便可)
    3.生成vo.cs文件
    4.反射生成vo對象,設值對象
    5.序列化
在實現以上步驟的過程當中,碰到過了很多問題。


1.使用Oledb讀取Excel,要同時考慮到.xls和.xlsx文件: 前端

//2003(Microsoft.Jet.Oledb.4.0)
string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);
//2010(Microsoft.ACE.OLEDB.12.0)
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'", excelFilePath);


2.建立字符串保存vo.cs文件,而後使用反射根據字符串動態建立對象。


CSharpCodeProvider provider = new CSharpCodeProvidor(); //建立編譯器
CompilerParameters paras = new CompilerParameters(); //設置編譯參數
paras.ReferenceAssemblies.Add(「System.dll」);
paras.GenerateExecutable = false;  //編譯成exe仍是dll
paras.GenerateInMemory = true; //是否寫入內存,不寫入內存就寫入磁盤
CompilerResults result = provider.CompileAssembleyFromSource(paras, sourceCode(對象的字符串代碼)); //編譯代碼
Assembly as = result.CompiledAssemble; //得到編譯後的程序集
Object obj = as.CreateInstance(「 com.game.vo.voName」);  


3.經過反射把excel中的值設置進obj


4.把設值好的對象保存進ArrayList當中,對其進行序列化,並保存成 vo.bytes,這裏之因此保存成.bytes,是爲了在unity中加載(儲存文件的擴展名以" .bytes"爲結尾的二進制數據。Unity會把它們當作TextAsset來使用。): web


public byte[] SerializeBinary(object  request)    
        {  
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();  
            System.IO.MemoryStream  memStream  =  new  System.IO.MemoryStream();  
            serializer.Serialize(memStream, request);  
            return  memStream.GetBuffer();  
        } 
        
5.把生成好的vo.bytes以及vo.cs放到Unity3d項目中


6.使用Unity3d把vo.bytes文件打包成assetBundle文件 (這個步驟是對文件進行壓縮,減小文件尺寸)


7.運行Unity3d項目,加載打包好的vo.assetBundle文件,加載成bytes: 後端


string url = "http://www.mywebsite.com/mygame/assetbundles/vo.assetBundle"; IEnumerator Start () { // Start a download of the given URL WWW www = WWW.LoadFromCacheOrDownload (url, 1); // Wait for download to complete yield return www; // Load and retrieve the AssetBundle AssetBundle bundle = www.assetBundle; // Load the TextAsset object TextAsset txt = bundle.Load("myBinaryAsText", typeof(TextAsset)) as TextAsset; // Retrieve the binary data as an array of bytes byte[] bytes = txt.bytes; } 編輯器


8.反序列化二進制獲得配置數據,這裏要注意一點就是前面生成的vo.cs文件必須放在com.game.vo目錄下,不然反序列化會報找不對vo對象。 ide


public object DeserializeBinary(byte[] buf) 
        { 
            System.IO.MemoryStream memStream = new MemoryStream(buf); 
            memStream.Position = 0; 
            System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer = 
                new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); 
            object newobj = deserializer.Deserialize(memStream); 
            memStream.Close(); 
            return newobj; 
        } 


9.完成。
相關文章
相關標籤/搜索