Unity | 微信小游戏中 WX.CreateUserInfoButton 的使用

274 阅读1分钟
  1. 将脚本挂载在 Unity 按钮上
public class GetWechatUserInfoButton : MonoBehaviour
{
    public Rect GetScreenPosition()
    {
        var rectTransform = gameObject.GetComponent<RectTransform>();
        // 获取 RectTransform 的四个角的世界坐标
Vector3[] worldCorners = new Vector3[4];
        rectTransform.GetWorldCorners(worldCorners);

        // 创建屏幕矩形
var screenRect = new Rect(
                worldCorners[0].x,
                worldCorners[0].y,
                worldCorners[2].x - worldCorners[0].x,
                worldCorners[2].y - worldCorners[0].y);

        Debug.Log($"Screen Rect: {screenRect}");
        return screenRect;
    }
}

  1. 调用微信提供的绘制按钮的方法
var wechatButton = getWechatUserInfoButton.GetComponent<GetWechatUserInfoButton>();
var rect = wechatButton.GetScreenPosition();
var userInfoButton =
WX.CreateUserInfoButton((int)rect.x, Screen.height - (int)rect.y -  (int)rect.height, (int)rect.width, (int)rect.height, "", true);

这个时候微信绘制的按钮就与 unity 按钮区域重合啦!