AR开发实战EasyAR项目之互动大屏(服务器交互与拍照视频)上

一、框架视图

AR开发实战EasyAR项目之互动大屏(服务器交互与拍照视频)上_第1张图片

二、关键代码

Ads:
A_Sc


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;    //操作文件流的命名空间;
using Antares.QRCode; //引用生成二维码的sdk命名空间;

/// 
/// 在与服务器交互的客户端
/// 
public class A_Sc : MonoBehaviour {

    //储存整个合影功能模块
    public GameObject Im_A_BG_Obj;


    //储存合影倒计时的提示面板
    public GameObject Im_A_Fc;

    //储存显示倒计时的Text组件
    public Text Tx_A_Ct;

    //定义一个标志位 判断倒计时是否开启
    public bool Count_Bl = false;

    //用来储存计时开始后帧数的变化
    public int i = 0;

    //用来存储及时开始后帧数的变化 计时的时长
    //public int Count_Int = 5;
    public int Count_Int = 3;

    //用来显示明星图片的Image组件;
    public Image Im_A_BG;

    //储存明星照片的精灵数组图片;
    public Sprite[] Sprite_A;

    //用来显示记录当前缩显示的图片数序列号;
    public int  Count_Sprite = 0;

    //储存右按钮 为了隐藏和显示
    public GameObject Bt_A_R;

    //储存左按钮 为了隐藏和显示;
    public GameObject Bt_A_L;

    //储存开始按钮 为了隐藏和显示
    public GameObject Bt_A_P;

    //储存显示提取码的组件
    public GameObject Im_A_In;

    //储存显示图片提取码的 Text组件
    public Text Tx_AIn_In;

    //申请Texture2D类型的变量 用于储存屏幕截图;
    public Texture2D Te2D;
    //private Texture2D Te2D;

    //用于储存服务器地址; 
    public string uploadURL = "http://39.108.193.209/UpLoadAR.aspx";

    //用来获取输入验证码的界面地址
    public string showARURL = "http://39.108.193.209/GetARImage.aspx";

    //用来储存显示二维码的变量;
    public Image Im_A_Ew;

    //用来贮存最开始的二维码;原始生成的纹理一般都是用Texture2D 来处理
    private Texture2D Te_Ew;


    //储存隐藏的老虎,方便调用出来
    public GameObject goTiger;

    //定义一个标志位是否让老虎出现
   public  bool isShowTiger=false;



    //-----------------旋转-------------------------------------------

    private  int width ;  //图片原本的宽度
    private  int height;
    private Texture2D newTexture;

    //-----------------旋转变量完毕-------------------------------------------

    void Start () {
        
    }
    

    void Update () {

        //判断当前图片是否第一张,是,生成动物,不是关闭动物生成
        if (Count_Sprite == 0&&isShowTiger)
        {
            //动物实例化生成
            GameObject.Find("P_01").GetComponent().enabled = true;
            goTiger.SetActive(true);
            //不销毁,隐藏起来
            StartCoroutine("isActive");
            isShowTiger = false;
        }
        else if (Count_Sprite != 0)
        {
            //停止动物实例化生成
            GameObject.Find("P_01").GetComponent().enabled = false;
        }
    

    }

    //在FixedUpdate中制作计时器 每30帧变化一次秒数
    void FixedUpdate()
    {
        if (Count_Bl==true)
        {
            //每一帧给记录帧数的变量+1;
            i++;

            //将当前倒计时的秒数转化为字符串 赋值给显示倒计时的Text组件
            Tx_A_Ct.text = Count_Int.ToString();

            //-----------------------------------------计时---------------------------
            //每经过30帧 计时显示一个整数;
            //if (i>30&&i<60)
            //{
            //    Count_Int = 4;
            //}
            //if (i > 60 && i < 90)
            //{
            //    Count_Int = 3;
            //}
            //if (i > 90 && i < 120)
            //{
            //    Count_Int = 2;
            //}
            //if (i > 120 && i < 150)
            //{
            //    Count_Int = 1;
            //}
            //if (i > 150 && i < 180)
            //{
            //    Count_Int = 0;
            //}
            //if (i==200)
            //{
            //    //开启截图功能
            //    Sc_Shot();

            //    //关闭合影时倒计时提示面
            //    Im_A_Fc.SetActive(false);

            //    //关闭计时器
            //    Count_Bl = false;
            //}
            //-----------------------------------------计时完毕---------------------------

            if (i > 30 && i < 60)
            {
                Count_Int = 2;
            }
            if (i > 60 && i < 90)
            {
                Count_Int = 1;
            }
            if (i == 120)
            {
                //开启截图功能
                Sc_Shot();

                //关闭合影时倒计时提示面
                Im_A_Fc.SetActive(false);

                //关闭计时器
                Count_Bl = false;

          
            }






        }
    }

    //开启倒计时函数
    public void Count_Start() {
        //重置倒计时秒数
        //Count_Int = 5;
        Count_Int = 3;
        //重置计时器帧数
        i = 0;
        //打开计时器开关
        Count_Bl = true;
        //显示计时器提示面板
        Im_A_Fc.SetActive(true);


    }


    /// 
    /// 截屏函数 截取写入名字后贺卡的函数  与服务器交互的固定格式
    /// 
    public void Sc_Shot() {

        //将影响合影的三个 左按钮 右按钮 开始按钮 隐藏起来;
        Bt_A_L.SetActive(false);
        Bt_A_R.SetActive(false);
        Bt_A_P.SetActive(false);

        //执行延迟函数 已经把影响截图的三个按钮隐藏 待画面刷新后进行截图
        StartCoroutine(Shot());

   
    }


    //延迟一帧后 画面刷新后再截图
    IEnumerator Shot() {

        //返回值为空 但已经刷新了一帧
        yield return null;

        //执行截图

        //定义Teture2D的属性 宽度1600 高度900 颜色模式RGB24
        Te2D = new Texture2D(1600, 900, TextureFormat.RGB24, true);//android

        //Te2D = new Texture2D(1100, 630, TextureFormat.RGB24, true);//pc1366

        //用于Texture2D纹理读取屏幕像素, 起始坐标为(160,140) 宽度1600,高度 900 固定格式
        //android
        Te2D.ReadPixels(new Rect(160, 140, 1600, 900), 0, 0, true); //矩形坐标 新建Image与第一张起始位置重合然后拉伸到另一个图片的起始位置 其中的高和宽就是另一张图片的起始位置数据  另一张图片的大小就是直接看它的宽和高就行了 

        //Te2D.ReadPixels(new Rect(130, 105, 1100, 630), 0, 0, true); //pc1366

        //-----------------旋转-------------------------------------------
       
        width = Te2D.width; 
        height = Te2D.height;
        newTexture = new Texture2D(height, width);
        Te2D=RotateTe2D();

        //-----------------旋转完毕-------------------------------------------

        //执行对Texture2D的操作和保存;
        Te2D.Apply();


        //把截图得到的Texture2D纹理 编码成为JPG格式 并且转化成二进制数组;
        byte[] bytes = Te2D.EncodeToJPG();


        //创建一个网络表单 用来把数据加载进去的网络表单
        WWWForm wwwForm = new WWWForm();

        //将接口和数组绑定到网络表单之中
        wwwForm.AddBinaryData("post", bytes);

        //将网络表单发送到指定的服务器中
        WWW www = new WWW(uploadURL, wwwForm); //参数1:启动服务器地址下程序(之前写好的放到服务器下固定的文件夹下的脚本) 参数2:网络表单加载后的数据

        //引用协程函数处理www  开启协程是为了等待网络加载完成后执行后面的显示提取码的操作;
        StartCoroutine(postImage(www));
    }


    //处理www进程的协程函数
    //必须把网络加载到进行完成后再执行显示提取码的操作;
    IEnumerator postImage(WWW www) {

        //等待www进程结束后;
        yield return www;

        //将服务器返回的图片提取码 显示在对应的组件之中;
        Tx_AIn_In.text = www.text;

        //显示影响截图的三个按钮;
        Bt_A_L.SetActive(true);
        Bt_A_R.SetActive(true);
        Bt_A_P.SetActive(true);

        //显示提取码组件
        Im_A_In.SetActive(true);

        //显示二维码图片;
        Encode_Ew(showARURL);
    }

    //左按钮
    public void Change_L() {
    
        if (Count_Sprite==0)
        {
            //如果当前下标为0,说明是第一张,再点击左边按钮时候跳到第三张
            Count_Sprite = (Sprite_A.Length-1);
          
        }
        else
        {
            //否则 说明图片都大 则下标减一
            Count_Sprite--;
        }

        //显示当前序号的图片
        Im_A_BG.sprite = Sprite_A[Count_Sprite];

        //执行按钮函数
        isShowTiger = true;

        //重置位置
        goTiger.GetComponent().enabled = true;
        goTiger.transform.position = new Vector3(-25, 0, 0);
        goTiger.GetComponent().Move_Bl = true;
      

    }

    //右按钮
    public void Change_R() {

        //如果当前下标为2说明已经是第三张图片 则跳转到第一张图片 下标为零
        if (Count_Sprite==(Sprite_A.Length-1))
        {
            Count_Sprite = 0;
        }
        else
        {
            //否则 说明图片下标都大,加一
            Count_Sprite++;
        }
        
        //显示当前下标的图片
        Im_A_BG.sprite = Sprite_A[Count_Sprite];

        //执行按钮函数
        isShowTiger = true;

        //重置位置
        goTiger.GetComponent().enabled = true;
        goTiger.transform.position = new Vector3(-25, 0, 3);
        goTiger.GetComponent().Move_Bl = true;
        goTiger.GetComponent().speed = 5f;
    }

    //关闭提取码面板的函数
    public void Close_Im_A_In() {
        Im_A_In.SetActive(false);
    }

    //开启合影模块函数
    public void Open_A()
    {
        Im_A_BG_Obj.SetActive(true);
        //显示整个模块


        Im_A_Fc.SetActive(false);


        //Tx_A_Ct.text = "5";
        Tx_A_Ct.text = "3";

        Count_Bl = false;


        i = 0;


        //Count_Int = 5;
        Count_Int = 3;

        Im_A_BG.sprite = Sprite_A[0];


        Count_Sprite = 0;

        Bt_A_R.SetActive(true);


        Bt_A_L.SetActive(true);


        Bt_A_P.SetActive(true);


        Im_A_In.SetActive(false);


        Tx_AIn_In.text = "";


    }

    //关闭合影模块函数
    public void Close_A()
    {
        Im_A_BG_Obj.SetActive(false);
        //隐藏整个模块
    }


    /// 
    /// 生成二维码输入验证码图片的函数
    /// 
    /// 
    public void Encode_Ew(string Pic_Adress)
    {
        //把生成的二维码存贮在Textures2D类型变量Te_Ew中
        Te_Ew = QRCodeProcessor.Encode(Pic_Adress, 256, 256, ErrorCorrectionLevel.H, "123888"); //参数1:用来生成二维码的字符串,参数2,3:二维码宽度和高度;


        //将Te_Ew转化成精灵类型的图片
        Sprite tmpSprite = Sprite.Create(Te_Ew, new Rect(0, 0, 256, 256), new Vector2(0f, 0f)); //固定格式 参数1:图片,参数2后两个参数图片宽度和高度 参数3:固定格式;

        //将生成的sprite类型图片赋值给image组件中去;
        Im_A_Ew.sprite = tmpSprite;
        
    }
    
    //老虎出现后要消失延迟函数
    IEnumerator isActive()
    {
        yield return new WaitForSeconds(12f);
        GameObject go = GameObject.Find("tiger_idle");
        if (go!=null)
        {
            go.SetActive(false);
        }
        //gameObject.SetActive(false);
    }

    /// 
    /// 旋转的函数
    /// 
    /// 
    public Texture2D RotateTe2D()
    {
        for (int i = 0; i < width - 1; i++)
        {
            for (int j = 0; j < height - 1; j++)
            {
                Color color = Te2D.GetPixel(i, j);
                newTexture.SetPixel(j, width - 1 - i, color);
            }
        }
    
        newTexture.Apply();
        newTexture=ScaleTexture(newTexture,1000,1480);
        return newTexture;
    }

    /// 
    /// Texture2D 缩放效果
    /// 
    /// 
    /// 
    /// 
    /// 
    Texture2D ScaleTexture(Texture2D source, int targetWidth, int targetHeight)
    {
        Texture2D result = new Texture2D(targetWidth, targetHeight, source.format, false);

        float incX = (1.0f / (float)targetWidth);
        float incY = (1.0f / (float)targetHeight);

        for (int i = 0; i < result.height; ++i)
        {
            for (int j = 0; j < result.width; ++j)
            {
                Color newColor = source.GetPixelBilinear((float)j / (float)result.width, (float)i / (float)result.height);
                result.SetPixel(j, i, newColor);
            }
        }

        result.Apply();
        return result;
    }

}


CameraMode

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;  //  引用命名空间

public class CameraMode : MonoBehaviour {

    /// 
    /// 自动对焦功能
    /// 


    void Start () {

        //一开始自动对焦
        VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
        VuforiaARController.Instance.RegisterOnPauseCallback(OnPaused);
    }
    
    
    void Update () {
        
    }


    //对焦函数 实例化Ar相机和停止使用的时候判断

    private void OnVuforiaStarted() {

        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    }


    private void OnPaused(bool paused) {

        if (!paused)
        {
            CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        }
    }
}

G_Sc

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;    //操作文件流的命名空间;
using Antares.QRCode; //引用生成二维码的sdk命名空间;


public class G_Sc : MonoBehaviour {

    //储存整个虚拟礼物模块
    public GameObject Im_G_BG;

    //储存输入文件的文本;
    public InputField IF_G_Ip;

    //申请变量储存名字的内容
    public Text Tx_G_Nm;


    //申请私有变量来存储服务器的地址;
    private string uploadURL = "http://39.108.193.209/UpLoadImage.aspx";

    //用来服务器发回来图片的地址组件;
    public Text tmpAddress; //有二维码就不显示路径了

    //用来储存显示二维码的变量;
    public Image Im_G_Ew;

    //用来贮存最开始的二维码;原始生成的纹理一般都是用Texture2D 来处理
    private Texture2D Te_Ew;

    //贮存输入框面板;
    public GameObject Im_G_Ip; //这里为了方便隐藏设置成gameobject的类型;


    //储存传奇歌曲
    public AudioSource m_AudioSource;


   

    //储存二维码提示语
    public GameObject Tx_G_Ew;

    //储存点我按钮
    public GameObject Im_G_Btn;


    //储存祝福语
    public GameObject Tx_G_LG;
    public GameObject Tx_G_Am;
    public GameObject Tx_G_Radom;

    //随机祝福语的内容数组
    private string[] arrStr;
    void Start () {

        //对数组内容进行赋值
        arrStr = new string[] { "",
                                "",


        };

    }


    void Update () {
        
    }

    /// 
    /// 将输入文本框中的名字显示在贺卡处的函数;
    /// 
    public void Ip_Nm() {

        //让贺卡处的名字等于文本框输入的内容;
        Tx_G_Nm.text = IF_G_Ip.text;
    }


    /// 
    ///  截图功能的函数 名字写完之后 把卡片截取下来上传到服务器
    /// 
    public void Sn_Shot() {


        //截屏时候先把输入框隐藏 (名字面板)的面板;
        Im_G_Ip.SetActive(false);

        //调用协程函数 延迟一帧后 再执行具体的截图函数;
        StartCoroutine(Shot());
       
        
    }


    //开启协程 保证取消其他UI对象,并且在刷新界面后进行截图 取消其他界面的干扰
    IEnumerator Shot() {

        yield return null; //必须有返回值 返回空 至少在下一帧执行,所以开启协程的意思就是先隐藏界面,下一帧再执行截屏操作;

        //Texture2D 是一种纹理格式  实例化出来
        //Texture2D Te = new Texture2D(600, 900, TextureFormat.RGB24, false); //参数1,2表示宽、高,参数3:表示颜色格式 参数4:对纹理不分析;
        Texture2D Te = new Texture2D(572, 850, TextureFormat.RGB24, false);//手机端

        //Texture2D Te = new Texture2D(400, 600, TextureFormat.RGB24, false);//pc 1366

        //读取屏幕的像素;
        //矩形 参数1、2是矩形起始的位置 参数3,4是矩形的大小;参数5,6,7一般不需要修改; 固定格式;
        //Te.ReadPixels(new Rect(100,100,600,800),0,0,true);
        //Te.ReadPixels(new Rect(260, 140, 600, 900), 0, 0, true); //参数1:起始位置的宽 参数2:起始位置的高 参数3:贺卡的宽 参数4:贺卡的高  参数3,参数4 必须与上图的纹理格式尺寸必须保持一致;
        Te.ReadPixels(new Rect(310, 175, 572, 850), 0, 0, true);//手机端

        //Te.ReadPixels(new Rect(220, 130, 400, 600), 0, 0, true); //pc1366
        //保存更改;
        Te.Apply();

        //将Texture2D转换成png格式存储在字节中 Bytes 这个变量中去 (特定方法,请记住 将2d变成png图片存储)
        //byte[] bytes = Te.EncodeToPNG();
        byte[] bytes = Te.EncodeToJPG();

        //将转换好的png图片存储在资源文件中去(特定方法,存储图片)
        //File.WriteAllBytes(Application.dataPath+"/ScreenShot.png",bytes); //存储到网络后不需要存储到本地了;

        //---------------------------------------------网络部分----------------------------
        //WWWForm是网络表单,用来生成表单数据,使用www类传递到Web服务器;
        WWWForm wwwForm = new WWWForm();

        //将转化好的  二进制数组的图片以及服务器接口 添加至网络表单中
        wwwForm.AddBinaryData("post", bytes);

        //使用www类来与Web服务器进行交互  参数为服务器地址和网络表单;
        WWW www = new WWW(uploadURL, wwwForm);


        //开启协程 用来获取下载地址的 协程函数 并将www作为参数传入;
        StartCoroutine(postImage(www));
    }




    //获取下载地址的协程函数;
    IEnumerator postImage(WWW www)
    {
        //返回www类型的参数值;
        yield return www;


        //协程执行结束后,执行以下内容
        //将服务器返回来的图片地址 显示在Text组件中
        tmpAddress.text = www.text; //有二维码就不储存了

        //将服务器返回来的图片网址字符串 作为参数传到生成二维码图片的函数中去;
        Encode_Ew(www.text);


    }

