Вы можете использовать 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();
}
}
с помощью следующей настройки сцены
![enter image description here](https://i.stack.imgur.com/cjpbx.png)
X-позиции спрайтов просто:
- image1: - width * 1.5;
- image2: - width *0,5;
- изображение 3: ширина * 0,5;
- изображение 4: ширина * 1,5;
и для четырех SpriteRenderers
![enter image description here](https://i.stack.imgur.com/tUTkT.png)
и коллайдеров
![enter image description here](https://i.stack.imgur.com/QjUJt.png)
Результат
(с дополнительным вызовом в Update
)
![enter image description here](https://i.stack.imgur.com/bAz61.gif)
Я оставил родительскую позицию на Z = 0
.Вы можете изменить это в соответствии с вашими потребностями.Таким образом, коллайдеры теперь смогут взаимодействовать с другими объектами.