- Home /
found a solution
Changing Video Cam Texture Material With Buttons
I have this script that allows video to be played on a plane, I was trying to figure out how to change the material on the plane the web cam video plays on because the effect is cool, but I'm not having a lot of luck. This us the Video player script I'm using:
using UnityEngine; // 41 Post - Created by DimasTheDriver on July/22/2012 . Part of the 'Unity: capturing the video input from multiple webcams' post. Available at: http://www.41post.com/?p=4854.
using System.Collections;
public class MultipleWebCamPreview: MonoBehaviour
{
//The texture that holds the video captured by the webcam
private WebCamTexture webCamTexture;
//An array that stores a reference to the names of all connected webcams
private string[] nameWebCams;
//The current webcam
private int currentCam = 0;
//The selected webcam
private int selectedCam = 0;
void Start()
{
//An integer that stores the number of connected webcams
int numOfCams = WebCamTexture.devices.Length;
//Initialize the nameWebCams array to hold the same number of strings as there are webcams
this.nameWebCams = new string[numOfCams];
//Get the name of each connected camera and store it into the 'nameWebCams' array
for(int i = 0; i<numOfCams; i++)
{
this.nameWebCams[i] = WebCamTexture.devices[i].name;
}
//Initialize the webCamTexture
webCamTexture = new WebCamTexture();
//Assign the images captured by the first available webcam as the texture of the containing game object
renderer.material.mainTexture = webCamTexture;
//Start streaming the images captured by the webcam into the texture
webCamTexture.Play();
}
void OnGUI()
{
//Render the SelectionGrid listing all the cameras and save the selected one at 'selectedCam'
selectedCam = GUI.SelectionGrid(new Rect(20,20,200,50), currentCam, nameWebCams, 1);
//If the selected camera isn't the current camera
if(selectedCam != currentCam)
{
//Assign the value of currentCam to selectCam
currentCam = selectedCam;
//Stop the streaming of captured images
webCamTexture.Stop();
//Assign a different webcam to the webCamTexture
webCamTexture.deviceName = WebCamTexture.devices[currentCam].name;
//Start streaming the captured images from this webcam to the texture
webCamTexture.Play();
}
}
}
and this is the script I came up with to change the material on the plane using buttons but as I say I can get the material to change but it does not do the same thing as when I play the scene and drag a material into the plane's material slot, only then do I get the effect I want????
using UnityEngine;
using System.Collections;
public class VideoScreenManager : MonoBehaviour {
//Material Reference
public Material newMaterialRef01;
public Material newMaterialRef02;
public Material newMaterialRef03;
public Material newMaterialRef04;
public Material newMaterialRef05;
public Material newMaterialRef06; //Default White Material
public void GoColor01(){
renderer.material = newMaterialRef01;
}
public void GoColor02(){
renderer.material = newMaterialRef02;
}
public void GoColor03(){
renderer.material = newMaterialRef03;
}
public void GoColor04(){
renderer.material = newMaterialRef04;
}
public void GoColor05(){
renderer.material = newMaterialRef05;
}
public void GoColor06(){
renderer.material = newMaterialRef06;
}
}
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Material changes when i drag and drop it on object but doesnt through code? 1 Answer
WebGL VideoTexture not working 0 Answers