    /// 
    /// 生成二维码图片的函数
    /// 
    /// 
    public void Encode_Ew(string Pic_Adress) {
        //把生成的二维码存贮在Textures2D类型变量Te_Ew中
        Te_Ew = QRCodeProcessor.Encode(Pic_Adress,256,256,ErrorCorrectionLevel.H,"123888"); //参数1:用来生成二维码的字符串,参数2,3:二维码宽度和高度;
       

        //将Te_Ew转化成精灵类型的图片
        Sprite tmpSprite = Sprite.Create(Te_Ew,new Rect(0,0,256,256),new Vector2(0f,0f)); //固定格式 参数1:图片,参数2后两个参数图片宽度和高度 参数3:固定格式;

        //将生成的sprite类型图片赋值给image组件中去;
        Im_G_Ew.sprite = tmpSprite;
    }

    //----------------------------------网络部分---------------------------------------


    //开启虚拟礼物模块
    public void Open_G()
    {
        Im_G_BG.SetActive(true);
        //显示虚拟礼物模块

        IF_G_Ip.text = "";


        Tx_G_Nm.text = "";

        
        Im_G_Ew.sprite = null;

        //隐藏二维码提示语
        Tx_G_Ew.SetActive(false);

        //储存点我按钮
        Im_G_Btn.SetActive(true);

        //输入按钮不用显示
        //Im_G_Ip.SetActive(true);

        //隐藏祝福语;
        Tx_G_LG.SetActive(false);
        Tx_G_Am.SetActive(false);
        Tx_G_Radom.SetActive(false);

        //播放传奇歌曲函数
        m_AudioSource.Play();
    }

    //关闭虚拟礼物模块
    public void Close_G()
    {
        Im_G_BG.SetActive(false);
        //隐藏虚拟礼物模块
        //暂停播放传奇歌曲按钮
        m_AudioSource.Pause();
    }



    //播放传奇歌曲函数
    //public void PlayMusic() {

    //    m_AudioSource.Play();
    //}


    //暂停播放传奇歌曲按钮
    //public void PauseMusic() {
    //    m_AudioSource.Pause();
    //}


    //显示输入名字的函数
    public void ShowNameLp() {
        Im_G_Ip.SetActive(true);
    }

    public void ShowText() {

        //把祝福语显示出来
        Tx_G_LG.SetActive(true);
        Tx_G_Am.SetActive(true);
        //随机祝福语
        Tx_G_Radom.GetComponent().text = arrStr[Random.Range(0, arrStr.Length)];
        Tx_G_Radom.SetActive(true);

        //把二维码提示语显示出来
        Tx_G_Ew.SetActive(true);


        //把点我 的按钮隐藏
        Im_G_Btn.SetActive(false);
    }

}

Kb_Sc


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;   //  引入相关命名空间;
using UnityEngine.EventSystems;
using WindowsInput;


public class Kb_Sc : MonoBehaviour {

    //申请变量储存名字输入框的组件;
    public InputField If_G_Ip;

    void Start () {
        
    }
    

    void Update () {
        
    }

    #region Btn 输入
    public void Btn_Ctrl_Space() {
        If_G_Ip.ActivateInputField(); //获取焦点;
        //开启协程
        StartCoroutine(Key_Ctrl_Space());
    }

    public void Btn_Ctrl_Shift()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_Ctrl_Shift());
    }

    public void Btn_Backspace()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_Backspace());
    }

    public void Btn_1()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_1());
    }

    public void Btn_2()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_2());
    }

    public void Btn_3()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_3());
    }

    public void Btn_4()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_4());
    }

    public void Btn_5()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_5());
    }

    public void Btn_6()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_6());
    }

    public void Btn_7()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_7());
    }

    public void Btn_8()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_8());
    }

    public void Btn_9()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_9());
    }

    public void Btn_Q()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_Q());
    }

    public void Btn_W()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_W());
    }

    public void Btn_E()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_E());
    }

    public void Btn_R()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_R());
    }

    public void Btn_T()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_T());
    }

    public void Btn_Y()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_Y());
    }

    public void Btn_U()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_U());
    }

    public void Btn_I()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_I());
    }

    public void Btn_O()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_O());
    }

    public void Btn_P()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_P());
    }

    public void Btn_A()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_A());
    }

    public void Btn_S()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_S());
    }

    public void Btn_D()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_D());
    }

    public void Btn_F()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_F());
    }

    public void Btn_G()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_G());
    }

    public void Btn_H()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_H());
    }

    public void Btn_J()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_J());
    }

    public void Btn_K()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_K());
    }

    public void Btn_L()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_L());
    }

    public void Btn_Z()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_Z());
    }

    public void Btn_X()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_X());
    }

    public void Btn_C()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_C());
    }

    public void Btn_V()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_V());
    }

    public void Btn_B()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_B());
    }

    public void Btn_N()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_N());
    }

    public void Btn_M()
    {
        If_G_Ip.ActivateInputField();
        StartCoroutine(Key_M());
    }


    #endregion


    #region Btn_TEnumerrator
    IEnumerator Key_Ctrl_Space()
    {

        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false); //把光标移到最后 

        InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.SPACE); //输入按键
    }

    IEnumerator Key_Ctrl_Shift()
    {

        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);

        InputSimulator.SimulateModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.SHIFT);
    }

    IEnumerator Key_1()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1);
    }

    IEnumerator Key_2()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_2);
    }

    IEnumerator Key_3()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_3);
    }

    IEnumerator Key_4()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_4);
    }

    IEnumerator Key_5()                 
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_5);
    }

    IEnumerator Key_6()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_6);
    }

    IEnumerator Key_7()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_7);
    }

    IEnumerator Key_8()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_8);
    }

    IEnumerator Key_9()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_9);
    }

    IEnumerator Key_Q()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_Q);
    }

    IEnumerator Key_W()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_W);
    }

    IEnumerator Key_E()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_E);
    }

    IEnumerator Key_R()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_R);
    }

    IEnumerator Key_T()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_T);
    }

    IEnumerator Key_Y()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_Y);
    }

    IEnumerator Key_U()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_U);
    }

    IEnumerator Key_I()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_I);
    }

    IEnumerator Key_O()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_O);
    }

    IEnumerator Key_P()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_P);
    }

    IEnumerator Key_A()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_A);
    }

    IEnumerator Key_S()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_S);
    }

    IEnumerator Key_D()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_D);
    }

    IEnumerator Key_F()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_F);
    }

    IEnumerator Key_G()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_G);
    }

    IEnumerator Key_H()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_H);
    }

    IEnumerator Key_J()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_J);
    }

    IEnumerator Key_K()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_K);
    }

    IEnumerator Key_L()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_L);
    }

    IEnumerator Key_Z()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_Z);
    }

    IEnumerator Key_X()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_X);
    }

    IEnumerator Key_C()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_C);
    }

    IEnumerator Key_V()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_V);
    }

    IEnumerator Key_B()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_B);
    }

    IEnumerator Key_N()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_N);
    }

    IEnumerator Key_M()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_M);
    }

    IEnumerator Key_Backspace()
    {
        yield return null;
        If_G_Ip.GetComponent().MoveTextEnd(false);
        InputSimulator.SimulateKeyPress(VirtualKeyCode.BACK);
    }


    #endregion
}


Screen_Ctrl

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

public class Screen_Ctrl : MonoBehaviour {


    void Start () {
        //开始运行的时候屏幕自适应;
        //Screen.SetResolution(1920,1080,true);

        Screen.SetResolution(1366, 768, true);
    }
    
    void Update () {
        //按退出键 退出游戏
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }
}

SetRenderQueue

/*
    SetRenderQueue.cs
    
    Sets the RenderQueue of an object's materials on Awake. This will instance
    the materials, so the script won't interfere with other renderers that
    reference the same materials.
*/

using UnityEngine;

[AddComponentMenu("Rendering/SetRenderQueue")]

public class SetRenderQueue : MonoBehaviour {
    
    [SerializeField]
    protected int[] m_queues = new int[]{3000};
    
    //放入shader的脚本 主要遮挡物体
    protected void Awake() {
        Material[] materials = GetComponent().materials;
        for (int i = 0; i < materials.Length && i < m_queues.Length; ++i) {
            materials[i].renderQueue = m_queues[i];
        }
    }
}

V_Sc

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


public class V_Sc : MonoBehaviour {

    //储存整个广告播放模块
    public GameObject Rim_V_BG_Obj;

    //用来储存播放视频的组件
    //public RawImage Rim_V_BG;

    //变量申请
    public MediaPlayerCtrl scrMedia;

    public bool m_bFinish = false;


    //用来储存视频
    //public MovieTexture Vo;

    //----------------------
    //视频数组
    //public MovieTexture[] Vo;

    //下标
    int index=0;
    //----------------------

    //用来储存视频中的声音;
    //private AudioClip VoDu;
    //public AudioClip VoAu;


    //定义字符串参数;
    string VideoName = "";

    void Start () {

        //一开始就播放;
        //St_Vo();

        scrMedia.OnEnd += OnEnd;
    }


    void Update () {
        
    }

