1
Support / Re: Add Buttons at runtime to List
« on: July 26, 2014, 12:21:45 pm »
Thanks man pointing that out!
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Reflection;
public class AddingButtonSample : MonoBehaviour
{
public AddingButtonSample smp;
// List of buttons
public List<tk2dUIItem> _sceneButtons;
// Button which will be added at the runtime
public tk2dUIItem _runtimeButton;
void Start ()
{
// A New list of buttons -> we add here couple buttons for testing
_sceneButtons = new List<tk2dUIItem>();
}
void OnEnable()
{
foreach (tk2dUIItem _item in _sceneButtons)
{
if (_item != null) { _item.OnClickUIItem += ClickButton; }
}
}
void OnDisable()
{
foreach (tk2dUIItem _item in _sceneButtons)
{
if (_item != null) { _item.OnClickUIItem -= ClickButton; }
}
}
// Click button -> this won't be caleed if button is added to list at the run time
public static void ClickButton(tk2dUIItem _item)
{
Debug.Log("You pressed button " + _item.name);
}
void Update ()
{
// Add new button to the list ->
if (Input.GetKeyDown(KeyCode.W))
{
tk2dUIItem _item = _runtimeButton;
EventInfo _eventInfo = _item.GetType().GetEvent("OnClickUIItem");
MethodInfo _methodInfo = smp.GetType().GetMethod("ClickButton");
Delegate _handler = Delegate.CreateDelegate(_eventInfo.EventHandlerType, _methodInfo);
_eventInfo.AddEventHandler(_item, _handler);
_sceneButtons.Add(_item);
}
}
}

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AddingButtonSample : MonoBehaviour
{
// List of buttons
public List<tk2dUIItem> _sceneButtons;
// Button which will be added at the runtime
public tk2dUIItem _runtimeButton;
void Start ()
{
// A New list of buttons -> we add here couple buttons for testing
_sceneButtons = new List<tk2dUIItem>();
}
void OnEnable()
{
foreach (tk2dUIItem _item in _sceneButtons)
{
if (_item != null) { _item.OnClickUIItem += ClickButton; }
}
}
void OnDisable()
{
foreach (tk2dUIItem _item in _sceneButtons)
{
if (_item != null) { _item.OnClickUIItem -= ClickButton; }
}
}
// Click button -> this won't be caleed if button is added to list at the run time
void ClickButton(tk2dUIItem _item)
{
Debug.Log("You pressed button " + _item.name);
}
void Update ()
{
// Add new button to the list ->
if (Input.GetKeyDown(KeyCode.W))
{
_sceneButtons.Add(_runtimeButton);
}
}
}
void OnEnable()
{
foreach(tk2dUIItem _item in _itemSlotBtns)
{
if (_item != null) { _item.OnReleaseUIItem += DragItemInPlayerInventory; }
}
}
void OnDisable()
{
foreach (tk2dUIItem _item in _itemSlotBtns)
{
if (_item != null) { _item.OnReleaseUIItem -= DragItemInPlayerInventory; }
}
}