Minor fix

This commit is contained in:
Savya Bikram Shah
2026-05-29 16:24:50 +05:45
parent b39d2a798c
commit 6872158d2d
2 changed files with 46 additions and 4 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading; using System.Threading;
using Cysharp.Threading.Tasks; using Cysharp.Threading.Tasks;
using Darkmatter.Core.Contracts.Services.Capture; using Darkmatter.Core.Contracts.Services.Capture;
@@ -10,6 +11,19 @@ namespace Darkmatter.Services.Capture
public async UniTask<byte[]> CapturePngAsync(GameObject captureObject, float scale, public async UniTask<byte[]> CapturePngAsync(GameObject captureObject, float scale,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var paperCanvas = GetRootCanvas(captureObject);
var disabledCanvases = DisableOtherRootCanvases(paperCanvas);
var cam = Camera.main;
CameraClearFlags prevFlags = default;
Color prevBg = default;
if (cam != null)
{
prevFlags = cam.clearFlags;
prevBg = cam.backgroundColor;
cam.clearFlags = CameraClearFlags.SolidColor;
cam.backgroundColor = new Color(0f, 0f, 0f, 0f);
}
await UniTask.WaitForEndOfFrame(cancellationToken); await UniTask.WaitForEndOfFrame(cancellationToken);
var fullScreen = ScreenCapture.CaptureScreenshotAsTexture(); var fullScreen = ScreenCapture.CaptureScreenshotAsTexture();
@@ -48,9 +62,35 @@ namespace Darkmatter.Services.Capture
finally finally
{ {
Object.Destroy(fullScreen); Object.Destroy(fullScreen);
foreach (var c in disabledCanvases) if (c != null) c.enabled = true;
if (cam != null)
{
cam.clearFlags = prevFlags;
cam.backgroundColor = prevBg;
}
} }
} }
private static Canvas GetRootCanvas(GameObject go)
{
if (go == null) return null;
var c = go.GetComponentInParent<Canvas>();
return c != null ? c.rootCanvas : null;
}
private static List<Canvas> DisableOtherRootCanvases(Canvas keep)
{
var disabled = new List<Canvas>();
var all = Object.FindObjectsByType<Canvas>(FindObjectsSortMode.None);
foreach (var c in all)
{
if (c == null || !c.isRootCanvas || c == keep || !c.enabled) continue;
c.enabled = false;
disabled.Add(c);
}
return disabled;
}
private static Rect ComputeCropRect(GameObject target, int screenW, int screenH) private static Rect ComputeCropRect(GameObject target, int screenW, int screenH)
{ {
if (target == null || target.transform is not RectTransform rt) if (target == null || target.transform is not RectTransform rt)

View File

@@ -274,6 +274,7 @@ RectTransform:
- {fileID: 1310839949} - {fileID: 1310839949}
- {fileID: 376589367} - {fileID: 376589367}
- {fileID: 1143672390} - {fileID: 1143672390}
- {fileID: 1340226039}
m_Father: {fileID: 201822947} m_Father: {fileID: 201822947}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
@@ -354,7 +355,6 @@ RectTransform:
- {fileID: 1518670451} - {fileID: 1518670451}
- {fileID: 1989194441} - {fileID: 1989194441}
- {fileID: 153461769} - {fileID: 153461769}
- {fileID: 1340226039}
m_Father: {fileID: 2069155641} m_Father: {fileID: 2069155641}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0} m_AnchorMin: {x: 0, y: 0}
@@ -1395,6 +1395,8 @@ MonoBehaviour:
m_Name: m_Name:
m_EditorClassIdentifier: Features.GameplayFlow::Darkmatter.Features.GameplayFlow.GameplayFlowFeatureModule m_EditorClassIdentifier: Features.GameplayFlow::Darkmatter.Features.GameplayFlow.GameplayFlowFeatureModule
sceneRefs: {fileID: 396806867} sceneRefs: {fileID: 396806867}
nextButtonView: {fileID: 0}
backButtonView: {fileID: 0}
--- !u!1 &1129540368 --- !u!1 &1129540368
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -2219,17 +2221,17 @@ RectTransform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1340226038} m_GameObject: {fileID: 1340226038}
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_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0} m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:
- {fileID: 1141121865} - {fileID: 1141121865}
m_Father: {fileID: 201822947} m_Father: {fileID: 153461769}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5} m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5} m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0} m_AnchoredPosition: {x: -139.44305, y: 163.66599}
m_SizeDelta: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1340226040 --- !u!114 &1340226040