43 lines
1.4 KiB
C#
43 lines
1.4 KiB
C#
using NEG.UI.UnityUi.Buttons;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
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();
|
|
}
|
|
|
|
}
|
|
}
|