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

一、框架视图

二、关键代码

Aniaml
Anim_Manager

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

public class Anim_Manager : MonoBehaviour {


    //动画状态机的储存
    public Animator V_Ani;

    //储存恐龙物体
    public GameObject V_Obj;

    //获取水平和垂直 的Vaule;
    private float translationFB;
    private float translationRL;



    //低头闻的声音文件
    public AudioClip Smell_Clip;

    void Start () {

       

    }


    void Update () {
        
     

        //控制吃的函数
        Eat();

        // 控制闻的函数
        Smell();

   

        //当满足正在播放恐龙的移动动画时才执行以下内容
        if (V_Ani.GetCurrentAnimatorStateInfo(0).IsName("Blend Tree"))  //参数1:动画层 参数2:动画名称
        {
            // 控制尖叫的函数
            Bark();
            //这个就处理了当恐龙尖叫时处理了bug
        }




        //当满足正在播放恐龙的移动动画时才执行以下内容
        if (V_Ani.GetCurrentAnimatorStateInfo(0).IsName("Blend Tree"))  //参数1:动画层 参数2:动画名称
        {
            //控制位移的函数;
            Translate_Ctrl();
            //这个就处理了当恐龙吃东西时 按ASDW不会有反应 处理了bug
        }

    

        //控制行走动画的函数
        Move_Anim();

        //控制向左旋转;
        Ro_Left();

        //控制向右旋转
        Ro_Right();
    }

    /// 
    ///控制位移的函数;
    /// 
    void Translate_Ctrl() {
        translationFB = Input.GetAxis("Vertical") * 0.08f; //垂直
        translationRL = Input.GetAxis("Horizontal") * 0.08f;    //水平

        //恐龙的位移;
        V_Obj.transform.Translate(translationRL, 0, translationFB);
    }

    /// 
    /// 控制行走动画的函数
    /// 
    void Move_Anim() {

        //动画已经融合,通过水平和垂直来控制前后左右移动 融合树
        V_Ani.SetFloat("FW", Input.GetAxis("Vertical")); //垂直 参数1:设置条件名称 参数2:设置值

        V_Ani.SetFloat("LR", Input.GetAxis("Horizontal")); //水平 参数1:设置条件名称 参数2:设置值
    }

    /// 
    /// 控制吃的函数
    /// 
    void Eat()
    {
        //按下C键,激活触发器 吃的动作
        //if (Input.GetKey(KeyCode.C))    //键盘操作改成手柄按键
        if (Input.GetKey(KeyCode.C)&&Input.GetButtonDown("Fire1"))  //手柄 开火键
        {
            V_Ani.SetTrigger("Eat");   //设置触发的关键语句
        }
    }

    /// 
    /// 控制闻的函数
    /// 
    void Smell() {

        //按下X键,激活闻 动作;
        //if (Input.GetKey(KeyCode.X)) //键盘操作改成手柄操作
        if (Input.GetKey(KeyCode.X)&&Input.GetButtonDown("Jump"))  //手柄 跳跃键
        {
            V_Ani.SetTrigger("Smell");
            //播放闻的声音文件
            //获取声音组件
            gameObject.GetComponent().clip = Smell_Clip;
            //获取声音播放组件的速度为1.5倍
            gameObject.GetComponent().pitch = 1.5f;
           
            //播放
            gameObject.GetComponent().Play();
        }
    }

    /// 
    /// 控制尖叫的函数
    /// 
    void Bark()
    {

        //按下R键,激活Bark动作
        if (Input.GetKey(KeyCode.R))
        {
            V_Ani.SetTrigger("Bark");
        }
    }

    /// 
    /// 向左旋转
    /// 
    void Ro_Left() {
        if (Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(0, -25 * Time.deltaTime, 0, Space.Self);
        }
    }

    /// 
    /// 向右旋转
    /// 
    void Ro_Right() {
        if (Input.GetKey(KeyCode.E))
        {
            transform.Rotate(0,25*Time.deltaTime,0,Space.Self);
        }
    }

}

Animal_Auto

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

public class Animal_Auto : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 5.0f;
    //储存走路时间

    public Animal_Auto inst;

    public Animator V_Ani;

    public bool Move_Bl = true;
    //控制恐龙是否行走

    //低头闻的声音文件
    // public AudioClip Smell_Clip;

    //定义销毁时间
    //private float destoryTime;

    //判断销毁时间
    public  bool isDestory=false;

    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();
        //Invoke("isBarking", 3.8f); //函数的调用
        //让恐龙经过1.8秒后吃东西
        //Invoke("Moving", 4f); //函数调用
        //让恐龙经过8.8秒(吃东西动作播放完)后继续走路
        //Invoke("Attacking", 7.8f);
        //Invoke("isMoving", 4.5f);
        // Invoke("Smelling", 7.8f);
        //Invoke("isEating", 6.8f);
        //Invoke("isMovingNext", 12.8f);

        //给动画状态机赋值
        //V_Ani = gameObject.GetComponent();


        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());

    }
    
    // Update is called once per frame
    void Update () {
        //if (Move_Bl) {
            RotateTo();
            MoveTo();
        //}

       
    }

    //让恐龙执行吃东西动画的函数
    public void isBarking()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("Barking");
        
    }

    public void isAttacking()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("Attacking");
    }

    public void isSmelling()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("Smelling");
        //gameObject.GetComponent().clip = Smell_Clip;
        //获取声音播放组件的速度为1.5倍
        gameObject.GetComponent().pitch = 1.5f;
        gameObject.GetComponent().volume = 2f;
        //播放
        gameObject.GetComponent().Play();
    }


    public void isEating() {
        
        V_Ani.SetTrigger("Eating");

        //执行协程函数
        StartCoroutine("isEat");
    }

    public void isMoving() {
        Move_Bl = true;
        V_Ani.SetTrigger("Moving");

;
    }

    public void isMovingNext()
    {

        V_Ani.SetTrigger("Moving");

        //执行协程函数
        StartCoroutine("isMove");
    }


    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
      
        //判断时间
        Destroy(gameObject, 45f);
    }



    //延迟函数 协程
    IEnumerator isEat() {
        yield return new WaitForSeconds(1.5f);
        Move_Bl = false;
    }

    IEnumerator isMove()
    {
        yield return new WaitForSeconds(1.5f);
        Move_Bl = true;
    }


    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(20f);
        gameObject.transform.GetComponent().enabled = false;
    }
}

Animal_Auto_Butterfly

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

public class Animal_Auto_Butterfly : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 5.0f;
    //储存走路速度

    //随机角度
   // private float index;

    public Animal_Auto_Butterfly inst;

   // public Animator V_Ani;



   

    //public AudioSource[] m_AudioSource;

    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();
        //给动画状态机赋值
        //V_Ani = gameObject.GetComponent();
        //播放走路的声音
        //m_AudioSource[0].Play();
        //m_AudioSource[0].pitch = 0.8f;
        //m_AudioSource[0].loop = true;

        //index = Random.Range(0, 1);

        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());
    }
    
    // Update is called once per frame
    void Update () {

            RotateTo();
            MoveTo();
   
        
    }



    





    


    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = this.transform.eulerAngles*0.4f;
        //Vector3 target = this.transform.eulerAngles * index;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }

      
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
        //transform.Translate(-(new Vector3(speed * Time.deltaTime,0, 0)));
        //销毁对象
        Destroy(gameObject,18f);
    }


    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(18f);
        gameObject.transform.GetComponent().enabled = false;
    }

}

Animal_Auto_Din

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

