Unity3D培训
美国上市Unity3D培训机构

400-111-8989

VR培训

Unity3D webplayer的截图实现

  • 发布:Unity3D培训
  • 来源:Unity3D教程
  • 时间:2015-12-08 14:56

首先是建一个 C# 类:

using System;

using UnityEngine;

using System.Collections;

public class PostPng

{

public static string UploadUrl = http://127.0.0.1/getpng.php;

public static void UploadPNG(MonoBehaviour thread, string fileName, Action result)

{

thread.StartCoroutine(UploadPNG(fileName, result));

}

private static IEnumerator UploadPNG(string fileName, Action result)

{

yield return new WaitForEndOfFrame();

int width = Screen.width;

int height = Screen.height;

Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false);

tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);

tex.Apply();

byte[] bytes = tex.EncodeToPNG();

GameObject.Destroy(tex);

WWWForm form = new WWWForm();

form.AddField("enctype", "multipart/form-data");

form.AddBinaryData("PngUpload", bytes, fileName, "image/png");

WWW post = new WWW(UploadUrl , form);

yield return post;

if (string.IsNullOrEmpty(post.error) && result != null)

{

result(true);

}

else

{

result(false);

}

post.Dispose();

}

}

再建一个 Cube,再建一个 C# 脚本:

OnGUI()

{

if (GUI.Button(new Rect(440, Screen.height - 25, 65, 23), "Snapshot"))

{

LoadHelp.UploadPNG(this, "Snapshot", delegate(bool result)

{

if (result)

{

Application.ExternalEval("alert('ok')");

}

});

}

}

最后要建立一个接受图片上传的程序,用什么取决于你的web服务器,我这里用的是php:

$filename = "";

$field = "PngUpload";

$pngfolder = "snapshot/";

if ($_FILES[$field]["error"] > 0)

{

echo "Error: " . $_FILES[$field]["error"];

}

else

{

$filename = $pngfolder . $_FILES[$field]["name"] . ".png";

move_uploaded_file($_FILES[$field]["tmp_name"], $filename);

echo("ok");

}

?>

预约申请免费试听课

填写下面表单即可预约申请免费试听!怕钱不够?可就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:Unity3D web网页跳转的问题及解决办法
下一篇:Unity3D格斗游戏运动特效
Unity3D中如何使用FileInfo类和File类?区别是什么?

Unity3D中如何使用FileInfo类和File类?区别是什么?

Unity3D Editor类(Inspector)经验总结

Unity3D Editor类(Inspector)经验总结

Unity3D webplayer的截图实现

Unity3D webplayer的截图实现

选择城市和中心
贵州省

广西省

海南省