teleias

extended button

Jan 15th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. //just expirimenting.
  7.  
  8. public abstract class EnumButton : MonoBehaviour
  9. {
  10.  
  11. }
  12. public interface IFooButtonListener
  13. {
  14.     void OnClick(Foo foo);
  15. }
  16. public enum Foo{bar, age};
  17. [RequireComponent(typeof(Button))]
  18. public class FooButton : EnumButton
  19. {
  20.  
  21.     [SerializeField] public Foo foo = Foo.bar;
  22.     public void Init(IFooButtonListener listener)
  23.     {
  24.         gameObject.GetComponent<Button>().
  25.             onClick.AddListener(
  26.                 ()=>listener.OnClick(foo)
  27.                 );
  28.     }
  29. }
  30.  
  31. public class Usage : MonoBehaviour, IFooButtonListener
  32. {
  33.     [SerializeField] List<FooButton> buttons;
  34.     public void Awake()
  35.     {
  36.         foreach(var b in buttons)
  37.         {
  38.             b.Init(this);
  39.         }
  40.     }
  41.  
  42.     public void OnClick(Foo foo)
  43.     {
  44.         Debug.Log("button click from "+foo);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment