webplayer的截图实现
2016-03-09 10:20
375 查看
首先是建一个 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<bool> result)
{
thread.StartCoroutine(UploadPNG(fileName, result));
}
private static IEnumerator UploadPNG(string fileName, Action<bool> 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:
<?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");
}
?>
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<bool> result)
{
thread.StartCoroutine(UploadPNG(fileName, result));
}
private static IEnumerator UploadPNG(string fileName, Action<bool> 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:
<?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");
}
?>
相关文章推荐
- java实现mysql数据库的导出
- javascript的定时器
- 近20个绚丽实用的jQuery/CSS3侧边栏菜单
- Oracle的缓冲区命令
- statusCode(状态码) 200、300、400、500序列
- jdk环境变量配置
- linux -a 到 -z 的意义
- Combination Sum II Combinations
- [国嵌攻略][122][按键定制器去抖]
- Firemonkey 指定 StringGrid 只能上下滾动,不要左右滚动
- 【项目管理和构建】——Maven下载、安装和配置(二)
- 2016.03.09
- 如何使用android真机请求PC上的网络服务
- 移动开发之关于“127.0.0.1”的错误使用
- x264代码剖析(五):encode()函数之x264_encoder_open()函数
- 分页算法
- 【iOS】CoreImage原生二维码生成(一)
- x264代码剖析(五):encode()函数之x264_encoder_open()函数
- mysql通过日志恢复数据库
- 学习前言