我为关卡菜单下载了一个脚本,但是当我尝试该脚本时,无法将滚动条分配给该脚本。我试图复制该脚本,然后再次粘贴并制作一个新脚本,但是它不起作用。我该如何解决?
using UnityEngine;
using UnityEngine.UI;
public class swipe_menu : MonoBehaviour
{
public GameObject scrollbar;
private float scrollpos = 0;
float[] pos;
void Start()
{
}
void Update()
{
pos = new float[transform.childCount];
float distance = 1f / (pos.Length - 1f);
for (int i = 0; i < pos.Length; i++)
{
pos[i] = distance * i;
}
if (Input.GetMouseButton(0))
{
scrollpos = scrollbar.GetComponent<Scrollbar>().value;
}
else
{
for (int i = 0; i < pos.Length; i++)
{
if (scrollpos < pos[i] + (distance / 2) && scrollpos > pos[i] - (distance / 2))
{
scrollbar.GetComponent<Scrollbar>().value = Mathf.Lerp(scrollbar.GetComponent<Scrollbar>().value, pos[i], 0.1f);
}
}
}
for (int i = 0; i < pos.Length; i++)
{
if (scrollpos < pos[i] + (distance / 2) && scrollpos > pos[i] - (distance / 2))
{
Debug.LogWarning("Current Selected Level" + i);
transform.GetChild(i).localScale = Vector2.Lerp(transform.GetChild(i).localScale, new Vector2(1.2f, 1.2f), 0.1f);
for (int j = 0; j < pos.Length; j++)
{
if (j != i)
{
transform.GetChild(j).localScale = Vector2.Lerp(transform.GetChild(j).localScale, new Vector2(0.8f, 0.8f), 0.1f);
}
}
}
}
}
您将滚动条用作GameObject
而不是Scrollbar
,请尝试使用带有正确变量的likes,它将起作用。
没有很多地方可以找到如何使用滚动条,我能找到的最好的是https://forum.unity.com/threads/access-scrollbar-value-not-possible-through-code.283486/