您的位置:首页 > 其它

AR 广告大屏虚拟礼物模块功能一

2017-07-28 11:30 309 查看
一、功能统计



二、关闭视频播放模块



三、打开虚拟礼物模块



四、创建两个脚本,UI_Manager(用于管理界面之间的分配)和G_Sc(礼物模块)

UI_Manager脚本的内容

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UI_Manager : MonoBehaviour {

void Start () {

}

void Update () {

}

//奇幻旅程按钮
public void Bt_M_V_Q()
{
//开始播放视频,调用UI_Manager所依附的物体上的V_Sc脚本中的StartVideo函数
gameObject.GetComponent<V_Sc>().StartVideo();
}

//拍照按钮
public void Bt_M_A_P()
{
//停止播放视频,调用UI_Manager所依附的物体上的V_Sc脚本中的StopVideo函数
gameObject.GetComponent<V_Sc>().StopVideo();
}

//获取贺卡的确认按钮
public void Bt_GIp_St()
{
//将输入的命令写到贺卡上
Debug.Log("Bt_GIp_St 1...................");
gameObject.GetComponent<G_Sc>().InputName();
Debug.Log("Bt_GIp_St 2...................");
//截取贺卡并保存
gameObject.GetComponent<G_Sc>().ScreenShot();
Debug.Log("Bt_GIp_St 3...................");
}
}
G_Sc的内容:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;

public class G_Sc : MonoBehaviour {

//申请变量储存If_G_Ip这个组件
public InputField If_G_Ip;

//申请变量储存显示名字的Text组件
public Text Tx_G_Nm;

void Start () {

}

void Update () {

}

//将输入框的文字显示在贺卡处的函数
public void InputName()
{
//让贺卡中的Text组件中的文字变为输入框中输入的文字
Tx_G_Nm.text = If_G_Ip.text;
}

//截图功能
public void ScreenShot()
{
//Textture2D为内存中一种图片纹理格式
//第一第二个参数指定了纹理的宽高,第三个参数指定了纹理的格式
Texture2D texture2D = new Texture2D(600,800,TextureFormat.RGB24,false);
//读取屏幕像素,确认截图区域
texture2D.ReadPixels(new Rect(0,0,600,800),0,0,true);
//保存对TextTure2D的更改
texture2D.Apply();
//将Texture2D转换为PNG格式储存在byte数组中
byte[] bytes = texture2D.EncodeToPNG();
//将转换的PNG图片保存在资源管理器中,Application.dataPath为资源管理器路径
File.WriteAllBytes(Application.dataPath+"/ScreenShot.png",bytes);
}

}

五、创建一个空物体,重命名为Sc_Manager,将两个脚本挂载到该物体上,并将变量赋给相应的值



六、选中Bt_GIp_St确认按钮,在On Click()中添加相应的内容并选中相应的方法



七、运行后,在文本框输入内容并点击“确认”按钮



图片被保存成功



注:1.参考资料:http://edu.manew.com/course/216/learn#lesson/3294

2.遇到的问题:截屏中没有任何内容



截图功能的代码:

//截图功能
public void ScreenShot()
{
//Textture2D为内存中一种图片纹理格式
//第一第二个参数指定了纹理的宽高,第三个参数指定了纹理的格式
Texture2D texture2D = new Texture2D(600,800,TextureFormat.RGB24,false);
//读取屏幕像素,确认截图区域
texture2D.ReadPixels(new Rect(0,0,600,800),0,0,true);
//保存对TextTure2D的更改
texture2D.Apply();
//将Texture2D转换为PNG格式储存在byte数组中
byte[] bytes = texture2D.EncodeToPNG();
//将转换的PNG图片保存在资源管理器中,Application.dataPath为资源管理器路径
File.WriteAllBytes(Application.dataPath+"/ScreenShot.png",bytes);
}
unity中报的错误:

ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.

UnityEngine.Texture2D:ReadPixels(Rect, Int32, Int32, Boolean)

G_Sc:ScreenShot() (at Assets/Scripts/G_Sc.cs:44)

UI_Manager:Bt_GIp_St() (at Assets/Scripts/UI_Manager.cs:41)

UnityEngine.EventSystems.EventSystem:Update()

[d3d11] attempting to ReadPixels outside of RenderTexture bounds! Reading (0, 0, 600, 800) from (724, 526)



unity中Game视图的大小



原因:Texture2D和Rect中长宽不能超过724、526

解决办法:方法一、将两个参数都控制在724、526之内



方法二:Game视图所显示的就是屏幕的大小,将Game视图的窗口拖大,在运行时会出现问题,打包后可能不会出现该问题



点击保存后在Assets文件夹中查看图片的内容:

图片截图成功,但是坐标位置不对,所以显示的内容不对

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: