-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageController.cs
More file actions
97 lines (84 loc) · 2.77 KB
/
PageController.cs
File metadata and controls
97 lines (84 loc) · 2.77 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
using UnityEngine;
using UnityEngine.UI;
public class PageController : MonoBehaviour
{
public GameObject coverModel; // Assign the cover model in the inspector
public GameObject[] pageModels; // Assign the models for each page in the inspector
public Button[] pageButtons; // Assign the buttons in the inspector
public Sprite[] activeSprites; // Assign the active sprites for each button in the inspector
public Sprite[] inactiveSprites; // Assign the inactive sprites for each button in the inspector
void Start()
{
// Ensure the cover model is initially visible
coverModel.SetActive(true);
// Ensure all page models are initially hidden
foreach (GameObject model in pageModels)
{
model.SetActive(false);
}
// Add listeners to each button to call ShowPageModel with the corresponding index
for (int i = 0; i < pageButtons.Length; i++)
{
int index = i; // Capture the current value of i
pageButtons[i].onClick.AddListener(() => ShowPageModel(index));
}
}
public void ShowPageModel(int pageIndex)
{
// Hide the cover model
coverModel.SetActive(false);
// Hide all page models first
foreach (GameObject model in pageModels)
{
model.SetActive(false);
}
// Show the model for the selected page
if (pageIndex >= 0 && pageIndex < pageModels.Length)
{
pageModels[pageIndex].SetActive(true);
}
// Update the button images
UpdateButtonImages(pageIndex);
}
public void ResetToCoverModel()
{
// Hide all page models
foreach (GameObject model in pageModels)
{
model.SetActive(false);
}
// Show the cover model
coverModel.SetActive(true);
// Reset button images
ResetButtonImages();
}
private void UpdateButtonImages(int activeIndex)
{
for (int i = 0; i < pageButtons.Length; i++)
{
Image buttonImage = pageButtons[i].GetComponent<Image>();
if (buttonImage != null)
{
if (i == activeIndex)
{
buttonImage.sprite = activeSprites[i];
}
else
{
buttonImage.sprite = inactiveSprites[i];
}
}
}
}
private void ResetButtonImages()
{
for (int i = 0; i < pageButtons.Length; i++)
{
Image buttonImage = pageButtons[i].GetComponent<Image>();
if (buttonImage != null)
{
buttonImage.sprite = inactiveSprites[i];
}
}
}
}