我有4个带有碰撞器的子画面,我想自动调整位置并将它们均匀缩放到屏幕底部,这样它们就不会越过圈并且完全不在屏幕之外。我不能在画布上做到这一点,它需要作为gameObjects完成。
我还试图根据外观将每个Sprits高度设置为1 / 4th-1 / 5th,这就是为什么代码在下面被4除。
如何使它们并排放置在底部?
public class AutoPosition : MonoBehaviour {
public Sprite [] goals;
public float width = Screen.width / 4;
public float height = Screen.height / 4;
// Use this for initialization
void Start () {
for (int i = 0; i < goals.Length; i++) {
goals[i]
}
}
您可以将SpriteRender用于图像。并将它们放置在父母内部GameObject
。仅简单地GameObject
正确缩放和定位一个父对象就足够了(类似于画布,但具有常规Transform
组件)。
public class applySize : MonoBehaviour
{
private void Apply()
{
// Get the main camera position
var cameraPosition = Camera.main.transform.position;
// This makes the parent GameObject "fit the screen size"
float height;
if (Camera.main.orthographic)
{
// Camera projection is orthographic
height = 2 * Camera.main.orthographicSize;
}
else
{
// Camera projection is perspective
height = 2 * Mathf.Tan(0.5f * Camera.main.fieldOfView * Mathf.Deg2Rad) * Mathf.Abs(cameraPosition.z - transform.position.z);
}
var width = height * Camera.main.aspect;
transform.localScale = new Vector3(width, height,1);
transform.position = cameraPosition - new Vector3(0,height*.375f, cameraPosition.z);
// Since the 4 images are childs of the parent GameObject there is no need
// place or scale them separate. It is all done with placing this parent object
}
private void Start()
{
Apply();
}
}
使用以下场景设置
Sprite的X位置就是
对于四个SpriteRenderer
和对撞机
结果
(带有的附加呼叫Update
)
我让家长职位保持不变Z = 0
。您可以根据需要进行更改。这样,对撞机现在应该能够与其他对象进行交互。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句