DF-GUI动态创建按钮并关联事件

在官网代码基础上增加,通过委托关联按钮处理代码:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[AddComponentMenu( "Daikon Forge/Examples/Add-Remove Controls/Create Child Control" )]
public class DemoCreateChildControls : MonoBehaviour 
{

	public dfScrollPanel target;

	// These colors are used to visually differentiate different
	// groups of controls just to make sure they are added in 
	// the right order
	private int colorNum = 0;
	private Color32[] colors = new Color32[]
	{
		Color.white,
		Color.red,
		Color.green,
		Color.black
	};

	public void Start()
	{
		// If not already assigned, grab a reference to the 
		// Scroll Panel that will contain the dynamically-
		// created controls.
		if( target == null )
		{
			this.target = GetComponent<dfScrollPanel>();
		}
	}

	public void OnClick()
	{

		for( int i = 0; i < 10; i++ )
		{

			// Creates the new control and adds it to the target
			// Scroll Panel. Returns a reference to the new
			// control.
			var button = target.AddControl<dfButton>();

			// Set whatever properties the control needs after creation
			button.NormalBackgroundColor = colors[ colorNum % colors.Length ];
			button.BackgroundSprite = "button-normal";
			button.Text = string.Format( "Button {0}", button.ZOrder );
			button.Anchor = dfAnchorStyle.Left | dfAnchorStyle.Right;
			button.Width = target.Width - target.ScrollPadding.horizontal;
			button.Click += ButtonClick;

		}

		colorNum += 1;

	}

	public void OnDoubleClick()
	{
		OnClick();
	}

	void ButtonClick(dfControl control, dfMouseEventArgs mouseEvent)
	{
		Debug.Log(((dfButton)control).Text + " clicked");
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值