topical media & game development

talk show tell print

lib-unity-presentation-Assets-Scripts-MovieBillboard.cs / cs



  using UnityEngine;
  using System.Collections;
  
  public class MovieBillboard : MonoBehaviour {
  
      private MovieTexture slide;
      Renderer ren;
      Transform player;
  
      public string fileName = "BombFactory.ogg";
      bool downloaded = false;
  
          void Awake () {
          player = GameObject.FindGameObjectWithTag("Player").transform;
          ren = transform.GetComponentInChildren<Renderer>();
          
          this.useGUILayout = false;
  
          InvokeRepeating("CheckPlayerDistance", 5, 5);
          StartCoroutine(StreamMovie());
          }
  
      IEnumerator StreamMovie(){
          /*string extra = "Movies/";
          if (Application.isWebPlayer)
          {
              extra = Application.dataPath + "/Movies/";
          }
  
  if UNITY_EDITOR
          extra = "Movies/";
  endif*/
          
  
          string url = "file://"+fileName;
          if (Application.isWebPlayer)
          {
              url = Application.dataPath+"/"+  fileName;
          }
          Debug.Log("About to stream: "+url);
          WWW www = new WWW(url);
          while (!www.isDone)
          {
              yield return 0;
          }
          if (www.error!=null)
          {
              Debug.LogError("Movie DL error:"+www.error);
              yield break;
          }
          Debug.Log("Veder "+url+" "+www.error);
          MovieTexture movieTexture = www.movie;
          while (!movieTexture.isReadyToPlay)
          {
              yield return 0;
  
          }
  
          ren.materials[1].mainTexture = slide = movieTexture;
          slide.loop = true;
          downloaded = true;
          Debug.Log("Downloaded " + url);
      }
  
      void OnGUI()
      {
          if (!downloaded) return;
          if (visible)
          {
              if (Time.frameCount <= 5) displayPercent = 1;
              displayPercent += Time.deltaTime * 3;
          }
          else if (displayPercent > 0)
          {
              displayPercent -= Time.deltaTime * 3;                  
          }
  
          if (displayPercent > 0)
          {
              displayPercent = Mathf.Clamp01(displayPercent);
              float w = Screen.width * displayPercent;
              float h = Screen.height * displayPercent;
              Rect rect = new Rect((Screen.width / 2 - w / 2), (Screen.height / 2 - h / 2), w, h);
              GUI.DrawTexture(rect, slide, ScaleMode.ScaleToFit);
          }
      }
  
      private bool visible = false;
      private float displayPercent = 0;
  
      void OnTriggerEnter (Collider other ) {
          if (downloaded && other.transform.tag == "Player")
          {
              //slide.audioClip.
              displayPercent = 0;
              visible = true;
          }
      }
      void OnTriggerExit(Collider other)
      {
          if (downloaded && other.transform.tag == "Player")
          {
           //   slide.Pause();
              visible = false;
          }
      }
  
      void CheckPlayerDistance()
      {
          if (!downloaded) return;
          if (Vector3.Distance(transform.position, player.position) <= 20)
          {
              slide.Play();
              slide.loop = true;
          }
          else
          {
              slide.Stop();
          }
      }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.