// // /*=============================================================================== // // Copyright (C) 2025 PhantomsXR Ltd. All Rights Reserved. // // // // This file is part of the Phantom.XRMOD.UnityFusion.Editor. // // // // The QuestPlatform cannot be copied, distributed, or made available to // // third-parties for commercial purposes without written permission of PhantomsXR Ltd. // // // // Contact info@phantomsxr.com for licensing requests. // // ===============================================================================*/ using System.Collections.Generic; using System.Threading.Tasks; using Phantom.XRMOD.ActionNotification.Runtime; using Phantom.XRMOD.Core.Runtime; using Phantom.XRMOD.UnityFusion.Runtime.CodeHook; using UnityEditor; using UnityEngine; namespace Phantom.XRMOD.Runtime.Editor { public partial class MonoBinderEditor { [InitializeOnLoadMethod] private static void RegisterConvertAction() { ActionNotificationCenter.DefaultCenter.AddAsyncObserver(ConvertToExecutor, nameof(ConvertToMonoBinderBatch)); } private static async Task ConvertToExecutor(BaseNotificationData _baseNotification) { if (_baseNotification.ObjectData is GameObject tmp_GameObject) await ConvertToMonoBinderBatch(tmp_GameObject); return Task.CompletedTask; } [MenuItem("Tools/XR-MOD/Tools/Convert To MonoBinder (Batch)", priority = 99)] private static async Task ConvertToMonoBinderBatch() { var tmp_SelectedObj = Selection.activeObject; if (tmp_SelectedObj == null) return; var tmp_PrefabOrGo = tmp_SelectedObj as GameObject; if (!EditorUtility.DisplayDialog("Convert to MonoBinder", "Convert to MonoBinder will remove this script! Are you sure?", "Ok", "Cancel")) return; await ConvertToMonoBinderBatch(tmp_PrefabOrGo); } public static async Task ConvertToMonoBinderBatch(GameObject _selectedObj) { var tmp_XRMODBehaviours = _selectedObj?.GetComponentsInChildren(); if (tmp_XRMODBehaviours == null || tmp_XRMODBehaviours.Length <= 0) return; foreach (XRMODBehaviour tmp_Behaviour in tmp_XRMODBehaviours) { if (!tmp_Behaviour.gameObject.TryGetComponent(out var tmp_MonoBinder)) { tmp_MonoBinder = tmp_Behaviour.gameObject.AddComponent(); } var tmp_MonoType = tmp_Behaviour.GetType(); tmp_MonoBinder.ScriptList ??= new List(); var tmp_IndexOfScript = tmp_MonoBinder.ScriptList.FindIndex(_data => _data.ClassName.Equals(tmp_MonoType.Name)); if (tmp_IndexOfScript >= 0) { // EditorUtility.DisplayDialog("Error", $"Script {tmp_MonoType.Name} is invalid!", "OK"); tmp_MonoBinder.ScriptList.RemoveAt(tmp_IndexOfScript); tmp_MonoBinder.ScriptList.Insert(tmp_IndexOfScript, new MonoData() { ClassName = tmp_MonoType.Name, ClassNamespace = tmp_MonoType.Namespace }); } else { tmp_MonoBinder.ScriptList.Add(new MonoData() { ClassName = tmp_MonoType.Name, ClassNamespace = tmp_MonoType.Namespace }); } TARGET_MONO = tmp_Behaviour; TARGET_MONO.enabled = true; await GrabFieldsType(tmp_MonoBinder); tmp_Behaviour.enabled = false; } } [MenuItem("Tools/XR-MOD/Tools/Remove All MonoBinder (Batch)", priority = 98)] private static void RemoveAllMonoBinderBatch() { var tmp_SelectedObj = Selection.activeObject; if (tmp_SelectedObj == null) return; var tmp_MonoBinders = (tmp_SelectedObj as GameObject)?.GetComponentsInChildren(); if (tmp_MonoBinders == null || tmp_MonoBinders.Length <= 0) return; if (!EditorUtility.DisplayDialog("Remove All MonoBinders", "It will remove all MonoBinder scripts! Are you sure?", "Ok", "Cancel")) return; for (int tmp_Idx = 0; tmp_Idx < tmp_MonoBinders.Length; tmp_Idx++) { if (tmp_MonoBinders[tmp_Idx].gameObject.TryGetComponent(out var tmp_Behaviour)) { tmp_Behaviour.enabled = true; } DestroyImmediate(tmp_MonoBinders[tmp_Idx], true); } } } }