Skip to content

Commit

Permalink
fix: fix potential issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Feb 27, 2025
1 parent b38dd4c commit 3377af2
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ internal static void ConvertTo<T>(this Object context) where T : MonoBehaviour
target.enabled = false;

// Find MonoScript of the specified component.
foreach (var script in Resources.FindObjectsOfTypeAll<MonoScript>())
foreach (var script in MonoImporter.GetAllRuntimeMonoScripts())
{
if (script.GetClass() != typeof(T))
{
Expand Down
15 changes: 14 additions & 1 deletion Packages/src/Runtime/Internal/Extensions/GraphicExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@ internal static class GraphicExtensions
private static readonly Bounds s_ScreenBounds = new Bounds(new Vector3(0.5f, 0.5f, 0.5f), new Vector3(1, 1, 1));

/// <summary>
/// Check if a Graphic component is currently in the screen view.
/// Get material for rendering.
/// </summary>
public static Material GetMaterialForRendering(this Graphic self)
{
if (!self || !self.isActiveAndEnabled) return null;

var cr = self.canvasRenderer;
if (!cr || cr.materialCount == 0) return null;

return cr.GetMaterial();
}

/// <summary>
/// Get materials for rendering.
/// </summary>
public static void GetMaterialsForRendering(this Graphic self, List<Material> result)
{
Expand Down
11 changes: 11 additions & 0 deletions Packages/src/Runtime/Internal/Utilities/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ public static T[] GetAllComponentsInPrefabStage<T>() where T : Component

public static bool isBatchOrBuilding => Application.isBatchMode || BuildPipeline.isBuildingPlayer;
#endif

[Conditional("UNITY_EDITOR")]
public static void QueuePlayerLoopUpdate()
{
#if UNITY_EDITOR
if (!EditorApplication.isPlaying)
{
EditorApplication.QueuePlayerLoopUpdate();
}
#endif
}
}

#if !UNITY_2021_2_OR_NEWER
Expand Down
15 changes: 2 additions & 13 deletions Packages/src/Runtime/SoftMask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,13 +652,8 @@ private void OnCanvasViewChanged()
{
_hasResolutionChanged = true;
SetDirtyAndNotify();

Misc.QueuePlayerLoopUpdate();
#if UNITY_EDITOR
if (!Application.isPlaying)
{
EditorApplication.QueuePlayerLoopUpdate();
}

EditorApplication.delayCall += () =>
{
if (!this || !isActiveAndEnabled) return;
Expand Down Expand Up @@ -833,13 +828,7 @@ private void RenderSoftMaskBuffer()
Profiler.EndSample();
}

#if UNITY_EDITOR
if (!Application.isPlaying)
{
EditorApplication.QueuePlayerLoopUpdate();
}
#endif

Misc.QueuePlayerLoopUpdate();
onRenderSoftMaskBuffer?.Invoke(this);
}

Expand Down
19 changes: 11 additions & 8 deletions Packages/src/Runtime/SoftMaskable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Coffee.UISoftMaskInternal;
using UnityEditor;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;
Expand Down Expand Up @@ -249,15 +250,14 @@ Material IMaterialModifier.GetModifiedMaterial(Material baseMaterial)
threshold = s.softnessRange.average;
subtract = true;
}
else if (_softMask)
else
{
threshold = _softMask.softnessRange.average;
}
}

localId = (uint)(Mathf.Clamp01(threshold) * (1 << 8) + (subtract ? 1 << 9 : 0) + (localId << 10));
#endif

var hash = new Hash128(
(uint)baseMaterial.GetInstanceID(),
(uint)_softMask.softMaskBuffer.GetInstanceID(),
Expand Down Expand Up @@ -307,6 +307,7 @@ public void SetMaterialDirty()
{
if (!isActiveAndEnabled || !_graphic) return;
_graphic.SetMaterialDirty();
Misc.QueuePlayerLoopUpdate();
}

public void SetMaterialDirtyForChildren()
Expand Down Expand Up @@ -345,7 +346,9 @@ private void OnValidate()
private void UpdateSceneViewMatrix()
{
if (!_graphic || !_graphic.canvas || !_maskableMaterial) return;
if (FrameCache.TryGet(_maskableMaterial, nameof(UpdateSceneViewMatrix), out bool _))

var mat = _graphic.GetMaterialForRendering();
if (!mat || FrameCache.TryGet(mat, nameof(UpdateSceneViewMatrix), out bool _))
{
return;
}
Expand Down Expand Up @@ -386,8 +389,8 @@ private void UpdateSceneViewMatrix()

// Set view and projection matrices.
Profiler.BeginSample("(SM4UI)[SoftMaskable] (Editor) UpdateSceneViewMatrix > Set matrices");
_maskableMaterial.SetMatrix(s_GameVp, gameVp);
_maskableMaterial.SetMatrix(s_GameTvp, gameTvp);
mat.SetMatrix(s_GameVp, gameVp);
mat.SetMatrix(s_GameTvp, gameTvp);
Profiler.EndSample();

// Calc Right eye matrices.
Expand All @@ -404,11 +407,11 @@ private void UpdateSceneViewMatrix()
Profiler.EndSample();
}

_maskableMaterial.SetMatrix(s_GameVp2, gameVp);
_maskableMaterial.SetMatrix(s_GameTvp2, gameVp);
mat.SetMatrix(s_GameVp2, gameVp);
mat.SetMatrix(s_GameTvp2, gameVp);
}

FrameCache.Set(_maskableMaterial, nameof(UpdateSceneViewMatrix), true);
FrameCache.Set(mat, nameof(UpdateSceneViewMatrix), true);
}
#endif
}
Expand Down
1 change: 1 addition & 0 deletions Packages/src/Runtime/Utilities/SoftMaskUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ public static Material CreateSoftMaskable(
Profiler.EndSample();

Profiler.BeginSample("(SM4UI)[SoftMaskableMaterial] Create > Set Properties");
mat.CopyPropertiesFromMaterial(baseMat);
mat.SetTexture(s_SoftMaskTex, softMaskBuffer);
mat.SetInt(s_SoftMaskableStereo, isStereo ? 1 : 0);
mat.SetVector(s_SoftMaskColor, new Vector4(
Expand Down

0 comments on commit 3377af2

Please sign in to comment.