Hello Guest

Show Posts

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.


Messages - akitsu91

Pages: [1]
1
Support / Re: Add Buttons at runtime to List
« on: July 26, 2014, 12:21:45 pm »
Thanks man pointing that out!

2
Support / Re: Add Buttons at runtime to List
« 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);
        }
    }
}

3
Support / Re: Add Buttons at runtime to List
« 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);
        }
    }
}

4
Support / Re: Add Buttons at runtime to List
« on: July 25, 2014, 07:01:15 pm »
So how iI should handle this? little example code would be nice if you can provide :)

5
Support / Re: Add Buttons at runtime to List
« 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?

6
Support / Re: Add Buttons at runtime to List
« on: July 25, 2014, 06:43:30 pm »
I'm am just adding buttons to the list at the moment.

7
Support / 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; }
        }
    }

Pages: [1]