AR电子书项目实训中期总结
已完成内容:
- 使用ZXing.Net库,验证并测试Unity中生成和识别二维码的功能。
- 使用Unity自带的WWW类测试并实现了本地图像和视屏资源的读取,并使用File方法实现了资源的写入。
//通过www读入本地文件。
private IEnumerator opt(string path)
{
string str = "file:///" + path;
string filename = path.Substring(path.LastIndexOf("\\") + 1);
filename = filename.Substring(0, filename.LastIndexOf('.'));
www = new WWW(@str);
yield return www;
if (www != null && string.IsNullOrEmpty(www.error))
{
//获取Texture
//Texture texture = www.texture;
string fullpath = UnityEngine.Application.dataPath + "/Resources/" + filename + ".png";
Debug.Log(fullpath);
testimage.canvasRenderer.SetTexture(txt);
Debug.Log("加载成功");
}
else
{
Debug.Log(www.error);
}
}
//文件输出到目标地址
byte[] data = www.texture.EncodeToPNG();
File.WriteAllBytes(fullpath, data);
- 使用ICSharpCode.SharpZipLib库测实现了Unity中文件的打包压缩功能。
//文件打包为zip
string[] Array = new string[1];
Array[0] = UnityEngine.Application.dataPath + "/Resources";
ZipUtility.Zip(Array, UnityEngine.Application.dataPath + "/ZIPS/test.zip",null, cbk)
- 初步搭建了一个服务器,并在Unity端使用WWW实现了Post请求上传文件的功能,同时使用PHP在服务器端实现了接收并存储文件的功能。
//使用WWW上传数据
byte[] bytes = readingZipWWW.bytes;
WWWForm form = new WWWForm();
form.AddField("Name", name);
form.AddBinaryData("post", bytes);
uploadWWW = new WWW(url, form);
Uploading = true;
StartCoroutine(PostData(uploadWWW));
<?php
//服务器端存储数据
$myFile = $_FILES["post"]["tmp_name"];
$content = '';
$fh = fopen($myFile, 'r') or die("can't open file");
while (!feof($fh)) {
$content .= fgets($fh);//filesize($myFile)) or die('can\'t read');
}
fclose($fh);
//文件存储路径
$file_path="upload/";
if(is_dir($file_path)!=TRUE) mkdir($file_path,0664) ;
$myFile = $file_path.$_REQUEST['Name'].".zip";
$fh = fopen($myFile, 'w') or die("can't open file");
//$stringData = $_FILES["fileUpload"];
$stringData = $content;//"START:\n" . join(',\n',headerCustom()) . ' \END';
fwrite($fh, $stringData);
fclose($fh);
echo $myFile;
//echo $file_path +
?>
- 使用Unity自带的JsonUtility类验证了数据对象和Json字符串之间相互转换的功能,同时使用File方法以文本形式将Json字符串输出到本地。
//实例和Json的相互转化
AssetsArrangementInfo tmpinfo = new AssetsArrangementInfo();
tmpinfo.srcList.Add("Test00");
string jsonresult = JsonUtility.ToJson(tmpinfo);
Debug.Log(jsonresult);
AssetsArrangementInfo dstInfo = JsonUtility.FromJson<AssetsArrangementInfo>(jsonresult);
Debug.Log(dstInfo.srcList[0]);
目前的工作:
目前正在制作桌面端的资源编辑和管理软件,该软件使用了如上所述的知识,拥有导入多个目标图片和视屏资源,将图片和视屏进行组合,并将资源进行打包上传的功能,上传成功后,程序还会根据服务器返回的地址生成资源的二维码,以供手机端扫描并下载资源。目前该软件的多数功能已设计完成,具体会在以后的几篇博客中进行介绍。