From f06562a28106a92d57d53b6590b97ff6e11ac0fe Mon Sep 17 00:00:00 2001 From: Az <56699741+AlchlcDvl@users.noreply.github.com> Date: Thu, 5 Nov 2026 23:05:03 -0500 Subject: [PATCH] remove useless `.gameObject` uses --- MiraAPI/Hud/CustomPaginableMenu.cs | 8 ++++---- .../ModifierDisplay/ModifierUiComponent.cs | 2 +- .../Patches/Options/GameOptionsMenuPatch.cs | 4 ++-- .../Patches/Options/GamePresetsTabPatches.cs | 4 ++-- .../Patches/Options/GameSettingMenuPatches.cs | 5 +++-- .../Patches/Options/LobbyViewPanePatches.cs | 6 +++--- MiraAPI/Patches/Options/MenuState.cs | 20 +++++++++---------- .../Patches/Options/RoleSettingMenuPatches.cs | 2 +- MiraAPI/Patches/RoleGuidePatches.cs | 13 +++--------- MiraAPI/Roles/CustomRoleManager.cs | 4 ++-- MiraAPI/Utilities/Extensions.cs | 15 ++++++++++++++ MiraAPI/Utilities/Helpers.cs | 2 +- 12 files changed, 47 insertions(+), 38 deletions(-) diff --git a/MiraAPI/Hud/CustomPaginableMenu.cs b/MiraAPI/Hud/CustomPaginableMenu.cs index aca20b53..c8ef1cef 100644 --- a/MiraAPI/Hud/CustomPaginableMenu.cs +++ b/MiraAPI/Hud/CustomPaginableMenu.cs @@ -72,18 +72,18 @@ public abstract class CustomPaginableMenu : CustomPhoneMenu().sprite = MiraAssets.NextButton.LoadAsset(); - nextButton.gameObject.GetComponent().DestroyImmediate(); + nextButton.GetComponent().DestroyImmediate(); - var passiveButton = nextButton.gameObject.GetComponent(); + var passiveButton = nextButton.GetComponent(); passiveButton.OnClick = new Button.ButtonClickedEvent(); passiveButton.OnClick.AddListener((UnityAction)customMenu.NextPage); var backButton = Object.Instantiate(nextButton, customMenu.transform).gameObject; backButton.transform.localPosition = new Vector3(-1.85f, -2.185f, customMenu.MenuDepth); backButton.name = "LeftArrowButton"; - backButton.gameObject.GetComponent().Destroy(); + backButton.GetComponent().Destroy(); backButton.GetComponent().flipX = true; - var prevPassive = backButton.gameObject.GetComponent(); + var prevPassive = backButton.GetComponent(); prevPassive.OnClick.AddListener((UnityAction)customMenu.PreviousPage); customMenu.PhoneUI.GetChild(0).GetComponent().material = diff --git a/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs b/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs index 3c66358b..8638f75e 100644 --- a/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs +++ b/MiraAPI/Modifiers/ModifierDisplay/ModifierUiComponent.cs @@ -37,7 +37,7 @@ public class ModifierUiComponent(nint cppPtr) : MonoBehaviour(cppPtr) icon = modBg.transform.GetChild(1).GetComponent(); var nameBg = transform.GetChild(1); nameText = nameBg.GetChild(0).GetComponent(); - descRect = desc.gameObject.GetComponent(); + descRect = desc.GetComponent(); nameText.font = desc.font = HudManager.Instance.TaskPanel.taskText.font; nameText.fontMaterial = desc.fontMaterial = HudManager.Instance.TaskPanel.taskText.fontMaterial; diff --git a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs index 0799bf2f..3d19f0f6 100644 --- a/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs +++ b/MiraAPI/Patches/Options/GameOptionsMenuPatch.cs @@ -313,7 +313,7 @@ internal static class GameOptionsMenuPatch }; _modIcon = modIconObj.AddComponent(); _modIcon.sprite = MiraAssets.BlankSprite.LoadAsset(); - _modIcon.material = gamemodeBg.gameObject.GetComponent().material; + _modIcon.material = gamemodeBg.GetComponent().material; var gamemodeTextObj = Object.Instantiate(GameModeOption.OptionBehaviour.transform.GetChild(1).gameObject, modeInfoHolder); gamemodeTextObj.transform.localPosition = new Vector3(0.3f, 0, -0.01f); @@ -739,7 +739,7 @@ internal static class GameOptionsMenuPatch var newText = Object.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform); newText.text = $"({"MiraApi.GameSetting.Global.ClickToClose".Translate()})"; newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f); - newText.gameObject.GetComponent().Destroy(); + newText.GetComponent().Destroy(); var boxCol = categoryHeaderMasked.gameObject.AddComponent(); boxCol.size = new Vector2(7, 0.7f); diff --git a/MiraAPI/Patches/Options/GamePresetsTabPatches.cs b/MiraAPI/Patches/Options/GamePresetsTabPatches.cs index 4bf0cf83..a759cc1e 100644 --- a/MiraAPI/Patches/Options/GamePresetsTabPatches.cs +++ b/MiraAPI/Patches/Options/GamePresetsTabPatches.cs @@ -164,7 +164,7 @@ internal static class GamePresetsTabPatches return; } - var tmpTranslator = presetTab.PresetDescriptionText.gameObject.GetComponent(); + var tmpTranslator = presetTab.PresetDescriptionText.GetComponent(); if (tmpTranslator) { tmpTranslator.Destroy(); @@ -282,7 +282,7 @@ internal static class GamePresetsTabPatches _folderButton.name = "OpenFolderButton"; _folderButton.transform.localPosition = new Vector3(3.55f, 1.1f, -2); - var folderRend = openFolderButton.transform.FindChild("Sprite").gameObject.GetComponent(); + var folderRend = openFolderButton.transform.FindChild("Sprite").GetComponent(); folderRend.sprite = MiraAssets.FolderIcon.LoadAsset(); folderRend.transform.localScale = new Vector3(0.4f, 0.4f, 1); diff --git a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs index 6c1ede7a..47c45a06 100644 --- a/MiraAPI/Patches/Options/GameSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/GameSettingMenuPatches.cs @@ -20,9 +20,10 @@ internal static class GameSettingMenuPatches } // Add Menu State object - if (!__instance.gameObject.GetComponent()) + __instance.gameObject.GetOrAddComponent(out var existed); + + if (!existed) { - __instance.gameObject.AddComponent(); Info("MenuState component added to GameSettingMenu."); } else diff --git a/MiraAPI/Patches/Options/LobbyViewPanePatches.cs b/MiraAPI/Patches/Options/LobbyViewPanePatches.cs index c77f93d1..79315f3a 100644 --- a/MiraAPI/Patches/Options/LobbyViewPanePatches.cs +++ b/MiraAPI/Patches/Options/LobbyViewPanePatches.cs @@ -72,7 +72,7 @@ public static class LobbyViewPanePatches // create modifiers button modifiersTabButton = Object.Instantiate(__instance.rolesTabButton, __instance.rolesTabButton.transform.parent); - modifiersTabButton.buttonText.gameObject.GetComponent().Destroy(); + modifiersTabButton.buttonText.GetComponent().Destroy(); modifiersTabButton.name = "ModifiersTabButton"; var pos = modifiersTabButton.transform.localPosition; pos.x = 2.1f; @@ -101,7 +101,7 @@ public static class LobbyViewPanePatches hover.transform.localPosition = new Vector3(0, 0f, 0.3f); hover.sprite = MiraAssets.NextButtonActive.LoadAsset(); - var passiveButton = nextButton.gameObject.GetComponent(); + var passiveButton = nextButton.GetComponent(); passiveButton.OnClick = new Button.ButtonClickedEvent(); passiveButton.OnClick.AddListener( (UnityAction)(() => @@ -123,7 +123,7 @@ public static class LobbyViewPanePatches = backButton.transform.FindChild("Hover").gameObject.GetComponentInChildren().flipX = true; - var passiveButton2 = backButton.gameObject.GetComponent(); + var passiveButton2 = backButton.GetComponent(); passiveButton2.OnClick = new Button.ButtonClickedEvent(); passiveButton2.OnClick.AddListener( (UnityAction)(() => diff --git a/MiraAPI/Patches/Options/MenuState.cs b/MiraAPI/Patches/Options/MenuState.cs index 66876991..ee3c75f9 100644 --- a/MiraAPI/Patches/Options/MenuState.cs +++ b/MiraAPI/Patches/Options/MenuState.cs @@ -121,24 +121,24 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr) var nextButton = Instantiate(Gsm.BackButton, Gsm.BackButton.transform.parent).gameObject; nextButton.transform.localPosition = new Vector3(-2.2663f, 1.5272f, -25f); nextButton.name = "RightArrowButton"; - nextButton.transform.FindChild("Inactive").gameObject.GetComponent().sprite = + nextButton.transform.FindChild("Inactive").GetComponent().sprite = MiraAssets.NextButton.LoadAsset(); - nextButton.transform.FindChild("Active").gameObject.GetComponent().sprite = + nextButton.transform.FindChild("Active").GetComponent().sprite = MiraAssets.NextButtonActive.LoadAsset(); - nextButton.gameObject.GetComponent().DestroyImmediate(); + nextButton.GetComponent().DestroyImmediate(); - var nextPassiveButton = nextButton.gameObject.GetComponent(); + var nextPassiveButton = nextButton.GetComponent(); nextPassiveButton.OnClick = new Button.ButtonClickedEvent(); nextPassiveButton.OnClick.AddListener((UnityAction)NextMod); var previousButton = Instantiate(nextButton, Gsm.BackButton.transform.parent).gameObject; previousButton.transform.localPosition = new Vector3(-4.4209f, 1.5272f, -25f); previousButton.name = "LeftArrowButton"; - previousButton.gameObject.GetComponent().Destroy(); - previousButton.transform.FindChild("Active").gameObject.GetComponent().flipX = - previousButton.transform.FindChild("Inactive").gameObject.GetComponent().flipX = true; + previousButton.GetComponent().Destroy(); + previousButton.transform.FindChild("Active").GetComponent().flipX = + previousButton.transform.FindChild("Inactive").GetComponent().flipX = true; - var prevPassiveButton = previousButton.gameObject.GetComponent(); + var prevPassiveButton = previousButton.GetComponent(); prevPassiveButton.OnClick = new Button.ButtonClickedEvent(); prevPassiveButton.OnClick.AddListener((UnityAction)PreviousMod); @@ -149,7 +149,7 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr) var mBtn = Instantiate(Gsm.RoleSettingsButton, Gsm.RoleSettingsButton.transform.parent); _largeButtons[MenuButton.Modifiers] = mBtn; mBtn.name = "ModifiersButton"; - mBtn.buttonText.gameObject.GetComponent().Destroy(); + mBtn.buttonText.GetComponent().Destroy(); mBtn.buttonText.text = "Modifiers".Translate(); mBtn.OnClick = new Button.ButtonClickedEvent(); mBtn.OnClick.AddListener( @@ -168,7 +168,7 @@ public class MenuState(IntPtr cppPtr) : MonoBehaviour(cppPtr) var c1Btn = Instantiate(mBtn, mBtn.transform.parent); _largeButtons[MenuButton.CustomOne] = c1Btn; c1Btn.name = "CustomOneButton"; - c1Btn.buttonText.gameObject.GetComponent().Destroy(); + c1Btn.buttonText.GetComponent().Destroy(); c1Btn.OnClick = new Button.ButtonClickedEvent(); c1Btn.OnClick.AddListener( (UnityAction)(() => diff --git a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs index aa6396df..41e376d3 100644 --- a/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs +++ b/MiraAPI/Patches/Options/RoleSettingMenuPatches.cs @@ -309,7 +309,7 @@ public static class RoleSettingMenuPatches var newText = Object.Instantiate(categoryHeaderMasked.Title, categoryHeaderMasked.transform); newText.text = $"{label}"; newText.transform.localPosition = new Vector3(2.6249f, -0.165f, 0f); - newText.gameObject.GetComponent().Destroy(); + newText.GetComponent().Destroy(); scrollerNum -= 0.422f; diff --git a/MiraAPI/Patches/RoleGuidePatches.cs b/MiraAPI/Patches/RoleGuidePatches.cs index 198e2774..239e3115 100644 --- a/MiraAPI/Patches/RoleGuidePatches.cs +++ b/MiraAPI/Patches/RoleGuidePatches.cs @@ -1575,20 +1575,13 @@ public static class RoleGuidePatches private static void SetupPanelButton(MatchInfoRolePanel panel, Action onClick) { - var collider = panel.roleIcon.gameObject.GetComponent(); - if (!collider) - { - collider = panel.roleIcon.gameObject.AddComponent(); - } + var collider = panel.roleIcon.gameObject.GetOrAddComponent(); collider.size = new Vector2(0.13f, 0.13f); collider.offset = new Vector2(0, 0); collider.enabled = true; - var passiveButton = panel.roleIcon.gameObject.GetComponent(); - if (!passiveButton) - { - passiveButton = panel.roleIcon.gameObject.AddComponent(); - } + + var passiveButton = panel.roleIcon.gameObject.GetOrAddComponent(); passiveButton.enabled = true; passiveButton.ClickSound = HudManager.Instance.MapButton.ClickSound; diff --git a/MiraAPI/Roles/CustomRoleManager.cs b/MiraAPI/Roles/CustomRoleManager.cs index d89e40a9..3b48087c 100644 --- a/MiraAPI/Roles/CustomRoleManager.cs +++ b/MiraAPI/Roles/CustomRoleManager.cs @@ -239,7 +239,7 @@ public static class CustomRoleManager internal static TaskPanelBehaviour CreateRoleTab(ICustomRole role) { - var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").gameObject.GetComponent(); + var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").GetComponent(); var clonePanel = Object.Instantiate(ogPanel.gameObject, ogPanel.transform.parent); clonePanel.name = "RolePanel"; @@ -258,7 +258,7 @@ public static class CustomRoleManager internal static void UpdateRoleTab(TaskPanelBehaviour panel, ICustomRole role) { var tabText = panel.tab.gameObject.GetComponentInChildren(); - var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").gameObject.GetComponent(); + var ogPanel = HudManager.Instance.TaskStuff.transform.FindChild("TaskPanel").GetComponent(); if (tabText.text != role.RoleName) { tabText.text = role.RoleName; diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 0446e84f..d3a0fbae 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -843,6 +843,21 @@ public static class Extensions return comp ? comp : go.AddComponent(); } + /// + /// Gets a component that is attached to the provided , attaching and returning the component if it wasn't found, and returns a bool that indicates if the component already existed before. + /// + /// The component type. + /// The to get the component from. + /// When this method returns true if the component already existed; otherwise false. + /// The component. + public static T GetOrAddComponent(this GameObject go, out bool exists) + where T : Component + { + var comp = go.GetComponent(); + exists = comp; + return exists ? comp : go.AddComponent(); + } + /// /// The type of the attribute. /// true if a custom attribute of type is applied to element; otherwise, false. diff --git a/MiraAPI/Utilities/Helpers.cs b/MiraAPI/Utilities/Helpers.cs index 638c41a4..f5d7d25d 100644 --- a/MiraAPI/Utilities/Helpers.cs +++ b/MiraAPI/Utilities/Helpers.cs @@ -304,7 +304,7 @@ public static class Helpers { var prefab = Object.FindObjectOfType(true); var arrow = Object.Instantiate(prefab, parent); - arrow.image = arrow.gameObject.GetComponent(); + arrow.image = arrow.GetComponent(); arrow.image.color = color; arrow.gameObject.layer = 5; arrow.gameObject.SetActive(true);