0

我正在编写移动 Unity 应用程序。我想为一个场景添加关闭 (X) 图标。并在图标周围添加一些填充以使可点击区域更大(因为图标不是那么大)。

我写了下一个代码:

// create button
GameObject buttonContainerObject = new GameObject("XIconContainer", typeof(Button), typeof(RectTransform));
buttonContainerObject.GetComponent<Button>().onClick.AddListener(onClickAction);

// set button location and size
buttonContainerObject.transform.SetParent(canvas.transform);
buttonContainerObject.GetComponent<RectTransform>().sizeDelta = new Vector2(area_width, area_hight)
buttonContainerObject.transform.position = some_position;

// create image
GameObject buttonIconObject = new GameObject("XIconImage", typeof(Image));
buttonIconObject.GetComponent<Image>().sprite = xIconSprite;

// set image location and size
buttonIconObject.transform.SetParent(buttonContainerObject.transform);
buttonIconObject.transform.localPosition = new Vector3(0f, 0f); // in the center of button

但是当我构建项目并点击按钮时,只有当我点击图像、外部图像(和内部buttonContainerObject边界)时它才会响应,没有任何反应。

在这种情况下我能做什么?感谢您的回复。

4

1 回答 1

0

您可以制作一个大按钮并将其矩形变换更改为您想要的大小,并将您的图标作为对象的子对象。

于 2017-03-26T15:45:54.353 回答