Hello Guest

Author Topic: Add Buttons at runtime to List  (Read 6610 times)

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Add Buttons at runtime to List
« 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; }
        }
    }

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Add Buttons at runtime to List
« Reply #1 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.

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Add Buttons at runtime to List
« Reply #2 on: July 25, 2014, 06:43:30 pm »
I'm am just adding buttons to the list at the moment.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Add Buttons at runtime to List
« Reply #3 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.

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Add Buttons at runtime to List
« Reply #4 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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Add Buttons at runtime to List
« Reply #5 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.

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Add Buttons at runtime to List
« Reply #6 on: July 25, 2014, 07:01:15 pm »
So how iI should handle this? little example code would be nice if you can provide :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Add Buttons at runtime to List
« Reply #7 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.

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Add Buttons at runtime to List
« Reply #8 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);
        }
    }
}

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Add Buttons at runtime to List
« Reply #9 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);
        }
    }
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Add Buttons at runtime to List
« Reply #10 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);
        }

akitsu91

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Add Buttons at runtime to List
« Reply #11 on: July 26, 2014, 12:21:45 pm »
Thanks man pointing that out!