46 lines
1.2 KiB
C#
46 lines
1.2 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class OneLinkTest : MonoBehaviour
|
|
{
|
|
[SerializeField] private GameObject btnContainer;
|
|
|
|
[SerializeField] private GameObject btn;
|
|
|
|
[SerializeField] private Text message;
|
|
|
|
void Start()
|
|
{
|
|
var aFInit = new AFInit();
|
|
|
|
aFInit.text = message;
|
|
// Get funcs methods witch has Btn attribute
|
|
var methods = aFInit.GetType().GetMethods();
|
|
foreach (var method in methods)
|
|
{
|
|
var btnAttr = method.GetCustomAttribute<BtnAttribute>();
|
|
if (btnAttr == null)
|
|
{
|
|
continue;
|
|
}
|
|
var btnObj = Instantiate(btn, btnContainer.transform);
|
|
btnObj.GetComponentInChildren<Text>().text = btnAttr.btnName;
|
|
btnObj.GetComponent<Button>().onClick.AddListener(() => method.Invoke(aFInit, null));
|
|
btnObj.SetActive(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
|
|
public class BtnAttribute : Attribute
|
|
{
|
|
public string btnName;
|
|
public BtnAttribute(string btnName)
|
|
{
|
|
this.btnName = btnName;
|
|
}
|
|
}
|