Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased

### Changes

- [UUM-131032] Added a reset of static variables when entering playmode to allow fast enter playmode compatibility.
- [UUM-138960] Removed a large utility dictionary to reduce binary size and runtime memory overhead.

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions Debug/Scripts/ShowHoveredInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public class ShowHoveredInfo : MonoBehaviour
static Material m_Material;
bool m_IsHovering;

#if UNITY_EDITOR
[RuntimeInitializeOnLoadMethod]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high
The default load type for RuntimeInitializeOnLoadMethod is AfterSceneLoad, which executes after the scene has loaded and Awake has been called. If an instance of ShowHoveredInfo exists in the scene, its Start method may run before this reset, causing it to initialize m_Material only for it to be immediately nulled here. This can lead to NullReferenceExceptions if the material is used later and also results in a resource leak.

Using BeforeSceneLoad ensures the static reference is cleared before any instance logic runs.

Suggested change
[RuntimeInitializeOnLoadMethod]
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]

🤖 Helpful? 👍/👎

static void ResetStaticsOnLoad()
{
m_Material = null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium
When 'Enter Play Mode' is used with Domain Reload disabled, static material instances created via new Material() will leak if the reference is nulled without the material being destroyed. It is recommended to explicitly destroy the material before clearing the reference to prevent memory accumulation in the Editor.

Suggested change
m_Material = null;
if (m_Material != null) UnityEngine.Object.DestroyImmediate(m_Material);
m_Material = null;

🤖 Helpful? 👍/👎

}
#endif

void Start()
{
if (m_Material == null)
Expand Down
12 changes: 12 additions & 0 deletions Editor/EditorCore/CutTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
Undo.undoRedoPerformed += UndoRedoPerformed;
MeshSelection.objectSelectionChanged += UpdateTarget;
ProBuilderEditor.selectModeChanged += OnSelectModeChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}

public override void OnWillBeDeactivated()
Expand All @@ -210,6 +211,7 @@
Undo.undoRedoPerformed -= UndoRedoPerformed;
MeshSelection.objectSelectionChanged -= UpdateTarget;
ProBuilderEditor.selectModeChanged -= OnSelectModeChanged;
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;

Check warning on line 214 in Editor/EditorCore/CutTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/CutTool.cs#L214

Added line #L214 was not covered by tests
}

/// <summary>
Expand Down Expand Up @@ -242,6 +244,16 @@
ToolManager.RestorePreviousTool();
}

private void OnPlayModeStateChanged(PlayModeStateChange state)
{

Check warning on line 248 in Editor/EditorCore/CutTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/CutTool.cs#L248

Added line #L248 was not covered by tests
if (state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.ExitingPlayMode)
{
// Reset tool state when entering/exiting playmode
if (ToolManager.IsActiveTool(this))
ExitTool();

Check warning on line 253 in Editor/EditorCore/CutTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/CutTool.cs#L253

Added line #L253 was not covered by tests
}
}

/// <summary>
/// Undo/Redo callback: Reset and recompute lines, and update the targeted face if needed
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions Editor/EditorCore/DrawShapeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@
ToolManager.activeToolChanged += OnActiveToolChanged;
ToolManager.activeContextChanged += OnActiveContextChanged;
ProBuilderEditor.selectModeChanged += OnSelectModeChanged;
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;

Check warning on line 726 in Editor/EditorCore/DrawShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/DrawShapeTool.cs#L726

Added line #L726 was not covered by tests

if (m_CurrentState == null)
m_CurrentState = InitStateMachine();
Expand All @@ -741,6 +742,7 @@
ToolManager.activeToolChanged -= OnActiveToolChanged;
ToolManager.activeContextChanged -= OnActiveContextChanged;
ProBuilderEditor.selectModeChanged -= OnSelectModeChanged;
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;

Check warning on line 745 in Editor/EditorCore/DrawShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/DrawShapeTool.cs#L745

Added line #L745 was not covered by tests

if (m_ProBuilderShape != null && !( m_CurrentState is ShapeState_InitShape ))
m_CurrentState = ShapeState.ResetState();
Expand All @@ -766,6 +768,19 @@
ToolManager.RestorePreviousPersistentTool();
}

private void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.ExitingEditMode || state == PlayModeStateChange.ExitingPlayMode)
{

Check warning on line 774 in Editor/EditorCore/DrawShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/DrawShapeTool.cs#L771-L774

Added lines #L771 - L774 were not covered by tests
// Reset tool state when entering/exiting playmode
if (ToolManager.IsActiveTool(this))
{
m_CurrentState = ShapeState.ResetState();
ToolManager.RestorePreviousPersistentTool();
}
}
}