    /// 
    /// 播放广告视频的函数;
    /// 
    public void St_Vo() {

        //随机播放视频的下标
       //index = Random.Range(0,Vo.Length);

        //清空视频
        scrMedia.UnLoad();

        //显示整个广告视频模块
        Rim_V_BG_Obj.SetActive(true);

        //允许视频循环播放;
        //Vo[index].loop = true;

        //播放视频
        //Vo[index].Play();

        //将RawImage上的纹理设置为视频
        //Rim_V_BG.texture = Vo[index];

        //对视频中的声音进行赋值;
        //VoAu = Vo[index].audioClip;

        //将声音源中的组件赋值为视频中的声音
        //gameObject.GetComponent().clip = VoAu;

        //声音源的组件进行声音的播放;
        //gameObject.GetComponent().Play();
        
        

        //下标累加
        index++;

        //视频名称赋值
        VideoName = "k" + index + ".mp4";

        //停止生成动物
        //GameObject.Find("P_01").GetComponent().enabled = false;


        //-------------------------Easy MoiveTexture--------------------------------------

        //显示播放视频的组件
        scrMedia.gameObject.SetActive(true);

        //载入视频
        //scrMedia.Load("k1.mp4");
        scrMedia.Load(VideoName);
        //m_bFinish = false;
        //Debug.Log("载入视频····");

        //播放视频 调用协程函数 不能同时载入同时播放 否则会卡顿
        //StartCoroutine(PlayVedio());
        StartCoroutine("PlayVedio");
        //Debug.Log("开启播放视频————————————————————");
        
        //达到视频长度归零
        if (index == 5)
        {
            index = 0;
        }

        //-----------------------------------------------------------------------------------

    }

    /// 
    /// 延迟播放视频的函数
    /// 
    /// 
    IEnumerator PlayVedio() {
        //Debug.Log("协程函数··········");
        //延迟0.3毫秒后播放
        yield return new WaitForSeconds(0.3f);
        scrMedia.Play();
        m_bFinish = false;

    }



    /// 
    /// 停止播放视频的方法
    /// 
    public void Stop_Vo() {
        //停止视频的播放;
        //Vo[index].Stop();

        ////停止声音的播放;
        //gameObject.GetComponent().Stop();

        //隐藏整个广告视频播放模块
        //Rim_V_BG_Obj.SetActive(false);




        //-------------------------Easy MoiveTexture--------------------------------------

        //Debug.Log("停止播放视频···········");
        scrMedia.UnLoad();

        //隐藏播放视频的组件
        scrMedia.gameObject.SetActive(false);

        //隐藏整个广告视频播放模块
        Rim_V_BG_Obj.SetActive(false);

        //-----------------------------------------------------------------------------------


    }

    void OnEnd()
    {
        StartCoroutine(IsEnd());
    }


    IEnumerator IsEnd()
    {

        yield return new WaitForSeconds(1f);
        //m_bFinish = true;
    }
}

UI_Manager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI; //引用命名空间

public class UI_Manager : MonoBehaviour {

    //开始可以默认 识别之后可以播放
    public bool isPlayVideo =true;


    //马预制体隐藏起来的物体 方便调用
    public GameObject goPrefab;

    //接收预制体 方便销毁
    private GameObject go;

    //储存祝福语
    //public GameObject Tx_G_LG;
    //public GameObject Tx_G_Am;
    ////随机祝福语
    //public GameObject Tx_G_Radom;
    ////随机祝福语的内容数组
    //private string[] arrStr;

    ////储存二维码提示语
    //public GameObject Tx_G_Ew;

    ////储存点我按钮
    //public GameObject Im_G_Btn;


    void Start () {

        //对数组内容进行赋值
        //arrStr = new string[] { "",
       ",


        //};
    }
    
    
    void Update () {
        
    }
    /// 
    /// 奇幻旅程的按钮
    /// 
    public void Bt_M_V_Q()
    {
        //调用本物体下的脚本(V_Sc)中的播放视频的方法;
        gameObject.GetComponent().St_Vo();
        //停止视频的播放;
        //gameObject.GetComponent().Stop_Vo();

        gameObject.GetComponent().Close_A();
        gameObject.GetComponent().Close_G();

        //暂停音乐
        //gameObject.GetComponent().PauseMusic();

        //可以播放视频
        isPlayVideo = true;

        //停止老虎的出现
        gameObject.GetComponent().isShowTiger = false;
        //停止动物实例化生成
        GameObject.Find("P_01").GetComponent().enabled = false;


        //隐藏已经生成的动物
        GameObject.Find("P_01").GetComponent().HideAnimals();
    }

    /// 
    /// 拍照按钮
    /// 
    public void Bt_M_A_P()
    {

        //拍照之前先把视频停止播放;
        //同理,获取本物体下的V_Sc中停止播放视频的方法;
        gameObject.GetComponent().Open_A();
        gameObject.GetComponent().Close_G();
        gameObject.GetComponent().Stop_Vo();

        //暂停音乐
        //gameObject.GetComponent().PauseMusic();

        //不可以播放视频
        isPlayVideo = false;

        //判断不是第一张才停止生成
        if (gameObject.GetComponent().Count_Sprite!=0)
        {
            //停止动物实例化生成
            GameObject.Find("P_01").GetComponent().enabled = false;
            //隐藏已经生成的动物
            //GameObject.Find("P_01").GetComponent().HideAnimals();
        }
       
    }


    