public class Animal_Auto_Din : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 5.0f;
    //储存走路速度

    public Animal_Auto_Din inst;

    //public Animator V_Ani;

    public bool Move_Bl = true;
    //控制恐龙是否行走

    //低头闻的声音文件
    // public AudioClip Smell_Clip;

    public AudioSource[] m_AudioSource;

    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();

        //播放走路的声音
        m_AudioSource[0].Play();
        m_AudioSource[0].pitch = 0.5f;
        m_AudioSource[0].loop = true;

        //调用恐龙动画
        Invoke("isAllosaurus_IdleBellow", 5.8f);

        Invoke("isAllosaurus_Walk", 9.8f);

        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());
    }
    
    // Update is called once per frame
    void Update () {
        if (Move_Bl) {
            //RotateTo();
            MoveTo();
        }
        
    }



    

    public void isAllosaurus_IdleBellow()
    {
        Move_Bl = false;
        gameObject.transform.GetComponent().Play("Allosaurus_IdleBellow");
        //transform.animation.Play("Allosaurus_IdleBellow");
        //gameObject.GetComponent().clip = Smell_Clip;
        //获取声音播放组件的速度为1.5倍
        //gameObject.GetComponent().pitch = 1.5f;
        //gameObject.GetComponent().volume = 2f;
        //播放
        //gameObject.GetComponent().Play();
        m_AudioSource[0].Stop();
        m_AudioSource[1].Play();
    }


    public void isAllosaurus_Walk() {

        Move_Bl = true;
        gameObject.transform.GetComponent().Play("Allosaurus_Walk");
        //执行协程函数
        //StartCoroutine("isEat");
        //播放恐龙的走路声音
        m_AudioSource[0].Play();
    }


    


    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = -this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(-(new Vector3(0, 0, speed * Time.deltaTime)));
        //transform.Translate(-(new Vector3(speed * Time.deltaTime,0, 0)));
        //销毁对象
        Destroy(gameObject,41f);
    }



    //延迟函数 协程
    IEnumerator isEat() {
        yield return new WaitForSeconds(1.5f);
        Move_Bl = false;
    }

    IEnumerator isMove()
    {
        yield return new WaitForSeconds(1.5f);
        Move_Bl = true;
    }


    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(6.5f);
        gameObject.transform.GetComponent().enabled = false;
    }
}

Animal_Auto_Din_G

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

public class Animal_Auto_Din_G : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 10f;
    //储存走路时间

    public Animal_Auto_Din_G inst;



    //public AudioSource m_AudioSource;

    //低头闻的声音文件
    // public AudioClip Smell_Clip;


    //private bool isDestory=false;
    //private float destoryTime;
    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();

        //m_AudioSource.Play();

        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());
      
    }
    
    // Update is called once per frame
    void Update () {

            MoveTo();
        //if (Input.GetMouseButtonDown(0))
        //{
        //    destoryTime = 12f;
        //}
        //else
        //{
        //    destoryTime = 4f;
        //}

        //Destroy(gameObject,destoryTime);
    }



    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = -this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(-(new Vector3(0, 0, speed * Time.deltaTime)));
        //transform.Translate(-(new Vector3(speed * Time.deltaTime,0, 0)));
        //销毁对象
        Destroy(gameObject,25.5f);
    }


    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(6.5f);
        gameObject.transform.GetComponent().enabled = false;
    }


    void OnBecameVisible()
    {
        Debug.Log(gameObject.name + "这个物体出现在屏幕里面了");

    }

    //物体离开屏幕  
    void OnBecameInvisible()
    {

        Debug.Log(gameObject.name + "这个物体离开屏幕里面了");


    }
}

Animal_Auto_ElePhant

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

public class Animal_Auto_ElePhant : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 5.0f;
    //储存走路速度

    public Animal_Auto_ElePhant inst;

    public Animator V_Ani;

    //public bool Move_Bl = true;
    //控制大象是否行走

    //低头闻的声音文件
    // public AudioClip Smell_Clip;

    //public AudioSource[] m_AudioSource;

    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();
        //给动画状态机赋值
        V_Ani = gameObject.GetComponent();
        //播放走路的声音
        //m_AudioSource[0].Play();
        //m_AudioSource[0].pitch = 0.8f;
        //m_AudioSource[0].loop = true;


        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());
    }
    
    // Update is called once per frame
    void Update () {

            //RotateTo();
            MoveTo();
   
        
    }



    





    


    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = -this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
        //transform.Translate(-(new Vector3(speed * Time.deltaTime,0, 0)));
        //销毁对象
        Destroy(gameObject,33f);
    }




    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(12f);
        gameObject.transform.GetComponent().enabled = false;
    }

}

Animal_Auto_Horse

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

/// 
/// 马行走的函数
/// 
public class Animal_Auto_Horse : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 5.0f;
    //储存走路时间

    public Animal_Auto_Horse inst;

    public Animator V_Ani;

    public bool Move_Bl = true;
    //控制马是否行走 马要吃东西的时候 要停下来

    public AudioSource[] m_AudioSource;

    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();
 
        //Invoke("isEating", 6.8f);
        //Invoke("isMovingNext", 12.8f);

        //给动画状态机赋值
        V_Ani = gameObject.GetComponent();


        //播放行走的声音
        m_AudioSource[0].Play();
        StartCoroutine("PlayAudio");


        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());
    }
    
    // Update is called once per frame
    void Update () {
        //if (Move_Bl) {
           // RotateTo();
            MoveTo();
        //}
        
    }




    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
        Destroy(gameObject,22.5f);
      
    }


    //协程延迟函数
    IEnumerator PlayAudio() {

        yield return new WaitForSeconds(3.1f);
        speed = 10f;
        m_AudioSource[0].Stop();
        m_AudioSource[1].Play();
      
        yield return new WaitForSeconds(3f);
        m_AudioSource[1].Stop();
        m_AudioSource[0].Play();
        speed = 5f;
    }


    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(8.5f);
        gameObject.transform.GetComponent().enabled = false;
    }
}

Animal_Auto_Tiger

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

public class Animal_Auto_Tiger : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 5.0f;
    //储存走路速度

    public Animal_Auto_Tiger inst;

    public Animator V_Ani;

    public bool Move_Bl = true;
    //控制恐龙是否行走

    public AudioSource[] m_AudioSource;


    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();
        //给动画状态机赋值
        V_Ani = gameObject.GetComponent();

        //播放老虎的声音
        m_AudioSource[0].Play();
        m_AudioSource[0].loop = true;
            
        Invoke("isHitting", 3f);

        Invoke("isRunning", 6f);


        //不销毁,隐藏起来
        StartCoroutine("isActive");


        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());


        
    }
    
    // Update is called once per frame
    void Update () {
        if (Move_Bl) {
            //RotateTo();
            MoveTo();
        }
        
    }

    //让恐龙执行吃东西动画的函数
    

    

    public void isHitting()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("hitting");
        //播放老虎咆哮的声音
        m_AudioSource[1].Play();
        m_AudioSource[0].Stop();
    }


  

    public void isRunning() {
       
        V_Ani.SetTrigger("running");
        speed = 10.0f;
        //播放老虎跑步的声音
        m_AudioSource[2].Play();
        m_AudioSource[2].loop = true;

        //执行协程函数
        if (gameObject.active)
        {
            StartCoroutine("isRun");
        }
     
    }

 


    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
        //不销毁,隐藏
        //Destroy(gameObject,12f);
        
    }



    //延迟函数 协程
    IEnumerator isRun() {
        yield return new WaitForSeconds(0.5f);
        Move_Bl = true;
    
    }

    //延迟函数
     IEnumerator isActive() {
        yield return new WaitForSeconds(12f);
        //GameObject.Find("tiger_idle").SetActive(false);
        gameObject.SetActive(false);
    }

    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(12f);
        gameObject.transform.GetComponent().enabled = false;
    }



  
}

Audio_Ctrl

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

public class Audio_Ctrl : MonoBehaviour {

    void Start() {

    }

    //加入事件函数;
    //尖叫声音片段
    public AudioClip Bark_Clip;

    //吃 声音片段
    public AudioClip Eat_Clip;


    //走路 声音片段
    public AudioClip Walk_Clip;

