Webcam on Multiple Objects
Hi I'm a design student working on a project for university where I want to be able to use a webcam as a material across multiple objects. I've found various suggestions of how to do this across different forums, and have got a webcam on a single object up and running, but can't get it onto multiple objects. I'm new to c# and have started to learn it but still know pretty much nothing. My project deadline is looming and I'd really appreciate if anyone that's feeling generous might be able to help me out. Thanks a lot!
Answer by zackwxyz · Apr 10, 2017 at 12:32 PM
No worries, solved it myself :) If anyone else without much code experience like me wants to do this, use the below c# script and create a folder called Resources, and inside that, a material called webCamTex
Then drag the script and the new material onto one of your objects, and drag the webCamTex material onto all other objects you want the webcam to show up on.
big thanks to @Spyrai on this answer for the majority of the solution: http://answers.unity3d.com/questions/1123651/use-the-same-webcamtexture-on-multiple-objects.html
using UnityEngine;
public class WebCamScript : MonoBehaviour
{
private WebCamTexture _webcamTexture;
private Renderer _renderer;
// Assign the Material you are using for the web cam feed
[SerializeField] private Material webCamTex;
void Start()
{
webCamTex = Resources.Load("webCamTex", typeof(Material)) as Material;
// Grabbing all web cam devices
WebCamDevice[] devices = WebCamTexture.devices;
// I just use the first one, use which ever one you need
string camName = devices[0].name;
// set the Texture from the cam feed
WebCamTexture camFeed = new WebCamTexture(camName);
// Assign the materials texture to the WebCamTexture you made,
// this applies it to all objects using this Material
webCamTex.mainTexture = camFeed;
// Then start the texture
camFeed.Play();
gameObject.GetComponent<Renderer>().material = webCamTex;
}
}
Answer by Mattress · Dec 05, 2017 at 05:25 PM
This solved my problem, thank you! I initially put the script and material on every object by mistake. Must remember to have one script in the scene and then attach the material to everything else! Thanks again!
Your answer
Follow this Question
Related Questions
4K - WebCameTexture.Play() Could not connect pins - RenderStream() 0 Answers
webcam Texture Wrong Camera Devices Length 0 Answers
Cannot request Webcam Width ,Height and start camera because of too many call? I guess. 0 Answers
How do play a video on top of a webcam texture on mobile? 0 Answers
Set webcam image format to MJPG 0 Answers