using NEG.UI.UnityUi.Buttons; using UnityEditor; using UnityEngine; using UnityEngine.UI; namespace NEG.UI.UnityUi.Editor { public static class ButtonsExtensions { [MenuItem("CONTEXT/Button/Change To Navigation Button")] public static void ChangeScript(MenuCommand command) { if (command.context == null) return; var button = command.context as Button; var go = new GameObject(); var sourceScriptAsset = MonoScript.FromMonoBehaviour(go.AddComponent()); string relativePath = AssetDatabase.GetAssetPath(sourceScriptAsset); Object.DestroyImmediate(go); var chosenTextAsset = AssetDatabase.LoadAssetAtPath(relativePath); if (chosenTextAsset == null) { Debug.LogWarning($"Selected script couldn't be loaded ({relativePath})"); return; } Undo.RegisterCompleteObjectUndo(command.context, "Changing component script"); var so = new SerializedObject(button); var scriptProperty = so.FindProperty("m_Script"); so.Update(); scriptProperty.objectReferenceValue = chosenTextAsset; so.ApplyModifiedProperties(); } } }