Check warning on line 783 in Editor/EditorCore/DrawShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/DrawShapeTool.cs#L776-L783

Added lines #L776 - L783 were not covered by tests
void HandleUndoRedoPerformed()
{
if(ToolManager.IsActiveTool(this))
Expand Down
24 changes: 24 additions & 0 deletions Editor/EditorCore/EditShapeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,30 @@
}
}

[InitializeOnEnterPlayMode]

Check warning on line 97 in Editor/EditorCore/EditShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditShapeTool.cs#L97

Added line #L97 was not covered by tests
static void ResetStaticsOnLoad()
{
s_Faces = null;
s_CurrentId = -1;
s_SizeManipulationInit = false;

Check warning on line 102 in Editor/EditorCore/EditShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditShapeTool.cs#L99-L102

Added lines #L99 - L102 were not covered by tests
s_SizeDelta = 0f;
s_StartMousePosition = Vector2.zero;
s_StartSize = Vector3.zero;
s_StartPositionLocal = Vector3.zero;
s_StartPositionGlobal = Vector3.zero;
s_StartScale = Vector3.zero;
s_StartScaleInverse = Vector3.zero;
s_StartCenter = Vector3.zero;

Check warning on line 110 in Editor/EditorCore/EditShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditShapeTool.cs#L106-L110

Added lines #L106 - L110 were not covered by tests
s_Direction = Vector3.zero;
s_DefaultMidpointHandleSize = 0.03f;
s_DefaultMidpointSquareSize = 0.15f;
s_CurrentAngle = 0f;
s_CurrentArrowHovered = -1;

Check warning on line 115 in Editor/EditorCore/EditShapeTool.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditShapeTool.cs#L113-L115

Added lines #L113 - L115 were not covered by tests
s_ShapeRotation = Quaternion.identity;
s_ArrowsLines = null;
s_IconContent = null;
}

public override bool gridSnapEnabled => true;

static GUIContent s_IconContent;
Expand Down
8 changes: 8 additions & 0 deletions Editor/EditorCore/EditorGUILayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@
static int s_ResizeHandleAreaDimension = 6;
static int s_MoveWindowAreaHeight = 30;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_RowToggle = true;
s_ResizeHandleAreaDimension = 6;
s_MoveWindowAreaHeight = 30;
}

Check warning on line 204 in Editor/EditorCore/EditorGUILayout.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorGUILayout.cs#L202-L204

Added lines #L202 - L204 were not covered by tests

public static Rect DoResizeHandle(Rect rect, int minimumWidth, int minimumHeight)
{
var evt = Event.current;
Expand Down
21 changes: 21 additions & 0 deletions Editor/EditorCore/EditorGUIUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
: new GUIContent("FCE", "Face Selection"),
};
}

internal static void ResetForPlayMode()
{
selectModeIcons = null;
s_ObjectIcon = null;
s_VertexIcon = null;
s_EdgeIcon = null;
s_FaceIcon = null;
}

Check warning on line 56 in Editor/EditorCore/EditorGUIUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorGUIUtility.cs#L51-L56

Added lines #L51 - L56 were not covered by tests
}

static readonly Color TOOL_SETTINGS_COLOR = UnityEditor.EditorGUIUtility.isProSkin
Expand Down Expand Up @@ -469,6 +478,18 @@

static GUIStyle _sceneBoldLabel = null;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
Styles.ResetForPlayMode();
_splitStyle = null;
_centeredGreyMiniLabel = null;
_solidBackgroundStyle = null;
_buttonNoBackgroundSmallMarginStyle = null;
_sceneBoldLabel = null;
sceneLabelRect = new Rect(0f, 0f, 0f, 0f);
}

Check warning on line 491 in Editor/EditorCore/EditorGUIUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorGUIUtility.cs#L482-L491

Added lines #L482 - L491 were not covered by tests

/**
* Draw a label in the scene view with a solid color background.
*/
Expand Down
35 changes: 27 additions & 8 deletions Editor/EditorCore/EditorHandleDrawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,36 @@
ResetPreferences();
}

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
ReleaseResources();
s_Initialized = false;

Check warning on line 207 in Editor/EditorCore/EditorHandleDrawing.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorHandleDrawing.cs#L207

Added line #L207 was not covered by tests
s_WireHandles = null;
s_VertexHandles = null;
s_SelectedFaceHandles = null;
s_SelectedVertexHandles = null;
s_SelectedEdgeHandles = null;
s_TemporaryHandles = null;
}

