Unity3d 如何在脚本[Unity 4.6 beta]中修改RectTransform属性
你好,我正在使用Unity 4.6测试版的新UI系统 尝试了不同的代码,查看了文档并四处搜索,但找不到答案 比如说。我有一个图像,我想在运行时更改宽度和高度Unity3d 如何在脚本[Unity 4.6 beta]中修改RectTransform属性,unity3d,Unity3d,你好,我正在使用Unity 4.6测试版的新UI系统 尝试了不同的代码,查看了文档并四处搜索,但找不到答案 比如说。我有一个图像,我想在运行时更改宽度和高度 public GameObject image4; image4.GetComponent<RectTransform>().rect.Set(0,0,100, 300); 这也不行 运行时更改大小的正确方法是什么?你可以用JS或C#给我举个例子,没关系。一天。 我发现了一个可以帮助我们处理新UI系统的扩展。 如果你愿意,你可
public GameObject image4;
image4.GetComponent<RectTransform>().rect.Set(0,0,100, 300);
这也不行
运行时更改大小的正确方法是什么?你可以用JS或C#给我举个例子,没关系。一天。
我发现了一个可以帮助我们处理新UI系统的扩展。
如果你愿意,你可以改进这个扩展
public static class RectTransformExtensions
{
public static void SetDefaultScale(this RectTransform trans) {
trans.localScale = new Vector3(1, 1, 1);
}
public static void SetPivotAndAnchors(this RectTransform trans, Vector2 aVec) {
trans.pivot = aVec;
trans.anchorMin = aVec;
trans.anchorMax = aVec;
}
public static Vector2 GetSize(this RectTransform trans) {
return trans.rect.size;
}
public static float GetWidth(this RectTransform trans) {
return trans.rect.width;
}
public static float GetHeight(this RectTransform trans) {
return trans.rect.height;
}
public static void SetPositionOfPivot(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x, newPos.y, trans.localPosition.z);
}
public static void SetLeftBottomPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y + (trans.pivot.y * trans.rect.height), trans.localPosition.z);
}
public static void SetLeftTopPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x + (trans.pivot.x * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
}
public static void SetRightBottomPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x - ((1f - trans.pivot.x) * trans.rect.width), newPos.y + (trans.pivot.y * trans.rect.height), trans.localPosition.z);
}
public static void SetRightTopPosition(this RectTransform trans, Vector2 newPos) {
trans.localPosition = new Vector3(newPos.x - ((1f - trans.pivot.x) * trans.rect.width), newPos.y - ((1f - trans.pivot.y) * trans.rect.height), trans.localPosition.z);
}
public static void SetSize(this RectTransform trans, Vector2 newSize) {
Vector2 oldSize = trans.rect.size;
Vector2 deltaSize = newSize - oldSize;
trans.offsetMin = trans.offsetMin - new Vector2(deltaSize.x * trans.pivot.x, deltaSize.y * trans.pivot.y);
trans.offsetMax = trans.offsetMax + new Vector2(deltaSize.x * (1f - trans.pivot.x), deltaSize.y * (1f - trans.pivot.y));
}
public static void SetWidth(this RectTransform trans, float newSize) {
SetSize(trans, new Vector2(newSize, trans.rect.size.y));
}
public static void SetHeight(this RectTransform trans, float newSize) {
SetSize(trans, new Vector2(trans.rect.size.x, newSize));
}
}
我在这里找到的源代码:
试着使用类似的东西[在C#中]:
。。。
RectTransform rTrans=(RectTransform)transform.GetComponent();
//设置新的宽度和高度
rTrans.anchoredPosition=新向量2(15200);
...
当更改父级(如实例化后)时,事情变得一团糟,我发现重置一些参数确实有帮助(花了相当多的时间试图解决这个问题):
希望这对其他人有所帮助:)您通常不想直接修改这些值,如果这些值来自已使用正确坐标设置的预制件,甚至更不想直接修改,如果您只想将其正确添加到画布(或其他)下,但它“更改位置”,请执行以下操作:
游戏对象实例;
无效开始(){
GameObject画布=GameObject.Find(“画布”);
if(canvas!=null){
GameObject go=Resources.Load(“MyPrefab”);
if(go!=null){
实例=实例化(go);
instance.GetComponent().SetParent(canvas.GetComponent(),false);
}
}
}
在集合父对象上传递
false
将避免在实例化预置时以奇怪的形式更改rect变换。实际使用以下方法会有所帮助:
GetComponent.sizeDelta=新向量(新大小x,新大小y)
通过设置此属性,我成功地在新GUI中实现了缩放
以下是我自己的缩放代码:
void Start()
{
// Store initial Size and Position;
ZoomedOutSize = new Vector2(GetComponent<RectTransform>().rect.width, GetComponent<RectTransform>().rect.height);
ZoomedOutPos = new Vector2(GetComponent<RectTransform>().localPosition.x, GetComponent<RectTransform>().localPosition.y);
}
void Update()
{
// Calculate the total delta at runtime cause it depends on parent (yah i know it's not optimal to have new vector ecery Update)
ZoomSizeRate = new Vector2(gameObject.GetComponentInParent<ZoomDialog>().MaxSize.x - ZoomedOutSize.x, gameObject.GetComponentInParent<ZoomDialog>().MaxSize.y - ZoomedOutSize.y);
ZoomPosRate = -GetComponent<RectTransform>().localPosition;
// Zoom is the float with range(0,1) 0 - no Zoom, 1 - Fully Zoom to ParentSize;
//Set the size delta and position as Initial + Rate * Zoom
GetComponent<RectTransform>().sizeDelta = new Vector2(ZoomedOutSize.x + ZoomSizeRate.x * Zoom, ZoomedOutSize.y + ZoomSizeRate.y * Zoom);
GetComponent<RectTransform>().localPosition = new Vector2(ZoomedOutPos.x + ZoomPosRate.x * Zoom, ZoomedOutPos.y + ZoomPosRate.y * Zoom);
}
void Start()
{
//储存初始尺寸和位置;
ZoomedOutSize=新矢量2(GetComponent().rect.width,GetComponent().rect.height);
ZoomedOutPos=newvector2(GetComponent().localPosition.x,GetComponent().localPosition.y);
}
无效更新()
{
//计算运行时的总增量,因为它取决于父级(是的,我知道有新的向量ecery更新不是最佳的)
ZoomSizeRate=newVector2(gameObject.getComponentParent().MaxSize.x-ZoomedOutSize.x,gameObject.getComponentParent().MaxSize.y-ZoomedOutSize.y);
ZoomPostRate=-GetComponent().localPosition;
//缩放是浮动,范围为(0,1)0-无缩放,1-完全缩放到ParentSize;
//将大小增量和位置设置为初始+速率*缩放
GetComponent().sizeDelta=新矢量2(ZoomedOutSize.x+zoomizerate.x*Zoom,ZoomedOutSize.y+zoomizerate.y*Zoom);
GetComponent().localPosition=new Vector2(ZoomedOutPos.x+ZoomPosRate.x*Zoom,ZoomedOutPos.y+ZoomPosRate.y*Zoom);
}
嘿,朋友,试试这个来改变UI游戏对象的位置和宽度
如果要使用对象的实例
用这个
leftButton.GetComponent().anchoredPosition=新向量2(-125,-36f);
rightButton.GetComponent().sizeDelta=新向量2(x,y)代码>
如果想要将脚本放在UI对象上,请尝试更改高度和宽度
GetComponent<RectTransform>().anchoredPosition = new Vector2(-125, -36f);
GetComponent<RectTransform>().sizeDelta = new Vector2(x, y)
GetComponent().anchoredPosition=新向量2(-125,-36f);
GetComponent().sizeDelta=新向量2(x,y)
GetComponent().anchoredPosition3D;
它应该可以很好地工作x,y,z是矩形变换的PosX,PosY和PosZ
可以使用Vector3存储值
Vector3 values = new Vector3();
public GameObject rect_obj;
//Use this if script is on that recttransform component
values = this.transform.GetComponent<RectTransform>().anchoredPosition3D;
//else
values = rect_obj.transform.GetComponent<RectTransform>().anchoredPosition3D;
Vector3值=新的Vector3();
公共游戏对象rect_obj;
//如果脚本位于该recttransform组件上,请使用此选项
值=this.transform.GetComponent().anchoredPosition3D;
//否则
value=rect_obj.transform.GetComponent().anchoredPosition3D;
我不知道为什么setter不能按预期工作,但您可以这样做:image4.GetComponent().localScale=newvector3(1,15,1);我看不到你的答案对已经给出的答案有任何附加价值。告诉我你想知道什么??如果没有,请告诉我,欢迎光临,因为性能原因,这是不好的。请让专家来回答。
GameObject instance;
void Start () {
GameObject canvas = GameObject.Find ("Canvas");
if (canvas != null) {
GameObject go = Resources.Load<GameObject>("MyPrefab");
if(go != null){
instance = Instantiate(go);
instance.GetComponent<Transform>().SetParent(canvas.GetComponent<Transform>(), false);
}
}
}
void Start()
{
// Store initial Size and Position;
ZoomedOutSize = new Vector2(GetComponent<RectTransform>().rect.width, GetComponent<RectTransform>().rect.height);
ZoomedOutPos = new Vector2(GetComponent<RectTransform>().localPosition.x, GetComponent<RectTransform>().localPosition.y);
}
void Update()
{
// Calculate the total delta at runtime cause it depends on parent (yah i know it's not optimal to have new vector ecery Update)
ZoomSizeRate = new Vector2(gameObject.GetComponentInParent<ZoomDialog>().MaxSize.x - ZoomedOutSize.x, gameObject.GetComponentInParent<ZoomDialog>().MaxSize.y - ZoomedOutSize.y);
ZoomPosRate = -GetComponent<RectTransform>().localPosition;
// Zoom is the float with range(0,1) 0 - no Zoom, 1 - Fully Zoom to ParentSize;
//Set the size delta and position as Initial + Rate * Zoom
GetComponent<RectTransform>().sizeDelta = new Vector2(ZoomedOutSize.x + ZoomSizeRate.x * Zoom, ZoomedOutSize.y + ZoomSizeRate.y * Zoom);
GetComponent<RectTransform>().localPosition = new Vector2(ZoomedOutPos.x + ZoomPosRate.x * Zoom, ZoomedOutPos.y + ZoomPosRate.y * Zoom);
}
GetComponent<RectTransform>().anchoredPosition = new Vector2(-125, -36f);
GetComponent<RectTransform>().sizeDelta = new Vector2(x, y)
Vector3 values = new Vector3();
public GameObject rect_obj;
//Use this if script is on that recttransform component
values = this.transform.GetComponent<RectTransform>().anchoredPosition3D;
//else
values = rect_obj.transform.GetComponent<RectTransform>().anchoredPosition3D;