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.