Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AndyUr · May 28, 2018 at 10:18 PM · webcamtextureswitch scenes

WebcamTexture not correctly disabled on scene changes

When making scene transitions my WebcamTexture doesn't get properly destroyed. During the next scene, creating a new WebcamTexture incorrectly initializes the device parameters and never updates frames. Also after Stopping playmode, my camera stays on (with the light on), and the only way to free it is to close the unity editor.

This is a minimum example, where I can reproduce this issue. Just use this component on a scene object and press enter to switch scenes.

 using System.Collections;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class CamTest : MonoBehaviour {
 
     WebCamTexture liveFeed;
 
     IEnumerator Start ()
     {
         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
         if (Application.HasUserAuthorization(UserAuthorization.WebCam))
         {
             liveFeed = new WebCamTexture();
             liveFeed.Play();
         }
         else
             Debug.LogWarning("Camera access not obtained!");
 
 
     }
     void OnDisable ()
     {
         if (liveFeed && liveFeed.isPlaying)
             liveFeed.Stop();
     }
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Return))
         {
             SceneManager.LoadScene(gameObject.scene.name);
         }
     }
 }




Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by AndyUr · May 28, 2018 at 11:07 PM

Seems like I found a working solution. Using a static WebcamTexture variable keeps the camera correctly initialized during scene transitions Here's the fixed code (plus some setup to show the feed on a RawImage).

 using System.Collections;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class CamTest : MonoBehaviour {
 
     static WebCamTexture liveFeed;
     public RawImage feed;
 
     IEnumerator Start ()
     {
         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
 
         if (Application.HasUserAuthorization(UserAuthorization.WebCam))
         {
             if(!liveFeed)
                 liveFeed = new WebCamTexture();
             liveFeed.Play();
 
             feed.texture = new Texture2D(liveFeed.width, liveFeed.height, TextureFormat.RGBA32, false);
         }
         else
             Debug.LogWarning("Camera access not obtained!");
 
 
     }
     void OnDisable ()
     {
         if (liveFeed && liveFeed.isPlaying)
             liveFeed.Stop();
     }
 
     void Update()
     {
         if(liveFeed && liveFeed.didUpdateThisFrame)
         {
             Color32[] buffer = liveFeed.GetPixels32();
             (feed.texture as Texture2D).SetPixels32(buffer);
             (feed.texture as Texture2D).Apply();
             Debug.Log("ding");
         }
 
         if(Input.GetKeyDown(KeyCode.Return))
         {
             SceneManager.LoadScene(gameObject.scene.name);
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

83 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Script for changing levels 3 Answers

how to use WebcamTexture working well on mobile device 0 Answers

2013 Face tracking plugin 1 Answer

Projector with WebCamTexture as texture 0 Answers

Light Data from WebCam? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges