能解释一下OffsetUI中enable分支中的这段代码吗?
if (_enable) { float rx = Screen.width / _original.x; float ry = Screen.height / _original.y; if (rx < ry) { float r = rx; float cha = (1f - r) * Screen.height; float x = (1f - r) / 2 * Screen.width; float y = cha / 2f; this.gameObject.transform.localScale = new Vector3 (r, r, 1); this.gameObject.transform.localPosition = new Vector3 (x, y, 0); _rect.sizeDelta = new Vector2 ((_original.x - Screen.width), cha / r); } else { float r = ry; float cha = (1f - r) * Screen.width; float y = (1 - r) / 2 * Screen.height; float x = cha / 2f; this.gameObject.transform.localScale = new Vector3 (r, r, 1); this.gameObject.transform.localPosition = new Vector3 (x, y, 0); _rect.sizeDelta = new Vector2 (cha / r, (_original.y - Screen.height)); } }
else中的代码应该是一个道理,为了让代码更完整一点, 就传上来了。
先说一下我自己的理解:
this.gameObject.transform.localScale = new Vector3 (r, r, 1);//这句应该是等比例缩放
this.gameObject.transform.localPosition = new Vector3 (x, y, 0);//这句应该是居中(结合上面x和y的值来看)
_rect.sizeDelta = new Vector2 ((_original.x - Screen.width), cha / r);//这句应该是变化_rect的大小, 但是不知道Vector2构造函数中的值有什么讲究
另外, _rect的大小需要单独做变化吗?难道不是随着父元素的值一起变化的吗?