【问题标题】:How do i get the text of a UI element to move in Unity 4.6+?如何让 UI 元素的文本在 Unity 4.6+ 中移动?
【发布时间】:2015-05-19 00:58:08
【问题描述】:

当玩家接近一个物品时,我希望显示它的一些属性,但我不希望这些信息掩盖玩家。我有以下代码不起作用。

t = GetComponentInChildren<Text> ();

void OnTriggerEnter(Collider col){
    if (col.gameObject.tag.Equals ("Player")) {
        playerInRange = true;
        col.GetComponent<Controller>().itemsInRange.Add(this.gameObject);
        GetComponentInChildren<Canvas> ().enabled = true;
    }
    if (player.transform.position.x < this.transform.position.x) {
        Debug.Log ("On yer right!");
        t.rectTransform.position.Set(this.transform.position.x+50,  this.transform.position.z, this.transform.position.z);
    }
    if (player.transform.position.x > this.transform.position.x) {
        t.rectTransform.position.Set(this.transform.position.x-50,  this.transform.position.z, this.transform.position.z);
    }
}

Debug.Log 出现了,所以所有条件都满足了,但是文本没有移动。有人有什么想法吗?

【问题讨论】:

    标签: user-interface unity3d


    【解决方案1】:

    问题是 tranform.position 的 getter 返回位置 Vector3 的“副本”,因此调用 Set 只会更改该 vector3 的“副本”,而不是 rect 变换的位置。

    这是因为 Vector3 是一个结构体而不是一个类,因此不是通过引用传递的。 https://msdn.microsoft.com/en-us/library/ms173109.aspx

    所以我不会调用 set 而是将新向量 3 分配给位置变量

    t.rectTransform.position = new Vector3(transform.position.x-50, transform.position.y, transform.position.z);

    【讨论】:

    • 赢家鸡肉晚餐!谢谢
    【解决方案2】:

    如果您正在调试,您能否验证您正在显示的位置是您期望的位置,目前“Set”方法的第二个和第三个参数是:

     this.transform.position.z
    

    中间的不应该是 position.y 吗?

    【讨论】:

    • 其中一个.transform.position.z 应该是y,但这不会导致这个问题。所有职位字段都没有改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多