Unity3d 如何通过代码更改UI按钮的宽度?

Unity3d 如何通过代码更改UI按钮的宽度?,unity3d,Unity3d,我安装了unity 5。我不明白如何用c代码更改按钮的宽度。我还需要更改字体大小。如果使用OnGui。可以使用向量3或Rect 但对于4.6 Unity,它提供了新的GUI用户界面。您必须操纵锚点。而不是实际的缩放 新的GUI将始终依赖于其父级,最常见的是画布作为主父级 确保使用的命名空间 UnityEngine.UI 否则,您将无法拥有控制它所需的类 对于新UI中的按钮,您将需要额外的工作。原因Button类不是从需要修改的RectTransform类继承的 首先参考您要修改的按钮 publi

我安装了unity 5。我不明白如何用c代码更改按钮的宽度。我还需要更改字体大小。

如果使用OnGui。可以使用向量3或Rect

但对于4.6 Unity,它提供了新的GUI用户界面。您必须操纵锚点。而不是实际的缩放

新的GUI将始终依赖于其父级,最常见的是画布作为主父级

确保使用的命名空间 UnityEngine.UI

否则,您将无法拥有控制它所需的类

对于新UI中的按钮,您将需要额外的工作。原因Button类不是从需要修改的RectTransform类继承的

首先参考您要修改的按钮

public Button myBtn;
//Drag the button in Unity3d in this script.
接下来我们访问它的组件RectTransform,因为它应用于游戏对象,但它不是在Button类上继承的

myBtn.GetComponent<RectTransform>().anchorMax = new Vector2(30.0f, 30.0f);
myBtn.GetComponent<RectTransform>().anchorMin = new Vector2(10.0f, 10.0f);
希望这能帮助您理解新的UI

Text myText;
// Drag the Text new GUI UI in the inspector.

myText.rectTransform.anchorMin = new Vector2(10.0f,10.0f);
// Because the Text class is a subclass of RectTransform or at lease there is a an object class rectTransform that is pointing to it.


// THIS is for the Font size.
myText.fontSize = 30;