johnhattan

Component Button that copies styles

Oct 7th, 2017
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. [RequireComponent(typeof(Button))]
  5. [ExecuteInEditMode]
  6.  
  7. public class ButtonCopyStyleFrom : _MonoBehaviour
  8. {
  9.     public Button ButtonSource;
  10.     public Image ImageSource;
  11.     public bool CopyColors = true;
  12.     public bool CopyImage = true;
  13.  
  14.     // note that this is only done when the button is enabled. If you want it more dynamic, trigger off a different event
  15.     public void OnEnable()
  16.     {
  17.         if (ButtonSource != null)
  18.         {
  19.             if ((ImageSource == null) && CopyImage)
  20.                 ImageSource = ButtonSource.GetComponent<Image>();
  21.  
  22.             GetComponent<Button>().colors = ButtonSource.colors;
  23.         }
  24.  
  25.         if ((ImageSource != null) && CopyImage)
  26.         {
  27.             GetComponent<Image>().sprite = ImageSource.sprite;
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment