2016-09-30 27 views
0

(如果你有同样的功能更好的剧本,我接受所有无论C#或JavaScript)基于屏幕的高度统一性对象调整大小做错了

[我的偏好分辨率为1366 * 768]

我不现在不知道该怎么做,用这个c#(c sharp)脚本,我可以使用任何分辨率宽度大小来计算任何对象,但是当我更改分辨率高度大小(从768)时,对象以错误方式调整大小,这是我的c#(c sharp)脚本:(对不起,由于发布代码时出现错误,我使用代码段!)

using UnityEngine; 
 
using System.Collections; 
 

 
public class guicontrol : MonoBehaviour { 
 
\t void CalculateResolution(string ObjectName) { 
 
\t \t GameObject theobject = GameObject.Find (ObjectName); 
 
\t \t float selectedwidth = 1366; 
 
\t \t float selectedheight = 768; 
 
\t \t float objectscaley = theobject.transform.localScale.y; 
 
\t \t float objectscalex = theobject.transform.localScale.x; 
 
\t \t Debug.Log ("current object [ x : " + objectscalex + " | y : " + objectscaley + " ] "); 
 
\t \t float screenwidth = Screen.width; 
 
\t \t float screenheight = Screen.height; 
 
\t \t float coordinatey = theobject.transform.localPosition.y; 
 
\t \t float coordinatex = theobject.transform.localPosition.x; 
 
\t \t Debug.Log ("work at current stats : " + screenwidth + " " + screenheight); 
 
\t \t if (screenwidth != selectedwidth || screenheight != selectedheight) { 
 
\t \t \t float halfthree = 1/3; 
 
\t \t \t float coordinateytomove = (screenheight/selectedheight) * coordinatey; 
 
\t \t \t float coordinatextomove = (screenwidth/selectedwidth) * coordinatex; 
 
\t \t \t theobject.transform.position = new Vector2 (coordinatextomove, coordinateytomove); 
 
\t \t \t Debug.Log (coordinateytomove + " " + coordinatextomove); 
 
\t \t \t float scaleytochange = (screenheight/selectedheight) * objectscaley; 
 
\t \t \t float scalextochange = (screenwidth/selectedwidth) * objectscalex; 
 
\t \t \t theobject.transform.localScale = new Vector2 (scalextochange, scaleytochange); 
 
\t \t \t Debug.Log (scaleytochange + " " + scalextochange); 
 

 
\t \t } 
 

 
\t } 
 
\t // Use this for initialization 
 
\t void Start() { 
 
\t \t Debug.Log ("current position of startbox : " + transform.localPosition); 
 
\t \t Debug.Log ("current screen resolution : " + Screen.currentResolution); 
 
\t \t Debug.Log ("current screen width : " + Screen.width); 
 
\t \t Debug.Log ("current screen height : " + Screen.height); 
 
\t \t CalculateResolution ("startbox"); 
 
\t \t CalculateResolution ("background"); 
 
\t } 
 
\t 
 
\t // Update is called once per frame 
 
\t void Update() { 
 
\t 
 
\t } 
 
}

如果有任何以前的帖子回答这个告诉我!或给我更好的脚本(JavaScript或C锐利所有我接受),但仍具有相同的功能:)。

截图:
(1024×768)
1024 x 768
(300×768)
300 x 768 EASTER EGG !
(1024×600)(错误) 1024 x 600

你可以看到不同的屏幕截图,任何帮助,将不胜感激 ! :)。

EDITED!
EDITED! 01/10/2016
当我使用画布,并从任何对象中删除我的脚本,画布只是做最糟糕的事情,它的工作正常的偏好分辨率(1366 x 768),但是当我改变分辨率,这是做错了决议1024×768(先前工作)(查找的屏幕截图,并将1024×768的脚本工作)

截图: 1024x768 Canvas

回答

0

这整个事情是不必要的。从Unity 4.6开始,如果您想制作按钮,请使用Button组件。使用新的Unity UI。如果要显示UI图像,请使用Image或RawImage组件。

您唯一应该使用SpriteRenderer是当图像是角色或与角色碰撞的环境的一部分,并且还需要使用刚体。如果图像仅作为UI或背景存在,并且不与任何内容相冲突,则需要使用UI来执行此操作,而不是使用SpriteRenderer

当使用UI时,您可以轻松地使用Canvas Scaler,锚点和支点自动调整大小,而无需编写单行代码。画布缩放器会自动调整您的图像到任何屏幕大小。

要建立在团结一个简单的按钮,进入游戏对象 - >UI - >按钮。 这里是Unity UI的教程。

您还可以看到UI TOOLS: RESOLUTION & DEVICE INDEPENDENCE。它展示了如何使UI自动调整任何设备的大小。

+0

感谢您告诉我关于画布:)但仍然无法工作 –

+0

您做错了什么。这款游戏是针对手机(iOS,Android)还是桌面? – Programmer

相关问题