diff --git a/src/Console/AutoCompleter.cs b/src/Console/AutoCompleter.cs index cb6dde8..769e669 100644 --- a/src/Console/AutoCompleter.cs +++ b/src/Console/AutoCompleter.cs @@ -4,6 +4,7 @@ using System.Linq; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; +using UnityExplorer.Helpers; using UnityExplorer.UI; using UnityExplorer.UI.Modules; @@ -292,12 +293,7 @@ namespace UnityExplorer.Console hiddenChild.SetActive(false); var hiddenText = hiddenChild.AddComponent(); m_hiddenSuggestionTexts.Add(hiddenText); - -#if CPP - btn.onClick.AddListener(new Action(UseAutocompleteButton)); -#else btn.onClick.AddListener(UseAutocompleteButton); -#endif void UseAutocompleteButton() { diff --git a/src/Console/CodeEditor.cs b/src/Console/CodeEditor.cs index b297ded..b1687f2 100644 --- a/src/Console/CodeEditor.cs +++ b/src/Console/CodeEditor.cs @@ -11,6 +11,7 @@ using UnityExplorer.UI.Modules; using System.Collections.Generic; using System.Reflection; using UnityExplorer.UI.Shared; +using UnityExplorer.Helpers; namespace UnityExplorer.Console { @@ -72,11 +73,7 @@ The following helper methods are available: ConstructUI(); -#if CPP - InputField.onValueChanged.AddListener(new Action((string s) => { OnInputChanged(s); })); -#else - this.InputField.onValueChanged.AddListener((string s) => { OnInputChanged(s); }); -#endif + InputField.onValueChanged.AddListener((string s) => { OnInputChanged(s); }); } public void Update() @@ -354,11 +351,7 @@ The following helper methods are available: // Enable Ctrl+R toggle var ctrlRToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle ctrlRToggle, out Text ctrlRToggleText); -#if CPP - ctrlRToggle.onValueChanged.AddListener(new Action(CtrlRToggleCallback)); -#else ctrlRToggle.onValueChanged.AddListener(CtrlRToggleCallback); -#endif void CtrlRToggleCallback(bool val) { EnableCtrlRShortcut = val; @@ -374,11 +367,7 @@ The following helper methods are available: // Enable Suggestions toggle var suggestToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle suggestToggle, out Text suggestToggleText); -#if CPP - suggestToggle.onValueChanged.AddListener(new Action(SuggestToggleCallback)); -#else suggestToggle.onValueChanged.AddListener(SuggestToggleCallback); -#endif void SuggestToggleCallback(bool val) { EnableAutocompletes = val; @@ -395,11 +384,7 @@ The following helper methods are available: // Enable Auto-indent toggle var autoIndentToggleObj = UIFactory.CreateToggle(topBarObj, out Toggle autoIndentToggle, out Text autoIndentToggleText); -#if CPP - autoIndentToggle.onValueChanged.AddListener(new Action(OnIndentChanged)); -#else autoIndentToggle.onValueChanged.AddListener(OnIndentChanged); -#endif void OnIndentChanged(bool val) => EnableAutoIndent = val; autoIndentToggleText.text = "Auto-indent"; @@ -461,11 +446,7 @@ The following helper methods are available: btnText.color = Color.white; // Set compile button callback now that we have the Input Field reference -#if CPP - compileButton.onClick.AddListener(new Action(CompileCallback)); -#else compileButton.onClick.AddListener(CompileCallback); -#endif void CompileCallback() { if (!string.IsNullOrEmpty(inputField.text)) diff --git a/src/Helpers/EventHelper.cs b/src/Helpers/EventHelper.cs new file mode 100644 index 0000000..375ebee --- /dev/null +++ b/src/Helpers/EventHelper.cs @@ -0,0 +1,33 @@ +#if CPP +using System; +using UnityEngine.Events; + +namespace UnityExplorer.Helpers +{ + // Possibly temporary, just so Il2Cpp can do the same style "AddListener" as Mono. + // Just saves me having a preprocessor directive for every single AddListener. + + public static class EventHelper + { + public static void AddListener(this UnityEvent action, Action listener) + { + action.AddListener(listener); + } + + public static void AddListener(this UnityEvent action, Action listener) + { + action.AddListener(listener); + } + + public static void AddListener(this UnityEvent action, Action listener) + { + action.AddListener(listener); + } + + public static void AddListener(this UnityEvent action, Action listener) + { + action.AddListener(listener); + } + } +} +#endif \ No newline at end of file diff --git a/src/Helpers/UnityHelpers.cs b/src/Helpers/UnityHelpers.cs index db08be9..8926c09 100644 --- a/src/Helpers/UnityHelpers.cs +++ b/src/Helpers/UnityHelpers.cs @@ -18,12 +18,27 @@ namespace UnityExplorer.Helpers } } - public static string ActiveSceneName + public static bool IsNullOrDestroyed(this object obj, bool suppressWarning = false) { - get + var unityObj = obj as Object; + if (obj == null) { - return UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; + if (!suppressWarning) + ExplorerCore.LogWarning("The target instance is null!"); + + return true; } + else if (obj is Object) + { + if (!unityObj) + { + if (!suppressWarning) + ExplorerCore.LogWarning("The target UnityEngine.Object was destroyed!"); + + return true; + } + } + return false; } public static string ToStringLong(this Vector3 vec) diff --git a/src/Inspectors/GameObjects/ChildList.cs b/src/Inspectors/GameObjects/ChildList.cs index 2f285c9..8708569 100644 --- a/src/Inspectors/GameObjects/ChildList.cs +++ b/src/Inspectors/GameObjects/ChildList.cs @@ -177,11 +177,7 @@ namespace UnityExplorer.Inspectors.GameObjects mainColors.normalColor = new Color(0.07f, 0.07f, 0.07f); mainColors.highlightedColor = new Color(0.2f, 0.2f, 0.2f, 1); mainBtn.colors = mainColors; -#if CPP - mainBtn.onClick.AddListener(new Action(() => { OnChildListObjectClicked(thisIndex); })); -#else mainBtn.onClick.AddListener(() => { OnChildListObjectClicked(thisIndex); }); -#endif Text mainText = mainButtonObj.GetComponentInChildren(); mainText.alignment = TextAnchor.MiddleLeft; diff --git a/src/Inspectors/GameObjects/ComponentList.cs b/src/Inspectors/GameObjects/ComponentList.cs index 0887545..6d9a3a6 100644 --- a/src/Inspectors/GameObjects/ComponentList.cs +++ b/src/Inspectors/GameObjects/ComponentList.cs @@ -194,11 +194,7 @@ namespace UnityExplorer.Inspectors.GameObjects var checkImg = toggleObj.transform.Find("Background/Checkmark").GetComponent(); checkImg.color = UISyntaxHighlight.Class_Instance.ToColor(); checkImg.color *= 0.66f; -#if CPP - toggle.onValueChanged.AddListener(new Action((bool val) => { OnCompToggleClicked(thisIndex, val); })); -#else toggle.onValueChanged.AddListener((bool val) => { OnCompToggleClicked(thisIndex, val); }); -#endif toggleText.text = ""; s_compToggles.Add(toggle); @@ -215,11 +211,7 @@ namespace UnityExplorer.Inspectors.GameObjects mainColors.normalColor = new Color(0.07f, 0.07f, 0.07f); mainColors.highlightedColor = new Color(0.2f, 0.2f, 0.2f, 1); mainBtn.colors = mainColors; -#if CPP - mainBtn.onClick.AddListener(new Action(() => { OnCompListObjectClicked(thisIndex); })); -#else mainBtn.onClick.AddListener(() => { OnCompListObjectClicked(thisIndex); }); -#endif // Component button text diff --git a/src/Inspectors/GameObjects/GameObjectControls.cs b/src/Inspectors/GameObjects/GameObjectControls.cs index 0b8e238..57aa193 100644 --- a/src/Inspectors/GameObjects/GameObjectControls.cs +++ b/src/Inspectors/GameObjects/GameObjectControls.cs @@ -299,11 +299,7 @@ namespace UnityExplorer.Inspectors.GameObjects contentGroup.childControlWidth = true; // ~~ add hide button callback now that we have scroll reference ~~ -#if CPP - hideButton.onClick.AddListener(new Action(OnHideClicked)); -#else hideButton.onClick.AddListener(OnHideClicked); -#endif void OnHideClicked() { if (hideText.text == "Show") @@ -368,11 +364,7 @@ namespace UnityExplorer.Inspectors.GameObjects var applyButtonObj = UIFactory.CreateButton(setParentGroupObj); var applyButton = applyButtonObj.GetComponent