【问题标题】:Clickable area around icon in UnityUnity中图标周围的可点击区域
【发布时间】:2017-03-26 23:45:54
【问题描述】:

我正在编写移动 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 边界内)时它才会响应。

在这种情况下我该怎么办?感谢您的回复。

【问题讨论】:

    标签: unity3d clickable-image


    【解决方案1】:

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

    【讨论】:

    • 你读过我的代码吗?我已经这样做了,但它没有工作。
    • 在构建之前它是否有效?如果代码不起作用,我仍然会尝试自己在检查器中执行此操作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 2011-05-06
    • 2023-01-31
    • 1970-01-01
    • 2013-05-08
    • 2013-05-16
    • 2016-07-10
    相关资源
    最近更新 更多