Skip to content

Commit

Permalink
feat: simplifies photo capture element scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxAndreassenRPM committed Nov 23, 2023
1 parent 1b428a4 commit face150
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 96 deletions.
File renamed without changes.
21 changes: 21 additions & 0 deletions Runtime/AvatarCreator/Elements/PhotoCaptureConfirmElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using UnityEngine;
using UnityEngine.Events;

public class PhotoCaptureConfirmElement : MonoBehaviour
{
[Space(5)]
[Header("Events")]
public UnityEvent<Texture2D> onPhotoCaptureConfirmed;

private Texture2D selectedTexture;

public void SetTexture(Texture2D texture)
{
selectedTexture = texture;
}

public void ConfirmPhoto()
{
onPhotoCaptureConfirmed?.Invoke(selectedTexture);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@

public class PhotoCaptureElement : MonoBehaviour
{
[Header("Photo Capture Buttons")]
[SerializeField, Tooltip("Button taking the photo")] public Button takePhotoButton;

[Header("Photo Capture Settings")]
[Header("Settings")]
[SerializeField] private RawImage cameraTextureTarget;

[Space(5)]
[Header("Photo Capture Events")]
[Header("Events")]
public UnityEvent<Texture2D> onPhotoCaptured;

private WebCamTexture cameraTexture;

private void Awake()
{
InitializeCamera();
takePhotoButton.onClick.AddListener(TakePhoto);
}

private void OnDestroy()
{
takePhotoButton.onClick.RemoveListener(TakePhoto);
}

public void StartCamera()
Expand All @@ -43,6 +34,20 @@ public void StopCamera()
cameraTexture.Stop();
}
}

public void TakePhoto()
{
if (cameraTexture == null || !cameraTexture.isPlaying)
return;

var texture = new Texture2D(cameraTextureTarget.texture.width, cameraTextureTarget.texture.height, TextureFormat.ARGB32, false);
texture.SetPixels(cameraTexture.GetPixels());
texture.Apply();

StopCamera();

onPhotoCaptured?.Invoke(texture);
}

private void InitializeCamera()
{
Expand All @@ -60,20 +65,6 @@ private void SetupPhotoBoothTexture(string textureName)
cameraTextureTarget.texture = cameraTexture;
}

private void TakePhoto()
{
if (cameraTexture == null || !cameraTexture.isPlaying)
return;

var texture = new Texture2D(cameraTextureTarget.texture.width, cameraTextureTarget.texture.height, TextureFormat.ARGB32, false);
texture.SetPixels(cameraTexture.GetPixels());
texture.Apply();

StopCamera();

onPhotoCaptured?.Invoke(texture);
}

private static WebCamDevice? GetWebCamDevice()
{
var devices = WebCamTexture.devices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,19 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 478533099485849275}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 2661869294509717807}
m_TargetAssemblyTypeName: PhotoCaptureElement, ReadyPlayerMe.Core.AvatarCreator
m_MethodName: TakePhoto
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!1 &478533099591268935
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -264,7 +276,7 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2599385092859443039}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
Expand All @@ -289,11 +301,22 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: cbd28a49a9d475c44b6330b33b212b42, type: 3}
m_Name:
m_EditorClassIdentifier:
takePhotoButton: {fileID: 478533099485849252}
cameraTextureTarget: {fileID: 478533101296809636}
onPhotoCaptured:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 478533099591268935}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!1 &2772493456747415611
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,19 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 9002801012519271861}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 5920718505741648111}
m_TargetAssemblyTypeName: PhotoCaptureConfirmElement, ReadyPlayerMe.Core.AvatarCreator
m_MethodName: ConfirmPhoto
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!1 &5783080387223977866
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -505,7 +517,7 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5920718505741648110}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
Expand All @@ -530,11 +542,70 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: cbd28a49a9d475c44b6330b33b212b42, type: 3}
m_Name:
m_EditorClassIdentifier:
takePhotoButton: {fileID: 8114457064649773333}
cameraTextureTarget: {fileID: 8114457064313110805}
onPhotoCaptured:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 7022141032107403024}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 1
m_CallState: 2
- m_Target: {fileID: 8114457064649773335}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 4379579555830577598}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 1
m_CallState: 2
- m_Target: {fileID: 5920718505741648112}
m_TargetAssemblyTypeName: PhotoCaptureElement, ReadyPlayerMe.Core.AvatarCreator
m_MethodName: StopCamera
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 1
m_CallState: 2
- m_Target: {fileID: 5920718505741648111}
m_TargetAssemblyTypeName: PhotoCaptureConfirmElement, ReadyPlayerMe.Core.AvatarCreator
m_MethodName: SetTexture
m_Mode: 0
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 1
m_CallState: 2
--- !u!114 &5920718505741648111
MonoBehaviour:
m_ObjectHideFlags: 0
Expand All @@ -547,11 +618,21 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 696f4cf6c3c6d434b8ab068afc2a8758, type: 3}
m_Name:
m_EditorClassIdentifier:
confirmPhotoButton: {fileID: 7103173979936244105}
retakePhotoButton: {fileID: 7540223026286860592}
onPhotoCaptureConfirmed:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 8114457064687947254}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!1 &7022141032107403024
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -672,7 +753,55 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 7553597193823120021}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 8114457064649773335}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 1
m_CallState: 2
- m_Target: {fileID: 7022141032107403024}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 4379579555830577598}
m_TargetAssemblyTypeName: UnityEngine.GameObject, UnityEngine
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 5920718505741648112}
m_TargetAssemblyTypeName: PhotoCaptureElement, ReadyPlayerMe.Core.AvatarCreator
m_MethodName: StartCamera
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!1 &8114457064313110807
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -866,7 +995,19 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 8114457064649773322}
m_OnClick:
m_PersistentCalls:
m_Calls: []
m_Calls:
- m_Target: {fileID: 5920718505741648112}
m_TargetAssemblyTypeName: PhotoCaptureElement, ReadyPlayerMe.Core.AvatarCreator
m_MethodName: TakePhoto
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!1 &8114457064687947254
GameObject:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit face150

Please sign in to comment.