    //虚拟礼物按钮
    public void Bt_M_G_X()
    {
        //播放音乐
        //gameObject.GetComponent().PlayMusic();
        gameObject.GetComponent().Close_A();
        gameObject.GetComponent().Open_G();
        gameObject.GetComponent().Stop_Vo();
   

        //不可以播放视频
        isPlayVideo = false;
        //停止老虎的出现
        gameObject.GetComponent().isShowTiger = false;
        //停止动物实例化生成
        GameObject.Find("P_01").GetComponent().enabled = false;
        //隐藏已经生成的动物
        GameObject.Find("P_01").GetComponent().HideAnimals();
    }

    //返回按钮
    public void Bt_M_V_F()
    {
        //gameObject.GetComponent().St_Vo();
        ////调用UI_Manager所依附的物体上的V_Sc脚本中的St_Vo函数
        //gameObject.GetComponent().Close_A();
        //gameObject.GetComponent().Close_G();

    

        //重新加载场景
        //SceneManager.LoadScene("MainAnimal");

        //返回按钮直接退出
        //Application.Quit();

        //关闭所有脚本,恢复原始状态
        gameObject.GetComponent().Close_A();
        gameObject.GetComponent().Close_G();
        gameObject.GetComponent().Stop_Vo();

        //暂停音乐
        //gameObject.GetComponent().PauseMusic();

        //停止老虎的出现
        gameObject.GetComponent().isShowTiger = false;

        //寻找一个动物 马上显示或者延迟显示 协程函数 
        StartCoroutine("ShowNext");
        //动物实例化生成
        GameObject.Find("P_01").GetComponent().enabled = true;
    }


    /// 
    /// 输完名字后点击确认按钮的函数;
    /// 
    public void Bt_Glp_St() {


        //同理,获取物体上挂靠礼物脚本中将输入框的内容复制给贺卡处的方法
        //输入名字
        gameObject.GetComponent().Ip_Nm(); //方法前面必须是公开的;

        //截图 保存图片在资源文件下中去;
        gameObject.GetComponent().Sn_Shot(); //调用物体上的管理 礼物的脚本中保存截图的方法;

        //把祝福语显示出来
        //Tx_G_LG.SetActive(true);
        //Tx_G_Am.SetActive(true);
        ////随机祝福语
        //Tx_G_Radom.GetComponent().text = arrStr[Random.Range(0,arrStr.Length)];
        //Tx_G_Radom.SetActive(true);

        ////把二维码提示语显示出来
        //Tx_G_Ew.SetActive(true);


        ////把点我 的按钮隐藏
        //Im_G_Btn.SetActive(false);
        gameObject.GetComponent().ShowText();
    }

    //写完功能函数要写点击按钮函数 方便调用;
    //向左切换明星图片的按钮
    public void Bt_A_L() {
        gameObject.GetComponent().Change_L();
    }

    //向右切换明星图片的按钮
    public void Bt_A_R() {
        gameObject.GetComponent().Change_R();
    }


    //开始合影按钮
    public void Bt_A_P() {
        gameObject.GetComponent().Count_Start(); //开启倒计时函数
        //不要实例化物体
       //停止动物实例化生成
        GameObject.Find("P_01").GetComponent().enabled = false;
    }

    //提取码面板的确认按钮
    public void Bt_AIn_A_Q() {
        gameObject.GetComponent().Close_Im_A_In();
    }

    //提取码面板的返回按钮
    public void Bt_AIn_A_F() {
        gameObject.GetComponent().Close_Im_A_In();
    }


    //延迟函数
   public  IEnumerator ShowNext() {
        yield return new WaitForSeconds(0.5f);
        //GameObject go = GameObject.FindGameObjectWithTag("Fish") as GameObject;
        //if (go != null)
        //{
        //    go.SetActive(true);
        //}
        ////10秒后销毁物体
        ////Destroy(go,10f);

        ////再过10秒继续隐藏
        //yield return new WaitForSeconds(10f);
        //if (go!=null)
        //{
        //    go.SetActive(false);
        //}

        //实例化物体
        go = GameObject.Instantiate(goPrefab, new Vector3(-25,0,-5),Quaternion.identity) as GameObject;
        go.transform.localRotation = Quaternion.Euler(0,90,0);
        Destroy(go,10f);
     
    }


    //退出按钮
    public void ExitGame() {

        Application.Quit();
    }
}

三、效果展示

你可能感兴趣的:(AR开发实战EasyAR项目之互动大屏(服务器交互与拍照视频)上)