首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一,C#,2D :如何将UI画布项(按钮)位置设置为与另一个游戏对象相同的位置?

统一,C#,2D :如何将UI画布项(按钮)位置设置为与另一个游戏对象相同的位置?
EN

Stack Overflow用户
提问于 2021-10-17 05:01:26
回答 1查看 789关注 0票数 0

我试图通过代码将一个按钮显示在与方框相同的位置,这样当调用该函数时,按钮游戏对象就会移动到菜单框游戏对象所在的位置。

代码语言:javascript
复制
private RectTransform Bpos; 

// this var holds the box sprite
    public GameObject menu;

// this var hold the button game object
    public GameObject button;

// vector to hold position
    Vector3 pos;

void Start(){
// sets vector to box position
       pos = menu.transform.position;

       //declairs button recttransform
            Bpos = button.GetComponent<RectTransform>();

       // sets button recttransformto pos vector
       button.Bpos = pos;

}```
i am currently getting the error at line (button,Bpos = pos;)

 error CS1061: 'GameObject' does not contain a definition for 'Bpos' and no accessible extension method 'Bpos' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)
for the life of me i cant get this to work 
EN

回答 1

Stack Overflow用户

发布于 2021-10-17 08:52:33

UI使用anchoredPosition而不是transform.position。您需要将按钮的anchoredPosition设置为菜单的anchoredPosition。您可以通过访问anchoredPosition获得RectTransform。我会这样写:

代码语言:javascript
复制
void Start(){
   // get anchoredPosition of menu and save it in pos
   pos = menu.GetComponent<RectTransform>().anchoredPosition;

   // set anchoredPosition of the button to pos (anchoredPosition of menu)
   button.GetComponent<RectTransform>().anchoredPosition = pos

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69601445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档