From 78cbfcd18e92604f70e894d42880e3099b8f6093 Mon Sep 17 00:00:00 2001 From: 4demec <102261683+4demec@users.noreply.github.com> Date: Sun, 14 Apr 2024 00:32:17 +0200 Subject: [PATCH] Fixed incorrect return values fixed incorrect non-reference return values causing null reference errors when being cast to T in GetItem() --- .../Runtime/Core/StatefulComponent.cs | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/Assets/Plugins/StatefulUI/Runtime/Core/StatefulComponent.cs b/Assets/Plugins/StatefulUI/Runtime/Core/StatefulComponent.cs index f51c38f..4b2cbf1 100644 --- a/Assets/Plugins/StatefulUI/Runtime/Core/StatefulComponent.cs +++ b/Assets/Plugins/StatefulUI/Runtime/Core/StatefulComponent.cs @@ -198,24 +198,7 @@ private GameObject GetObject(int role) return null; } - private Image GetImage(int role) - { - for (var i = 0; i < Images.Count; i++) - { - if (Images[i].Role == role) - { - return Images[i].Image; - } - } - - var go = gameObject; - var roleName = RoleUtils.GetName(RoleUtils.ImageRoleType, role); - Debug.LogError($"View {name} does not contain image with role {roleName}, scene path: {go.GetScenePath()}", go); - - return null; - } - - private ImageReference GetImageReference(int role) + private ImageReference GetImage(int role) { for (var i = 0; i < Images.Count; i++) { @@ -290,15 +273,15 @@ private AnimatorReference GetAnimator(int role) return null; } - public bool TryGetContainer(int role, out ContainerView view) + public bool TryGetContainer(int role, out ContainerReference containerRef) { - view = null; + containerRef = null; for (var i = 0; i < Containers.Count; i++) { if (Containers[i].Role == role) { - view = Containers[i].Container; + containerRef = Containers[i]; return true; } } @@ -306,25 +289,25 @@ public bool TryGetContainer(int role, out ContainerView view) return false; } - private ContainerView GetContainer(int role) + private ContainerReference GetContainer(int role) { - if (!TryGetContainer(role, out var view)) + if (!TryGetContainer(role, out var containerRef)) { var go = gameObject; var roleName = RoleUtils.GetName(RoleUtils.ContainerRoleType, role); Debug.LogError($"View {name} does not contain container with role {roleName}, scene path: {go.GetScenePath()}", go); } - return view; + return containerRef; } - private Slider GetSlider(int role) + private SliderReference GetSlider(int role) { for (var i = 0; i < Sliders.Count; i++) { if (Sliders[i].Role == role) { - return Sliders[i].Slider; + return Sliders[i]; } } @@ -335,13 +318,13 @@ private Slider GetSlider(int role) return null; } - private Toggle GetToggle(int role) + private ToggleReference GetToggle(int role) { for (var i = 0; i < Toggles.Count; i++) { if (Toggles[i].Role == role) { - return Toggles[i].Toggle; + return Toggles[i]; } } @@ -457,12 +440,12 @@ public T GetItem(int roleValue) where T : class { var type = typeof(T); var manager = StatefulUiManager.Instance; - + if (type == manager.AnimatorReferenceType) return GetAnimator(roleValue) as T; if (type == manager.ButtonReferenceType) return GetButton(roleValue) as T; if (type == manager.ContainerReferenceType) return GetContainer(roleValue) as T; if (type == manager.DropdownReferenceType) return GetDropdown(roleValue) as T; - if (type == manager.ImageReferenceType) return GetImageReference(roleValue) as T; + if (type == manager.ImageReferenceType) return GetImage(roleValue) as T; if (type == manager.InnerComponentReferenceType) return GetInnerComponent(roleValue) as T; if (type == manager.ObjectReferenceType) return GetObject(roleValue) as T; if (type == manager.SliderReferenceType) return GetSlider(roleValue) as T; @@ -471,6 +454,7 @@ public T GetItem(int roleValue) where T : class if (type == manager.ToggleReferenceType) return GetToggle(roleValue) as T; if (type == manager.VideoPlayerReferenceType) return GetVideoPlayer(roleValue) as T; + throw new Exception($"Type {type} is not supported"); }