我正在编写移动 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
边界)时它才会响应,没有任何反应。
在这种情况下我能做什么?感谢您的回复。