Getting Started Tutorial] [Unity3D Unity3D to play audio and video

Foreword

In the process of game development, we often used to play audio and video, today we bring to you an easy way to play audio and video using the built-in function of Unity. This reference to the content of "Unity3D game development," a book written by Xuanyu Song, grateful.

 

1 play audio

First of all, Unity supported audio formats wav, mp3, ogg and so on. GameObject create an empty object in the scene, and add AudioSource components. AudioClip under a section of mp3 music folders onto the project's assets and binds to the music file you just created a space object of AudioSource components on.

Write the following script PlayAudio.cs:

using UnityEngine;
using System.Collections;

public class PlayAudio : MonoBehaviour {

    public AudioSource audio;

    public float volume;
	void Start () {
        volume = 0.2f;
	}
	
	// Update is called once per frame
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 200, 100), "Play"))
        {
            if (!audio.isPlaying)
            {
                audio.Play();
            }
        }
        if (GUI.Button(new Rect(10, 120, 200, 100), "Close"))
        {
            if (audio.isPlaying)
            {
                audio.Stop();
            }
        }
        if (GUI.Button(new Rect(10, 230, 200, 100), "Pause"))
        {
            if (audio.isPlaying)
            {
                audio.Pause();
            }
        }
        volume = GUI.HorizontalSlider(new Rect(250, 50, 200, 50), volume, 0, 1);
        GUI.Label(new Rect(250, 70, 300, 20), "Volume:" + (int)(volume * 100) + "%");

        if (audio.isPlaying)
        {
            audio.volume = volume;
        }       
    }
}


The PlayAudio.cs bound to MainCamera, then just created GameObject drag it to the Audio variable PlayAudio script.

Run the program, the following results:



2 Play Video

Unity Supported video formats include mov, mpg, mpeg, mp4, avi, asf. But sometimes, when the video will be dragged into the Unity will be problems. Therefore, we recommend the video format conversion. Recommended tools: Theora Converter .NET, address https://pan.baidu.com/s/1miQ8NNU . Download and install the converted file to be played is ogv format that can be directly identified Unity.

The transformed ogv format video file onto the Project's Assets folder. Create a Plane in the scene, and binds the following script to PlayVideo.cs on Plane. And will be broadcast video onto myMovieTexture variable on the location of the script.

using UnityEngine;
using System.Collections;

public class PlayVideo : MonoBehaviour {

    public MovieTexture myMovieTexture;
	void Start () {
        this.GetComponent<Renderer>().material.mainTexture = myMovieTexture;
        myMovieTexture.loop = true;
	}
	
	// Update is called once per frame
    void OnGUI()
    {
        if (GUI.Button(new Rect(10, 10, 200, 100), "PlayMovie/ContinueMovie"))
        {
            if (!myMovieTexture.isPlaying)
            {
                myMovieTexture.Play();
            }
        }
        if (GUI.Button(new Rect(10, 110, 200, 100), "PauseMovie"))
        {
            if (!myMovieTexture.isPlaying)
            {
                myMovieTexture.Pause();
            }
        }
        if (GUI.Button(new Rect(10, 210, 200, 100), "CloseMovie"))
        {
            if (!myMovieTexture.isPlaying)
            {
                myMovieTexture.Stop();
            }
        }
    }
}


running result:



发布了65 篇原创文章 · 获赞 265 · 访问量 55万+

Guess you like

Origin blog.csdn.net/zzlyw/article/details/54292856