Nanashi-softプログラマ専用Unityでゲーム開発


◇Unityでゲーム開発 -DotNetZipライブラリを使ってDeflate圧縮・解凍-

なぜ DotNetZipライブラリを使用するのかと言うと……

Unity3D上から,GZipStreamを呼び出すと以下のエラーが出ます
EntryPointNotFoundException: CreateZStream
System.IO.Compression.DeflateStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen, Boolean gzip)
(wrapper remoting-invoke-with-check) System.IO.Compression.DeflateStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode,bool,bool)
System.IO.Compression.GZipStream..ctor (System.IO.Stream compressedStream, CompressionMode mode, Boolean leaveOpen)
System.IO.Compression.GZipStream..ctor (System.IO.Stream compressedStream, CompressionMode mode)
(wrapper remoting-invoke-with-check) System.IO.Compression.GZipStream:.ctor (System.IO.Stream,System.IO.Compression.CompressionMode)


○DotNetZip Library
http://dotnetzip.codeplex.com/
右のdownloadをクリックすればダウンロードできる

ライセンスは,Microsoft Public License (Ms PL)
アーカイブ内にあるライセンスファイルの内容を表示すれば良いと思う

○Unity3Dプロジェクトにインストール

zlib-v1.9→Release内の Ionic.Zlib.dllを,Projectの Scriptsの下などにドロップする
スクリプトと一緒のフォルダに置いておけば,ビルド時に自動的に同梱してくれる

○サンプルソース

Examples→C#→ZLIB内のファイル郡です
とは言っても,Unityの C#は通常とはちょっと異なるので……

例えば,文字列の圧縮・解凍は ZlibStreamExample.csがあります
これを Unity用に書き直すと↓こんな感じになります
using UnityEngine;
using System.Collections;

using System;
using Ionic.Zlib;

public class main : MonoBehaviour{

	static System.IO.MemoryStream StringToMemoryStream(string s){
		byte[] a=System.Text.Encoding.UTF8.GetBytes(s);
		return new System.IO.MemoryStream(a);
	}

	static String MemoryStreamToString(System.IO.MemoryStream ms){
		byte[] ByteArray = ms.ToArray();
		return System.Text.Encoding.UTF8.GetString(ByteArray);
	}

	static void CopyStream(System.IO.Stream src, System.IO.Stream dest){
		byte[] buffer=new byte[1024];
		int len=src.Read(buffer, 0, buffer.Length);
		while(len > 0){
			dest.Write(buffer, 0, len);
			len=src.Read(buffer, 0, buffer.Length);
		}
		dest.Flush();
	}

	void Start(){
		try{
			System.IO.MemoryStream msSinkCompressed;
			System.IO.MemoryStream msSinkDecompressed;
			ZlibStream zOut;
			String originalText="日本語でOK♪";
			Debug.Log("original:" + originalText);

			//圧縮
			msSinkCompressed=new System.IO.MemoryStream();
			zOut=new ZlibStream(msSinkCompressed, CompressionMode.Compress, CompressionLevel.BestCompression, true);
			CopyStream(StringToMemoryStream(originalText), zOut);
			zOut.Close();


			//解凍
			msSinkCompressed.Seek(0, System.IO.SeekOrigin.Begin);
			msSinkDecompressed = new System.IO.MemoryStream();
			zOut=new ZlibStream(msSinkDecompressed, CompressionMode.Decompress, true);
			CopyStream(msSinkCompressed, zOut);

			string decompressed=MemoryStreamToString(msSinkDecompressed);
			Debug.Log("decompressed:" + decompressed);

			if(originalText == decompressed){
				Debug.Log("成功!");
			}else{
				Debug.Log("失敗。。。");
			}
		}catch (System.Exception e){
			Debug.Log("Exception: " + e);
		}
	}

}
日本語が使えるように,エンコーディングを UTF-8に変更しています

あと,Ionic.Zlib.dllは zlib-v1.9-CompactFrameworkの方でも動きました。自分で圧縮・解凍する分にはこちらの方がいいかも?

zipや bz2も使えるみたいなので,後は誰かよろしく(ぉ



TOPプログラマ専用Unityでゲーム開発

メイナの実験場~これくらいなら大丈夫でしょうか~パジャマ萌えっ娘☆優愛