internal static void ReleaseResources()
{
ClearHandles();
if(s_MeshPool2 != null)
s_MeshPool2.Dispose();
if(s_EdgeMaterial != null) UnityObject.DestroyImmediate(s_EdgeMaterial);
if(s_WireMaterial != null) UnityObject.DestroyImmediate(s_WireMaterial);
if(s_LineMaterial != null) UnityObject.DestroyImmediate(s_LineMaterial);
if(s_VertMaterial != null) UnityObject.DestroyImmediate(s_VertMaterial);
if(s_GlWireMaterial != null) UnityObject.DestroyImmediate(s_GlWireMaterial);
if(s_FaceMaterial != null) UnityObject.DestroyImmediate(s_FaceMaterial);
if (s_MeshPool2 != null) s_MeshPool2.Dispose();
if (s_FaceMaterial != null) UnityObject.DestroyImmediate(s_FaceMaterial);
if (s_EdgeMaterial != null) UnityObject.DestroyImmediate(s_EdgeMaterial);
if (s_VertMaterial != null) UnityObject.DestroyImmediate(s_VertMaterial);
if (s_WireMaterial != null) UnityObject.DestroyImmediate(s_WireMaterial);
if (s_LineMaterial != null) UnityObject.DestroyImmediate(s_LineMaterial);
if (s_GlWireMaterial != null) UnityObject.DestroyImmediate(s_GlWireMaterial);
s_MeshPool2 = null;
s_FaceMaterial = null;
s_EdgeMaterial = null;
s_VertMaterial = null;
s_WireMaterial = null;
s_LineMaterial = null;
s_GlWireMaterial = null;
}

internal static void ResetPreferences()
Expand Down
13 changes: 13 additions & 0 deletions Editor/EditorCore/EditorHandleUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@
public static int CurrentID { get { return currentId; } }
static int currentId = -1;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{

Check warning on line 72 in Editor/EditorCore/EditorHandleUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorHandleUtility.cs#L72

Added line #L72 was not covered by tests
if (s_HandleMaterial != null)
Object.DestroyImmediate(s_HandleMaterial);
s_HandleMaterial = null;

Check warning on line 75 in Editor/EditorCore/EditorHandleUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorHandleUtility.cs#L74-L75

Added lines #L74 - L75 were not covered by tests
currentId = -1;
s_HandleMatrix.Clear();
handleOffset = Vector2.zero;
initialMousePosition = Vector2.zero;
axisConstraint = new HandleConstraint2D(0, 0);
}

Check warning on line 81 in Editor/EditorCore/EditorHandleUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorHandleUtility.cs#L77-L81

Added lines #L77 - L81 were not covered by tests

static Vector2 handleOffset = Vector2.zero;
static Vector2 initialMousePosition = Vector2.zero;

Expand Down
12 changes: 12 additions & 0 deletions Editor/EditorCore/EditorSceneViewPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
// When enabled, a mouse click on an unselected mesh will select both the GameObject and the mesh element picked.
const bool k_AllowUnselected = true;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_DeepSelectionPrevious = 0x0;
s_AppendModifierPreviousState = false;
s_Selection.Clear();
s_NearestVertices.Clear();
s_OverlappingGameObjects.Clear();
s_IndexBuffer.Clear();
s_EdgeBuffer.Clear();
}

Check warning on line 47 in Editor/EditorCore/EditorSceneViewPicker.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorSceneViewPicker.cs#L37-L47

Added lines #L37 - L47 were not covered by tests
public static void DoMouseHover(SceneSelection selection)
{
if (selection.faces.Count == 0)
Expand Down
8 changes: 8 additions & 0 deletions Editor/EditorCore/EditorShapeUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@
}
}

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_AvailableShapeTypes = null;
s_ShapeTypes = null;
s_ShapeTypesGUILists = null;
}

Check warning on line 113 in Editor/EditorCore/EditorShapeUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorShapeUtility.cs#L107-L113

Added lines #L107 - L113 were not covered by tests
static EditorShapeUtility()
{
ResetPrefs();
Expand Down
6 changes: 6 additions & 0 deletions Editor/EditorCore/EditorStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
static GUIStyle s_HeaderLabel;
static GUIStyle s_SceneTextBox;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_Initialized = false;
}

Check warning on line 36 in Editor/EditorCore/EditorStyles.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorStyles.cs#L31-L36

Added lines #L31 - L36 were not covered by tests
public static GUIStyle buttonStyle { get { Init(); return s_ButtonStyle; } }
public static GUIStyle toolbarHelpIcon { get { Init(); return s_ToolbarHelpIcon; } }
public static GUIStyle settingsGroup { get { Init(); return s_SettingsGroup; } }
Expand Down
16 changes: 16 additions & 0 deletions Editor/EditorCore/EditorUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@
static EditorWindow s_NotificationWindow;
static bool s_IsNotificationDisplayed;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_IsNotificationDisplayed = false;
EditorApplication.update -= NotifUpdate;
if (s_NotificationWindow != null)
{
// Window may be destroyed when entering Play Mode without domain reload.
try { s_NotificationWindow.RemoveNotification(); }
catch {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low
Empty catch blocks can swallow unexpected exceptions and make troubleshooting difficult. If the exception is expected because the window might be destroyed, consider checking for null first or catching a specific exception type (e.g., MissingReferenceException).

🤖 Helpful? 👍/👎

}

Check warning on line 41 in Editor/EditorCore/EditorUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorUtility.cs#L32-L41

Added lines #L32 - L41 were not covered by tests

s_NotificationWindow = null;
s_NotificationTimer = 0f;
}

Check warning on line 45 in Editor/EditorCore/EditorUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/EditorUtility.cs#L43-L45

Added lines #L43 - L45 were not covered by tests

[UserSetting("General", "Show Action Notifications", "Enable or disable notification popups when performing actions.")]
static Pref<bool> s_ShowNotifications = new Pref<bool>("editor.showEditorNotifications", false);

Expand Down
6 changes: 6 additions & 0 deletions Editor/EditorCore/IconUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

static string s_IconFolderPath = "Packages/com.unity.probuilder/Editor Default Resources/Icons/";

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_Icons.Clear();
}

Check warning on line 27 in Editor/EditorCore/IconUtility.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/IconUtility.cs#L25-L27

Added lines #L25 - L27 were not covered by tests

/// <summary>
/// Load an icon from icons folder located in the package's 'Editor Default Resources'.
/// Naming convention is: "path/to/iconName" (without extension). Use the 'd_' prefix for dark skin icons.
Expand Down
27 changes: 21 additions & 6 deletions Editor/EditorCore/Lightmapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
public static readonly GUIContent areaError = new GUIContent("Area Error", "");

static bool s_Initialized;
public static GUIStyle miniButton;
public static GUIStyle s_MiniButton;
public static GUIStyle miniButton { get { Init(); return s_MiniButton; } }

Check warning on line 41 in Editor/EditorCore/Lightmapping.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/Lightmapping.cs#L41

Added line #L41 was not covered by tests
public static bool unwrapSettingsFoldout;

public static void Init()
Expand All @@ -47,12 +48,20 @@

s_Initialized = true;

miniButton = new GUIStyle(GUI.skin.button);
miniButton.stretchHeight = false;
miniButton.stretchWidth = false;
miniButton.padding = new RectOffset(6, 6, 3, 3);
miniButton.margin = new RectOffset(4, 4, 4, 0);
s_MiniButton = new GUIStyle(GUI.skin.button);
s_MiniButton.stretchHeight = false;
s_MiniButton.stretchWidth = false;
s_MiniButton.padding = new RectOffset(6, 6, 3, 3);
s_MiniButton.margin = new RectOffset(4, 4, 4, 0);

Check warning on line 55 in Editor/EditorCore/Lightmapping.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/Lightmapping.cs#L51-L55

Added lines #L51 - L55 were not covered by tests
}

#if UNITY_EDITOR
internal static void Reset()
{
s_Initialized = false;
unwrapSettingsFoldout = false;
}
#endif

Check warning on line 64 in Editor/EditorCore/Lightmapping.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/Lightmapping.cs#L60-L64

Added lines #L60 - L64 were not covered by tests
}

[UserSettingBlock("Mesh Settings")]
Expand Down Expand Up @@ -106,6 +115,12 @@
Undo.postprocessModifications += PostprocessModifications;
}

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
Styles.Reset();
}

Check warning on line 123 in Editor/EditorCore/Lightmapping.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/Lightmapping.cs#L118-L123

Added lines #L118 - L123 were not covered by tests
/// <summary>
/// Toggles the LightmapStatic bit of an objects Static flags.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions Editor/EditorCore/MaterialEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@
// The currently loaded material palette asset.
static MaterialPalette s_CurrentPalette = null;

[InitializeOnEnterPlayMode]
static void ResetStaticsOnLoad()
{
s_CurrentPalette = null;
instance?.RefreshAvailablePalettes();
}

Check warning on line 139 in Editor/EditorCore/MaterialEditor.cs

View check run for this annotation

Codecov GitHub.com / codecov/patch

Editor/EditorCore/MaterialEditor.cs#L135-L139

Added lines #L135 - L139 were not covered by tests

// The user set "quick material"
Pref<Material> m_QueuedMaterial = new Pref<Material>("materialEditor.quickMaterial", null, SettingsScope.User);

Expand Down
Loading