Scroll.cs
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using UnityEngine;
using UnityEngine.UI;
public class Scroll : UIBase
{
public Image pre;
public Image next;
//当前选中的纹样
private GameObject current;
private string choosed = "Choosed";
private string unchoose = "Unchoose";
// Start is called before the first frame update
void Start()
{
//初始化纹样,第一个自动选中,并赋值给current
pre.GetComponent<Button>().onClick.AddListener(() =>
{
Debug.Log("pre");
int index = current.transform.GetSiblingIndex();
if (index > 0)
{
// 获取上一个兄弟节点
Transform previous = current.transform.parent.GetChild(index - 1);
updateChoosed(current, previous.gameObject);
}
});
next.GetComponent<Button>().onClick.AddListener(() =>
{
Debug.Log("next");
int index = current.transform.GetSiblingIndex();
int siblingCount = current.transform.parent.childCount;
if (index < siblingCount - 1)
{
// 获取下一个兄弟节点
Transform nextSibling = current.transform.parent.GetChild(index + 1);
updateChoosed(current, nextSibling.gameObject);
}
});
}
// Update is called once per frame
void Update()
{
}
public void ShowLog()
{
Debug.Log("this is UI: " + gameObject.name);
}
protected override void CloseComplete()
{
base.CloseComplete();
Debug.Log("关闭完成: " + gameObject.name);
}
protected override void OpenComplete()
{
base.OpenComplete();
Debug.Log("打开完成: " + gameObject.name);
}
private void SetChoosed(GameObject obj)
{
var choosedObj = obj.transform.Find(choosed).gameObject;
if (choosedObj != null)
choosedObj.SetActive(true);
var unchoosedObj = obj.transform.Find(unchoose).gameObject;
if (unchoosedObj != null)
choosedObj.SetActive(false);
}
private void SetUnchoose(GameObject obj)
{
var choosedObj = obj.transform.Find(choosed).gameObject;
if (choosedObj != null)
choosedObj.SetActive(false);
var unchoosedObj = obj.transform.Find(unchoose).gameObject;
if (unchoosedObj != null)
choosedObj.SetActive(true);
}
//更改选中的纹样
private void updateChoosed(GameObject unChooseObj, GameObject chooseObj)
{
SetUnchoose(unChooseObj);
SetChoosed(chooseObj);
// 更新 current 引用
current =chooseObj;
}
}