Skip to content

Commit 3b71534

Browse files
authored
Merge pull request #250 from De-Panther/fix_webcam_errors_vers
Fixed webcam errors in different versions of Unity
2 parents 84b0260 + 77e1af4 commit 3b71534

File tree

9 files changed

+467
-14
lines changed

9 files changed

+467
-14
lines changed

Build/Build/Build.data

543 Bytes
Binary file not shown.

Build/Build/Build.framework.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Build/Build/Build.wasm

911 Bytes
Binary file not shown.

DebugProjects/Unity2019.4/Packages/packages-lock.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"depth": 0,
3737
"source": "embedded",
3838
"dependencies": {
39-
"com.de-panther.webxr": "0.15.0-preview"
39+
"com.de-panther.webxr": "0.16.0-preview"
4040
}
4141
},
4242
"com.unity.burst": {

Packages/webxr-interactions/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Fixed
10+
- Webcam errors in different versions of Unity.
11+
912
## [0.16.1] - 2023-02-03
1013
### Fixed
1114
- No reference to controllers in SceneHitTest.

Packages/webxr-interactions/Runtime/Plugins/WebGL/FixWebCamWebGL.jslib

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ var LibraryFixWebCamWebGL = {
5151
webcamDevice.nextFrameAvailableTime = timeNow + webcamDevice.frameLengthInMsecs
5252
}
5353
videoElement = webcamDevice.video;
54-
}
55-
if (typeof MediaDevices !== "undefined") {
54+
} else if (!typeof MediaDevices !== "undefined") {
5655
if (!MediaDevices[deviceId].video) {
5756
console.error("WebCam not initialized.");
5857
return;

Packages/webxr-interactions/Runtime/Scripts/PlayWebcam.cs

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public class PlayWebcam : MonoBehaviour
3535
private IntPtr latestWebcamPtr = IntPtr.Zero;
3636
private int lightingTexID = -1;
3737

38+
private int webcamIndex = 0;
39+
3840
void Awake()
3941
{
4042
TrySetupRenderer();
@@ -80,16 +82,20 @@ void Start()
8082

8183
void Play()
8284
{
85+
var devices = WebCamTexture.devices;
86+
if (devices == null || devices.Length == 0)
87+
{
88+
return;
89+
}
8390
if (webcamTexture == null)
8491
{
85-
var devices = WebCamTexture.devices;
86-
if (devices == null || devices.Length == 0)
87-
{
88-
return;
89-
}
90-
webcamTexture = new WebCamTexture(devices[0].name);
92+
webcamTexture = new WebCamTexture(devices[Mathf.Clamp(webcamIndex, 0, devices.Length-1)].name);
9193
material.mainTexture = webcamTexture;
9294
}
95+
else
96+
{
97+
webcamTexture.deviceName = devices[Mathf.Clamp(webcamIndex, 0, devices.Length - 1)].name;
98+
}
9399
latestWebcamPtr = webcamTexture.GetNativeTexturePtr();
94100
#if UNITY_WEBGL && !UNITY_EDITOR
95101
JS_WebCamVideo_RemoveWhereTextureId(latestWebcamPtr);
@@ -98,6 +104,16 @@ void Play()
98104
StartCoroutine(SetScale());
99105
}
100106

107+
void Stop()
108+
{
109+
if (webcamTexture == null)
110+
{
111+
return;
112+
}
113+
StopAllCoroutines();
114+
webcamTexture.Stop();
115+
}
116+
101117
IEnumerator SetScale()
102118
{
103119
while (webcamTexture.GetNativeTexturePtr() == latestWebcamPtr)
@@ -121,8 +137,7 @@ void OnEnable()
121137

122138
void OnDisable()
123139
{
124-
StopAllCoroutines();
125-
webcamTexture.Stop();
140+
Stop();
126141
}
127142

128143
private void TrySetColor(string value, ref Color color, int propertyID, int colorLetter)
@@ -211,4 +226,14 @@ public void TrySetLightingTexture(Texture texture)
211226
}
212227
material.SetTexture(lightingTexID, texture);
213228
}
229+
230+
public void TrySetWebcamIndex(string value)
231+
{
232+
if (int.TryParse(value, out webcamIndex)
233+
&& isActiveAndEnabled && started)
234+
{
235+
Stop();
236+
Play();
237+
}
238+
}
214239
}

Packages/webxr-interactions/Runtime/Scripts/SceneHitTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ void Start()
2727
{
2828
if (controllers[i].hand == WebXRControllerHand.LEFT)
2929
{
30-
leftController ??= controllers[i];
30+
leftController = leftController ?? controllers[i];
3131
}
3232
else if (controllers[i].hand == WebXRControllerHand.RIGHT)
3333
{
34-
rightController ??= controllers[i];
34+
rightController = leftController ?? controllers[i];
3535
}
3636
if (leftController != null && rightController != null)
3737
{

0 commit comments

Comments
 (0)