【发布时间】:2020-04-04 18:17:12
【问题描述】:
我想统一使用滑动菜单。它在 1080x1920 分辨率下工作,但不适用于其他屏幕尺寸。
我的代码:
private Vector3 panelLocation;
public float percentThreshold = 0.2f;
public float easing = 0.5f;
private int totalPages = 3;
private int currentPage = 1;
GameObject slide_right, slide_left;
void Start()
{
panelLocation = transform.position;
slide_right = GameObject.FindWithTag("slide_right");
slide_left = GameObject.FindWithTag("slide_left");
}
void Update()
{
if (currentPage == 1)
{
slide_left.SetActive(false);
}else if(currentPage == totalPages)
{
slide_right.SetActive(false);
}
else
{
slide_left.SetActive(true);
slide_right.SetActive(true);
}
}
//sağa doğru çekersem -
//sola doğru çeekrsem +
public void OnDrag(PointerEventData data)
{
float difference = data.pressPosition.x - data.position.x;
transform.position = panelLocation - new Vector3(difference, 0, 0);
}
public void OnEndDrag(PointerEventData data)
{
float percentage = (data.pressPosition.x - data.position.x) / Screen.width;
if (Mathf.Abs(percentage) >= percentThreshold)
{
Vector3 newLocation = panelLocation;
if (percentage > 0 && currentPage < totalPages)
{
currentPage++;
newLocation += new Vector3(-Screen.width, 0, 0);
}
else if (percentage < 0 && currentPage > 1)
{
currentPage--;
newLocation += new Vector3(Screen.width, 0, 0);
}
StartCoroutine(SmoothMove(transform.position, newLocation, easing));
panelLocation = newLocation;
}
else
{
StartCoroutine(SmoothMove(transform.position, panelLocation, easing));
}
}
IEnumerator SmoothMove(Vector3 startpos, Vector3 endpos, float seconds)
{
float t = 0f;
while (t <= 1.0)
{
t += Time.deltaTime / seconds;
transform.position = Vector3.Lerp(startpos, endpos, Mathf.SmoothStep(0f, 1f, t));
yield return null;
}
}
其他级别代码的位置:
public RectTransform menu2, menu3;
void Start()
{
Debug.Log(Screen.width);
/*menu2.offsetMin += new Vector2(Screen.width, 0);
menu2.offsetMax += new Vector2(Screen.width, 0);
menu3.offsetMin += new Vector2(Screen.width * 2, 0);
menu3.offsetMax += new Vector2(Screen.width * 2, 0);*/
menu2.offsetMin += new Vector2(1080, 0);
menu2.offsetMax += new Vector2(1080, 0);
menu3.offsetMin += new Vector2(1080 * 2, 0);
menu3.offsetMax += new Vector2(1080 * 2, 0);
}
我尝试使用屏幕尺寸设置位置,但没有奏效。
我的画布缩放选项: 随屏幕尺寸和默认分辨率 1080x1920 缩放。
我试过这个 youtube 视频。
https://www.youtube.com/watch?v=rjFgThTjLso&t=241s
请帮忙。
【问题讨论】:
-
"我尝试使用屏幕尺寸设置位置,但没有成功。"什么不完全有效?发生了什么?
-
当我尝试设置位置时,1080x1920 再次起作用,但在 480x800 时,菜单 2 在游戏菜单中。喜欢:i.hizliresim.com/XxzPUt.png
标签: android ios unity3d animation