Neg_Utils/NEG/UI/UnityUi/Editor/ButtonsExtensions.cs
2024-02-12 21:26:24 +01:00

39 lines
1.3 KiB
C#

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<CustomNavigationButton>());
string relativePath = AssetDatabase.GetAssetPath(sourceScriptAsset);
Object.DestroyImmediate(go);
var chosenTextAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(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();
}
}
}