2D Toolkit Forum

2D Toolkit => Support => Topic started by: akitsu91 on July 25, 2014, 05:45:43 pm

Title: Add Buttons at runtime to List
Post by: akitsu91 on July 25, 2014, 05:45:43 pm
So I am making Inventory System for my game. I try add the runtime buttons to List<tk2dUIItem>, but It won't register events! Rest of code works well but I just can't add new buttons to the List

Here is what is in void OnEnable / void OnDisable:

Code: [Select]
   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; }
        }
    }
Title: Re: Add Buttons at runtime to List
Post by: unikronsoftware on July 25, 2014, 06:40:52 pm
Are you adding the events when you adding the items to the list? OnEnalbe is a unity function that is called ONCE at startup.
Title: Re: Add Buttons at runtime to List
Post by: akitsu91 on July 25, 2014, 06:43:30 pm
I'm am just adding buttons to the list at the moment.
Title: Re: Add Buttons at runtime to List
Post by: unikronsoftware on July 25, 2014, 06:45:19 pm
Well then the events wont be bound. You'll need to bind the appropriate events when you add things to the list, otherwise the function wont get called.
Title: Re: Add Buttons at runtime to List
Post by: akitsu91 on July 25, 2014, 06:48:24 pm
I don't understand what you mean? If I add the buttons the list it won't loop those newly added buttons at the runtime? Only those which are already at the when OnEnable?
Title: Re: Add Buttons at runtime to List
Post by: unikronsoftware on July 25, 2014, 06:59:25 pm
OnEnable is called at game startup. When you add your buttons from code, OnEnable wont run again, and your events for the new buttons wont be bound.
Title: Re: Add Buttons at runtime to List
Post by: akitsu91 on July 25, 2014, 07:01:15 pm
So how iI should handle this? little example code would be nice if you can provide :)
Title: Re: Add Buttons at runtime to List
Post by: unikronsoftware on July 25, 2014, 07:04:55 pm
No, as I don't know how you're adding the buttons at runtime or how you're binding the buttons. In fact the lines of code you posted tell me absolutely nothing about how anything works in your project.
Title: Re: Add Buttons at runtime to List
Post by: akitsu91 on July 25, 2014, 07:36:15 pm
Here is code which I'm trying to do pretty much. I hope this makes easier understand problem I have :)

Code: [Select]
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);
        }
    }
}
Title: Re: Add Buttons at runtime to List
Post by: akitsu91 on July 25, 2014, 09:34:36 pm
Hello I think I got it work. Is this what I should do to the subscribe button to my method? And i'm sorry to asking stupid questions all the time :)

Code: [Select]
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);
        }
    }
}
Title: Re: Add Buttons at runtime to List
Post by: unikronsoftware on July 26, 2014, 09:35:01 am
I still dont get it, why aren't you just binding this directly?
Code: [Select]
        // Add new button to the list ->
        if (Input.GetKeyDown(KeyCode.W))
        {
            tk2dUIItem _item = _runtimeButton;
            _item.OnClickUIItem += ClickButton;
            _sceneButtons.Add(_item);
        }
Title: Re: Add Buttons at runtime to List
Post by: akitsu91 on July 26, 2014, 12:21:45 pm
Thanks man pointing that out!