x


Create a switch that turns ON/OFF video streams. (in C#)

Hello i have this script.

using UnityEngine;
using System.Collections;

public class WebCam : MonoBehaviour
{

    public string url = "";

    void Start()
    {
        renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
        StartCoroutine(UpdateCam());
    }

    IEnumerator UpdateCam()
    {
        while (true)
        {
            Debug.Log("reloading webcam");
            WWW www = new WWW(url);
            yield return www;
            www.LoadImageIntoTexture((Texture2D)renderer.material.mainTexture);
        }
    }
}

I want to create a GUI switch that turns this stream on and off. The web cam material is streamed on the plane. My first solution was to just disable the plane render. But that still leaves the stream open and it takes resource that can be used somewhere else.

Then i edited the code so that it had a bool value on or off. If the value was false then it just skipped the while loop in my code. It worked only half way. Because my Update cam function is called on Start() it only initializes the true or false value and thats it. I turn my GUI switch off but nothing happens because the default value was already initialized in Start()

So i took my Update cam function and put it into Update() method. It worked perfectly. I could switch the stream on and off but since the update was called on every frame my application nearly freezed.

Since i am new to programming can anyone give me the pointers how to call that UpdateCam() function so that it only checks it once in a second. I know that there is Time.DeltaTime supposed to be in there but all this syntax thing is very new for me. Of course if you have even better solution then please tell me.

Thank you for your time.

more ▼

asked Apr 15 '10 at 07:40 PM

gotmilk gravatar image

gotmilk
45 3 4 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

i shall answer my own question. I came up with this and it works.

using UnityEngine;

using System.Collections;

public class streamScreen : MonoBehaviour { #region Fields //streamScreen myComponent; // public bool cameraOnOff = true; public string url = ""; float cameraUpdate = 0.1f; bool onOff = false;

#endregion

#region Properties

#endregion

#region Functions


void Start()
{
    renderer.enabled = true;
  //  streamScreen myComponent = (streamScreen)GetComponent("cameraStream");
  //  myComponent = (streamScreen)GameObject.Find("streamScreen").GetComponent("cameraStream");



 //   myComponent.enabled = false;

    renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);
    StartCoroutine(UpdateCam());

}



void Update()
{
    if (onOff == false)
    {
        cameraUpdate = 0.1f;
    }
    else
    {
        cameraUpdate = 5f; // put very big number here to wait for millions of seconds to make a stream
    }


}


 IEnumerator UpdateCam()    
{
   // while (cameraOnOff == true)
    while (true)
    {
        Debug.Log("reloading webcam");
        WWW www = new WWW(url);
        yield return www;
        www.LoadImageIntoTexture((Texture2D)renderer.material.mainTexture);
        yield return new WaitForSeconds(cameraUpdate);

    }

}

public void switchCamera()
{
//    renderer.enabled = true;


    onOff = !onOff;
    Debug.Log("You released the RIGHT button");
}

#endregion

}

Thank you for your time. :)

more ▼

answered Apr 15 '10 at 08:42 PM

gotmilk gravatar image

gotmilk
45 3 4 8

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x570
x280
x223
x39

asked: Apr 15 '10 at 07:40 PM

Seen: 1601 times

Last Updated: Apr 15 '10 at 07:40 PM