Skip to content

Commit e3dea77

Browse files
author
uhu
committed
feat: add menuRect mask
1 parent cf4db31 commit e3dea77

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Demo/API/Assets/Scripts/Demo.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ void Start()
3333
txtUserInfo.text = $"nickName:{userInfoButonRet.userInfo.nickName}, avartar:{userInfoButonRet.userInfo.avatarUrl}";
3434
});
3535
Debug.Log("infoButton Created");
36+
37+
this.AddMenuRectMask();
3638

3739
// fallbackFont作为旧版本微信或者无法获得系统字体文件时的备选CDN URL
3840
var fallbackFont = Application.streamingAssetsPath + "/Fz.ttf";
@@ -46,6 +48,48 @@ void Start()
4648
});
4749
}
4850

51+
public void AddMenuRectMask() {
52+
var systemInfo = WX.GetSystemInfoSync();
53+
54+
// 获取Canvas组件
55+
Canvas canvas = GetComponent<Canvas>();
56+
57+
// 如果没有找到Canvas组件,打印错误并返回
58+
if (canvas == null)
59+
{
60+
Debug.LogError("Canvas component not found.");
61+
return;
62+
}
63+
64+
// 获取Canvas的宽度
65+
float canvasWidth = canvas.GetComponent<RectTransform>().rect.width;
66+
67+
// 创建一个新的Image对象
68+
GameObject blackSquare = new GameObject("BlackSquare", typeof(Image));
69+
70+
// 设置Image对象的父对象为Canvas
71+
blackSquare.transform.SetParent(canvas.transform, false);
72+
73+
// 获取Image对象的RectTransform组件
74+
RectTransform rectTransform = blackSquare.GetComponent<RectTransform>();
75+
76+
// 获取小游戏系统按钮的位置
77+
var menuRect = WX.GetMenuButtonBoundingClientRect();
78+
79+
// 转换成当前场景屏幕比例的高度
80+
var safeButtom = (int)(menuRect.bottom * 1080f / systemInfo.screenWidth);
81+
82+
// 设置RectTransform的属性
83+
rectTransform.anchorMin = new Vector2(0, 1);
84+
rectTransform.anchorMax = new Vector2(0, 1);
85+
rectTransform.pivot = new Vector2(0, 1);
86+
rectTransform.sizeDelta = new Vector2(canvasWidth, safeButtom);
87+
88+
// 设置Image组件的颜色为黑色
89+
Image image = blackSquare.GetComponent<Image>();
90+
image.color = Color.black;
91+
}
92+
4993
public void OnEnterOptionsClick()
5094
{
5195
var options = WX.GetEnterOptionsSync();

0 commit comments

Comments
 (0)