一.toggle的用法
unity中使用 UGUI Toggle 和 Toggle Group 做单选列表
1.打开Unity,然后在场景中添加“Canvas”元素
2.在Canvas中添加“Panel”
3.在Panel下添加三个Toggle
Toggle中的Is On:默认是否选中
4.在Panel上添加“Toggle Group”组件
5.将Panel上的Toggle Group关联到Toggle的Group代码层中关联赋值
ToggleItem.group = toggleGroup;
这样就可以有单选功能了
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class toogle : MonoBehaviour
{
private void Awake()
{
GetComponent().onValueChanged.AddListener(OnValueChanged);
OnValueChanged(GetComponent().isOn);
}
private void OnValueChanged(bool value)
{
transform.GetChild(0).gameObject.SetActive(value);
}
}
这样子就可以通过ison来控制物体的setactive的值,
由此toggle中的ison选项是单选的,就可以通过这一机制来完成很多功能。
2.toggle与button的区别
而且toggle和button最大的区别在于,toggle的用法是能够在值改变时调用上面的方法,而button只能够在点击之后才能调用该方法
二,循环翻页
public void Move(int dir)
{
GameRoot.time = 0;
if (dir==0)
{
index++;
if (index >= rects.Length)
{
index = 0;
}
for (int i = 0; i < rects.Length; i++)
{
if (rects[i].localPosition.y >= offset * (rects.Length - 1))
{
rects[i].localPosition = new Vector3(0,-offset,0);
}
rects[i].DOLocalMoveY(rects[i].localPosition.y+offset,duraction);
}
}
else
{
index--;
if (index < 0)
{
index = rects.Length - 1;
}
for (int i = 0; i < rects.Length; i++)
{
if (rects[i].localPosition.y <= - offset * (rects .Length -1))
{
rects[i].localPosition = new Vector3(0, offset, 0);
}
rects[i].DOLocalMoveY(rects[i].localPosition.y - offset, duraction);
}
}