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();
    }

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

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

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

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

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

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

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

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

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

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

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

    /// <summary>
    /// 向右旋转
    /// </summary>
    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<PathNode>();
        //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<Animator>();


        //奔跑消失的声效;开启协程
        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<AudioSource>().clip = Smell_Clip;
        //获取声音播放组件的速度为1.5倍
        gameObject.GetComponent<AudioSource>().pitch = 1.5f;
        gameObject.GetComponent<AudioSource>().volume = 2f;
        //播放
        gameObject.GetComponent<AudioSource>().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<AudioSource>().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<PathNode>();
        //给动画状态机赋值
        //V_Ani = gameObject.GetComponent<Animator>();
        //播放走路的声音
        //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<AudioSource>().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<PathNode>();

        //播放走路的声音
        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<Animation>().Play("Allosaurus_IdleBellow");
        //transform.animation.Play("Allosaurus_IdleBellow");
        //gameObject.GetComponent<AudioSource>().clip = Smell_Clip;
        //获取声音播放组件的速度为1.5倍
        //gameObject.GetComponent<AudioSource>().pitch = 1.5f;
        //gameObject.GetComponent<AudioSource>().volume = 2f;
        //播放
        //gameObject.GetComponent<AudioSource>().Play();
        m_AudioSource[0].Stop();
        m_AudioSource[1].Play();
    }


    public void isAllosaurus_Walk() {

        Move_Bl = true;
        gameObject.transform.GetComponent<Animation>().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<AudioSource>().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<PathNode>();

        //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<AudioSource>().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<PathNode>();
        //给动画状态机赋值
        V_Ani = gameObject.GetComponent<Animator>();
        //播放走路的声音
        //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<AudioSource>().enabled = false;
    }

}

Animal_Auto_Horse

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

/// <summary>
/// 马行走的函数
/// </summary>
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<PathNode>();
 
        //Invoke("isEating", 6.8f);
        //Invoke("isMovingNext", 12.8f);

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


        //播放行走的声音
        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<AudioSource>().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<PathNode>();
        //给动画状态机赋值
        V_Ani = gameObject.GetComponent<Animator>();

        //播放老虎的声音
        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<AudioSource>().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<AudioSource>().clip = Bark_Clip;
        gameObject.GetComponent<AudioSource>().pitch = 2f;
        gameObject.GetComponent<AudioSource>().volume = 0.1f;
        gameObject.GetComponent<AudioSource>().Play();
    }

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

    }
    /// <summary>
    /// 恐龙走路的事件函数
    /// </summary>
    public void Au_Walk() {
        //获取声音片段 循环 速度 播放;
        gameObject.GetComponent<AudioSource>().clip = Walk_Clip;
        //gameObject.GetComponent<AudioSource>().loop = true;
        gameObject.GetComponent<AudioSource>().pitch = 1f;
        gameObject.GetComponent<AudioSource>().volume = 1f;
        gameObject.GetComponent<AudioSource>().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();

    }


    /// <summary>
    /// z执行射线播放音乐的方法
    /// </summary>
     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<Animal_Auto>().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<Din_Auto>().enabled = false;
                                break;
                            //case 1:
                                //go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                //获取鱼类下的所有小鱼们
                                //foreach (Transform ss in go.transform)//找到UIRoot下面的所有子物体,将他们设置为隐藏状态;
                                //{
                                //ss.gameObject.GetComponent<SardineCharacter>().enabled=false;
                                //}
                                //go.GetComponent<Animal_Auto>().enabled = false;
                                //break;
                            case 2:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent<Animal_Auto_Horse>().enabled = false;
                                break;
                            case 3:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent<Animal_Auto_Tiger>().enabled = false;
                                break;
                            case 4:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent<Animal_Auto_Din_G>().enabled = false;
                                break;
                            case 5:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent<Animal_Auto_Din>().enabled = false;
                                break;
                            case 6:
                                go = hit.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent<Animal_Auto_ElePhant>().enabled = false;
                                break;
                            case 7:
                                go = hit.transform.parent.gameObject.transform.parent.gameObject;
                                //Debug.Log(go.name+"这是标签的名字····。。。。。。。。");
                                go.GetComponent<Animal_Auto_Butterfly>().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<PathNode>();
        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<Animator>();


        //奔跑消失的声效;开启协程
        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<AudioSource>().clip = Smell_Clip;
        //获取声音播放组件的速度为1.5倍
        gameObject.GetComponent<AudioSource>().pitch = 1.5f;
        gameObject.GetComponent<AudioSource>().volume = 2f;
        //播放
        gameObject.GetComponent<AudioSource>().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<AudioSource>().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<GameObject[]> AnimalList = new List<GameObject[]>();

    // 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;
        }
    }

    /// <summary>
    /// 实例化 恐龙的方法
    /// </summary>
    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<Animal_Auto_Tiger>().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;

/// <summary>
/// 设置行走路径的脚本 位置点
/// </summary>
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");
    }

}

三、效果展示

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容