johnhattan icon

Component Button that copies styles

johnhattan | PRO | 10/07/17 08:09:07 PM UTC | 0 ⭐ | 261 👁️ | Never ⏰ | []
C# |

730 B

|

None

|

0 👍

/

0 👎

using UnityEngine;
using UnityEngine.UI;
 
[RequireComponent(typeof(Button))]
[ExecuteInEditMode]
 
public class ButtonCopyStyleFrom : _MonoBehaviour
{
    public Button ButtonSource;
    public Image ImageSource;
    public bool CopyColors = true;
    public bool CopyImage = true;
 
    // note that this is only done when the button is enabled. If you want it more dynamic, trigger off a different event
    public void OnEnable()
    {
        if (ButtonSource != null)
        {
            if ((ImageSource == null) && CopyImage)
                ImageSource = ButtonSource.GetComponent<Image>();
 
            GetComponent<Button>().colors = ButtonSource.colors;
        }
 
        if ((ImageSource != null) && CopyImage)
        {
            GetComponent<Image>().sprite = ImageSource.sprite;
        }
    }
}

Comments