// 功能:
// 掛載對象:
//注意:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class wwwLocalScript : MonoBehaviour
{
// 外部創(chuàng)建一個Image;
public Image m_image;
// 外部的一個sphere
public GameObject m_sphere;
//外部的展示視頻的Rowimage
public RawImage m_rowimage;
//視頻紋理
MovieTexture m_MoveTexture;
// 視頻中的聲音
public AudioSource m_MoveAudio;
///
/// www加載本地圖片
/// 1, 給個路徑
/// 2,創(chuàng)建一個www 的實例 (new 一個)
/// 3,
///
void Start ()
{
//加載桌面圖片
string imagedesktop = "file:/Users/lanou3g/Desktop/My123.jpg";
//string imageFilePath = "file://" + Application.dataPath + "/Resources/MyPicture.png";
// 加載圖片資源的實例
//WWW imageWWW = new WWW (imageFilePath);
WWW imageWWW = new WWW (imagedesktop);
while (imageWWW.isDone == false) {
print ("加載中..." + imageWWW.progress);
}
//如果是往image組件或者Cube上添加圖片 ? 根據加載的材質轉換為sprite
Texture2D m_texture = imageWWW.texture;
// 使用代碼將texture 轉換為sprite
Sprite sprite = Sprite.Create (m_texture, new Rect (0, 0, m_texture.width,
m_texture.height), new Vector2 (0.5f, 0.5f)); ?//0.5 ?像素點
m_image.sprite = sprite;// 給圖片賦值
// 將加載的texture 轉換為sphere 的主材質
m_sphere.GetComponent ().material.mainTexture = imageWWW.texture;
// 不使用WWW類加載本地視頻
//StartCoroutine (LoadLocalMove (null));
//加載本地視頻
string moveFilePath = "file://" + Application.dataPath + "/Resources/BearBird.ogg";
//使用WWW類加載本地視頻
StartCoroutine (LoadLocalMove (moveFilePath));
}
// 使用WWW類加載本地視頻
IEnumerator LoadLocalMove (string url)
{
WWW www = new WWW (url);
// 防止沒有加載完成
while (www.isDone == false) {
// 打印進度條
print (www.progress);
// 等待下一幀 再從這里開始
yield return null;
}
// 只有上面的yiled return null 不再繼續(xù)(www.isDone != false) 下面的代碼才會執(zhí)行
//交由RowImage展示
m_MoveTexture = www.movie;
// 準備好后交由RowImage展示
m_rowimage.texture = m_MoveTexture;
// 拿到視屏中的音頻并播放
m_MoveAudio.clip = m_MoveTexture.audioClip;
m_MoveTexture.loop = true;// 設置視頻循環(huán)播放
m_MoveAudio.loop = true;// 設置音頻循環(huán)播放
m_MoveTexture.Play ();//播放視頻
m_MoveAudio.Play ();// 播放音頻
}
//加載本地視頻 不使用WWW加載本地視頻
//IEnumerator LoadLocalMove (string url)
//{
//// 不使用WWW加載本地視頻
//while (m_MoveTexture == null) {
//m_MoveTexture = Resources.Load ("BearBird")as MovieTexture;
//Debug.Log ("視頻文件找到 開始播放...");
//yield return null;
//}
//// 放在RowImage 中去播放視頻
//m_rowimage.texture = m_MoveTexture;
//// 拿到視屏中的音頻并播放
//m_MoveAudio.clip = m_MoveTexture.audioClip;
//
//m_MoveTexture.loop = true;// 設置視頻循環(huán)播放
//m_MoveAudio.loop = true;// 設置音頻循環(huán)播放
//
//m_MoveTexture.Play ();//播放視頻
//m_MoveAudio.Play ();// 播放音頻
//}
}