    //尖叫声音的方法
    public void Au_Bark() {
        //获取声音片段 速度 播放;
        gameObject.GetComponent().clip = Bark_Clip;
        gameObject.GetComponent().pitch = 2f;
        gameObject.GetComponent().volume = 0.1f;
        gameObject.GetComponent().Play();
    }

    /// 
    /// 吃 的事件函数
    /// 
    public void Au_Eat()
    {
        //获取声音片段 速度 播放;
        gameObject.GetComponent().clip = Eat_Clip;
        gameObject.GetComponent().pitch = 0.8f;
        gameObject.GetComponent().volume = 1f;
        gameObject.GetComponent().Play();

    }
    /// 
    /// 恐龙走路的事件函数
    /// 
    public void Au_Walk() {
        //获取声音片段 循环 速度 播放;
        gameObject.GetComponent().clip = Walk_Clip;
        //gameObject.GetComponent().loop = true;
        gameObject.GetComponent().pitch = 1f;
        gameObject.GetComponent().volume = 1f;
        gameObject.GetComponent().Play();
    }
}

AudioPlayManager

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

public class AudioPlayManager : MonoBehaviour {

    //定义一个 射线 类型
    private Ray ray;

    //定义射线储存信息
    private RaycastHit hit;


    //定义声效播放数组
    public AudioSource[] mAudioSource;

    //定义标签 每个动物子物体加上标签
    int indexTag;


    //储存点击的物体
    private GameObject go;



    void Start () {
        
    }
    
    
    void Update () {
        //执行射线的方法
        AudioSendRay();

    }


    /// 
    /// z执行射线播放音乐的方法
    /// 
     void AudioSendRay() {

        if (Input.GetMouseButtonDown(0)) //点击屏幕
        {
            //点击的位置
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            //获取射线的碰撞信息
            if (Physics.Raycast(ray,out hit))
            {
                //独立判断鱼群
                if (hit.transform.tag == "Fish")
                {
                    hit.transform.GetComponent().enabled = false;
                    mAudioSource[1].Play();
                    Debug.Log("y鱼群实现了。。。。。。。。。。。。");
                }

                //转化便签为整型
                indexTag = int.Parse(hit.transform.tag);

                // Debug.Log(indexTag+"点击事件的碰撞器的字=标签------------------------");
                Debug.Log(hit.transform.tag);

                

                //遍历所有声效
                for (int i = 0; i < mAudioSource.Length; i++)
                {
                    //判断射线的信息标签不是对应的下标并且i不等于现在下标的 就停止播放声效
                    if (mAudioSource[i].isPlaying&&i!=indexTag)
                    {
                        mAudioSource[i].Stop();
                    }
                    else
                    {
                        //否则就播放对应下标的声效
                        mAudioSource[indexTag].Play();

                        //判断获取相关脚本 是移动方法失效  

                        switch (indexTag)
                        {
                            case 0:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;
                            //case 1:
                                //go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                //获取鱼类下的所有小鱼们
                                //foreach (Transform ss in go.transform)//找到UIRoot下面的所有子物体,将他们设置为隐藏状态;
                                //{
                                //ss.gameObject.GetComponent().enabled=false;
                                //}
                                //go.GetComponent().enabled = false;
                                //break;
                            case 2:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;
                            case 3:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;
                            case 4:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;
                            case 5:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;
                            case 6:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;
                            case 7:
                                go = hit.transform.parent.gameObject.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent().enabled = false;
                                break;

                            default:
                                break;
                        }

                    }
                }


            }

        }


    }
}

Din_Auto

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

public class Din_Auto : MonoBehaviour {

    public PathNode m_currentNode=null;

    public float speed = 20.0f;
    //储存走路时间

    public Din_Auto inst;

    public Animator V_Ani;

    public bool Move_Bl = true;
    //控制恐龙是否行走

    //低头闻的声音文件
    public AudioClip Smell_Clip;



    // Use this for initialization
    void Start () {
        inst = this;
        m_currentNode = GameObject.Find("P_01").GetComponent();
        Invoke("isBarking", 3.8f); //函数的调用
        //让恐龙经过1.8秒后吃东西
        //Invoke("Moving", 4f); //函数调用
        //让恐龙经过8.8秒(吃东西动作播放完)后继续走路
        //Invoke("Attacking", 7.8f);
        Invoke("isMoving", 4.5f);
       // Invoke("Smelling", 7.8f);
        Invoke("isEating", 6.8f);
        Invoke("isMovingNext", 12.8f);

        //给动画状态机赋值
        V_Ani = gameObject.GetComponent();


        //奔跑消失的声效;开启协程
        StartCoroutine(AudioHide());

    }
    
    // Update is called once per frame
    void Update () {
        if (Move_Bl) {
            RotateTo();
            MoveTo();
        }
        
    }

    //让恐龙执行吃东西动画的函数
    public void isBarking()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("Barking");
        
    }

    public void isAttacking()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("Attacking");
    }

    public void isSmelling()
    {
        Move_Bl = false;
        V_Ani.SetTrigger("Smelling");
        gameObject.GetComponent().clip = Smell_Clip;
        //获取声音播放组件的速度为1.5倍
        gameObject.GetComponent().pitch = 1.5f;
        gameObject.GetComponent().volume = 2f;
        //播放
        gameObject.GetComponent().Play();
    }


    public void isEating() {
        
        V_Ani.SetTrigger("Eating");

        //执行协程函数
        StartCoroutine("isEat");
    }

    public void isMoving() {
        Move_Bl = true;
        V_Ani.SetTrigger("Moving");

;
    }

    public void isMovingNext()
    {

        V_Ani.SetTrigger("Moving");

        //执行协程函数
        StartCoroutine("isMove");
    }


    public void RotateTo() {
        float current = transform.eulerAngles.y;
        this.transform.LookAt(m_currentNode.transform);
        Vector3 target = this.transform.eulerAngles;
        float next = Mathf.MoveTowardsAngle(current,target.y,20*Time.deltaTime);
        this.transform.eulerAngles = new Vector3(0,next,0);
    }

    public void MoveTo() {
        Vector3 pos1 = transform.position;
        Vector3 pos2 = m_currentNode.transform.position;
        float dist = Vector2.Distance(new Vector2(pos1.x,pos1.z),new Vector2(pos2.x,pos2.z));
        if (dist<2f) {  //数值越大,消失时间越长 具体测试才知道
            if (m_currentNode.P_Next == null)
            {
                Destroy(this.gameObject);
            }
            else {
                m_currentNode = m_currentNode.P_Next;
            }
            
        }
        transform.Translate(new Vector3(0, 0, speed * Time.deltaTime));
        Destroy(gameObject, 39f);
    }



    //延迟函数 协程
    IEnumerator isEat() {
        yield return new WaitForSeconds(1.5f);
        Move_Bl = false;
    }

    IEnumerator isMove()
    {
        yield return new WaitForSeconds(1.5f);
        Move_Bl = true;
    }

    //声音消失的时间
    IEnumerator AudioHide()
    {
        yield return new WaitForSeconds(15f);
        gameObject.transform.GetComponent().enabled = false;
    }
}

Ins_Din

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

public class Ins_Din : MonoBehaviour {

    //储存预制体
    //public GameObject Din_Pre;

    //储存预制体的数组
    public GameObject[]  Amimal_Pre;


    //计算时间
    private int Frame_Count;

    //实例化的物体
    GameObject go;

    //创建集合 把实例化动物存贮起来
  public  List AnimalList = new List();

    // Use this for initialization
    void Start() {

    }

    // Update is called once per frame
    void Update() {

    }

    void FixedUpdate() {
        Frame_Count++;

        if (Frame_Count>500) {  //控制恐龙的生成的时间 越大时间越长
            Ins();
            Frame_Count = 0;
        }
    }

    /// 
    /// 实例化 恐龙的方法
    /// 
    void Ins() {
        //随机下标
        int index = Random.Range(0,Amimal_Pre.Length);
       //实例化
       go=  Instantiate(Amimal_Pre[index], gameObject.transform.position, gameObject.transform.rotation) as GameObject;
        if (go.transform.tag=="Din")
        {
            go.transform.eulerAngles = new Vector3(0,-90,0);
            go.transform.position = new Vector3(-30,0,6);
            Destroy(go, 25.5f);
        }
        if (go.transform.tag == "BigDin")
        {
            go.transform.eulerAngles = new Vector3(0, -90, 0);
            go.transform.position = new Vector3(-30, 0, -6);
            Destroy(go, 41f);
        }
        //if (go.transform.tag == "horse")
        //{
        //    go.transform.eulerAngles = new Vector3(0, -90, 0);
        //    go.transform.position = new Vector3(-30, 0, -3);
        //}
        //Instantiate(Din_Pre, gameObject.transform.position, gameObject.transform.rotation);

        //控制鱼的高度
        //if (go.transform.name=="Fish")
        //{
        //    go.transform.position = new Vector3(0,0,0);
        //}

        //15秒后 销毁物体
        if (go.transform.tag=="Fish")
        {
            go.transform.eulerAngles = new Vector3(0, 0, 0);
            Destroy(go,45f);
        }

        if (go.transform.tag == "Tiger")
        {
            //go.transform.eulerAngles = new Vector3(0, 0, 0);
            Destroy(go, 40f);
        }

    }

    //物体出现在屏幕    //编辑器看不到效果
    void OnBecameVisible()
    {
        Debug.Log(this.name.ToString() + "这个物体出现在屏幕里面了");
        
    }

    //物体离开屏幕  
    void OnBecameInvisible()
    {
       
        Debug.Log(this.name.ToString() + "这个物体离开屏幕里面了");
        //物体离开屏幕后备销毁
        if (go!=null)
        {
            Destroy(go);
        }
       
    }



    //隐藏所有物体
    public void HideAnimals() {

        //把物体添加到集合中
        GameObject[] goes= GameObject.FindGameObjectsWithTag("Fish");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("BigDin");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("Din");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("horse");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("OtherDin");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("Tiger");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("Ele");
        AnimalList.Add(goes);
        goes = GameObject.FindGameObjectsWithTag("fly");
        AnimalList.Add(goes);
        //for (int i = 0; i < AnimalList.Count; i++)
        //{
        //    //Debug.Log(AnimalList[i].name + "集合---------------------------");
        //}
        for (int i = 0; i < AnimalList.Count; i++)
        {
            if (AnimalList[i]!=null)
            {
                for (int j = 0; j < AnimalList[i].Length; j++)
                {
                    //Debug.Log(AnimalList[i][j].name + "集合---------------------------");
                    if (AnimalList[i][j]!=null)
                    {
                        //if (AnimalList[i][j].name== "tiger_idle")
                        //{
                        //    AnimalList[i][j].transform.position = new Vector3(-25,0,0);
                        //    AnimalList[i][j].GetComponent().Move_Bl = true;
                        //    //AnimalList[i][j].transform.rotation=new 
                        //}
                        AnimalList[i][j].SetActive(false);
                        Debug.Log("------------隐藏" + j);
                    }
                 
                }
       
                //销毁物体
                //Destroy(AnimalList[i]);
              
            }
        }
    }

}

PathNode

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

/// 
/// 设置行走路径的脚本 位置点
/// 
public class PathNode : MonoBehaviour {

    //父类位置点
    public PathNode P_Paretn;

    //下一个 位置点
    public PathNode P_Next;

    //判断位置点 如果下个节点不为空 设置父类位置为空 设置相关 父类和子类的位置点信息
    public void SetNext(PathNode node) {
        if (P_Next != null) {
            P_Next.P_Paretn = null;
            P_Next = node;
            node.P_Paretn = this;
        }
    }

    //特定文件夹显示位置点的图片格式 在Giamos文件夹寻找指定图片文件
    void OnDrawGizmos() {
        Gizmos.DrawIcon(this.transform.position,"Point.png");
    }

}

三、效果展示

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