C# 精灵自动定位和缩放
我有4个带有对撞机的精灵,我想自动定位并将它们均匀地缩放到屏幕底部,这样它们就不会跑过头,也不会离开屏幕。我不能在画布上这样做,它需要作为游戏对象来完成 我还试图让每个精灵的高度为1/4-1/5,这取决于它的外观,这就是为什么代码被下面的4除 如何让它们并排放置在底部?C# 精灵自动定位和缩放,c#,unity3d,C#,Unity3d,我有4个带有对撞机的精灵,我想自动定位并将它们均匀地缩放到屏幕底部,这样它们就不会跑过头,也不会离开屏幕。我不能在画布上这样做,它需要作为游戏对象来完成 我还试图让每个精灵的高度为1/4-1/5,这取决于它的外观,这就是为什么代码被下面的4除 如何让它们并排放置在底部? public class AutoPosition : MonoBehaviour { public Sprite [] goals; public float width = Screen.width / 4; publi
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]
}
}
公共类自动定位:单行为{
公共精神[]目标;
公共浮动宽度=屏幕宽度/4;
公共浮子高度=屏幕高度/4;
//用于初始化
无效开始(){
for(int i=0;i
您可以使用这些图像。并将它们放置在父游戏对象
的内部。只需简单地缩放和定位一个父游戏对象
即可(与画布类似,但具有正常的变换
组件)
使用以下场景设置
精灵的X位置仅为
- 图1:-宽度*1.5
- 图2:-宽度*0.5
- 图3:宽度*0.5
- 图4:宽度*1.5
Update
中附加调用)
我让家长的位置保持在Z=0
。您可以根据需要更改此位置。
通过这种方式,碰撞器现在应该能够与其他对象交互。为什么不能使用画布?(游戏对象也是以画布为父对象的游戏对象).相机会移动还是保持静止?看,相机是静止的,但屏幕大小会改变@derHugoThank you这是迄今为止我能回答的最好的问题,我很高兴能帮上忙:)你也让我有兴趣为自己找到解决方案;)
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();
}
}