2016-02-23 50 views
0

自一两个月前开始使用Unity3D制作游戏。我已经完成了我的第一款Android游戏,它在我的手机(Samsung Galaxy S6)和仿真器上(Genymotion用不同的虚拟设备)完美地工作,但是当我在父亲的手机上试用时(Nexus 5,Xperia Z1 & Z3 )我意识到它工作不好。各种设备的屏幕尺寸/比率灾难

该游戏是一个2D汽车交通赛车手,所以你必须躲避该产卵者在X轴上随机位置创建的所有汽车。我不知道Unity3d太多,所以我不能更好地解释它,对不起... :(

问题是,在我的手机上,敌方汽车从上到下产卵正确,但在我父亲的手机上从sceen至底部中间产卵而当你移动你的车向左或向右,它看起来像切斜的另一个问题是

这是敌人的产卵的代码:。

public class SpawnerEnemigos : MonoBehaviour { 

public GameObject[] cochesEnemigos; 
int cocheEnemigoID; 
public float maxPos = 2f; 
public float delayTimer = 0.5f; 
private float timer; 

// Use this for initialization 
void Start() { 
    timer = delayTimer; 
} 

// Update is called once per frame 
void Update() { 

    timer -= Time.deltaTime; 
    if (timer <= 0) { 
     Vector2 enemigoRandomPos = new Vector2 (Random.Range(-maxPos, maxPos), transform.position.y); 
     cocheEnemigoID = Random.Range(0,7); 
     Instantiate (cochesEnemigos[cocheEnemigoID], enemigoRandomPos, transform.rotation); 
     timer = delayTimer; 
    } 
} 

}

+2

请记住,游戏不应该依赖手机屏幕尺寸。您可能硬编码了一些适合您S6的屏幕分辨率的值,但不适用于其他手机。没有附加代码,人们不可能为你提供更大的帮助。 – pleft

+1

除了上面提到的@elefasGR(这很可能是问题)之外,您的资产也可能无法正确确定其他屏幕密度的大小,导致它们在各种屏幕尺寸上的位置偏离。我要做的第一件事就是尝试模仿你父亲的手机,并匹配他们的确切屏幕尺寸和密度,然后从那里开始使用你的代码。 – NoChinDeluxe

+1

显示你的代码,你选择随机产生的位置。 – Buddy

回答

0

问题是,在我的手机上,敌方车辆从上到下产卵正确,但在我父亲的手机上从屏幕中间产生到底部。

由于乔提到这可能是由于视口的差异。具有不同宽高比的设备,汽车出生点可能会根据屏幕而改变。

下面是关于如何使用视口来计算,其中在世界上的文档您的对象将产生:Camera.ViewportToWorldPoint

// This is the part that we will be replacing. 
Vector2 enemigoRandomPos = new Vector2 (Random.Range(-maxPos, maxPos), transform.position.y); 

这是我将如何根据您所提供的代码去了解它:

// Set the offset of the X axis first. This should be fairly similar for most devices, 
// if you find issues with it apply the same logic as the Y axis. 
var x = Random.Range(-maxPos, maxPos); 
// Here is where the magic happens, ViewportToWorldPoint converts a number between 0 and 1 to 
// an in-world number based on what the camera sees. In this specific situation I am telling it: to use 0f, 1f 
// which roughly translates to "At the top of the screen, on the left corner". Then storing the Y value of the call. 
var y = Camera.main.ViewportToWorldPoint(new Vector2(0f, 1f)).y; 
// Now that we have the x and y values, we can simply create the enemigoRandomPos based on them. 
var enemigoRandomPos = new Vector2(x, y); 

可以ofcourse删除我所有的意见和在线整个事情,而不是:

var enemigoRandomPos = new Vector2(Random.Range(-maxPos, maxPos), Camera.main.ViewportToWorldPoint(new Vector2(0f, 1f)).y); 

几件事情要记住:

  • Camera.main可以不定义,你需要找到摄像机的一个实例(这是这个问题的范围之外,所以我会让你谷歌为此,如果你有问题,让我知道,我会很乐意提供进一步的信息)
  • X位置可能会在一些纵横比变得怪异,所以我建议你考虑也使用视口计算
  • 将这些值(Y和相机)存储在开始方法上会更有效,并且只有在高宽比改变或相机更改时才会更改它们。这对旧设备的性能会有所帮助。更多的家庭作业研究。 :)
  • 在对这类问题进行故障排除时,使用显示问题的静态小精灵(也就是不移动的东西)会很有帮助。我会在屏幕的所有角落+中心产生大约9个精灵,看看在调试过程中汽车从视觉辅助中产生的位置。
  • 问一个问题那就是图形的性质可以帮助人们试图给你的反馈很多,可以考虑加入一些当下次还提供了屏幕截图:d

而另一个问题是当您将汽车向右或向左移动时,它看起来像对角切割。

基于这个描述,它听起来像是汽车精灵和背景精灵三角形的某种裁剪问题。我建议根据相机的位置来回移动背景,以避免裁剪。