ListBoxSetting.cs
2.88 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
105
106
107
using System;
using UnityEngine;
namespace AirFishLab.ScrollingList
{
[Serializable]
public class ListBoxSetting
{
[SerializeField]
[Tooltip("The root transform that holding the list boxes")]
private Transform _boxRootTransform;
[SerializeField]
[Tooltip("The prefab of the box")]
private ListBox _boxPrefab;
[SerializeField, Min(1)]
[Tooltip("The number of boxes to be generated")]
private int _numOfBoxes = 5;
public Transform BoxRootTransform
{
get => _boxRootTransform;
set => _boxRootTransform = value;
}
public ListBox BoxPrefab => _boxPrefab;
public int NumOfBoxes => _numOfBoxes;
#region Private Members
/// <summary>
/// The name of the list
/// </summary>
private string _name;
/// <summary>
/// Is the setting initialized?
/// </summary>
private bool _isInitialized;
#endregion
#region Property Setter
public void SetBoxRootTransform(RectTransform rootTransform)
{
if (CheckIsInitialized())
return;
_boxRootTransform = rootTransform;
}
public void SetBoxPrefab(ListBox boxPrefab)
{
if (CheckIsInitialized())
return;
_boxPrefab = boxPrefab;
}
public void SetNumOfBoxes(int numOfBoxes)
{
if (CheckIsInitialized())
return;
_numOfBoxes = numOfBoxes;
}
#endregion
/// <summary>
/// Is the box setting initialized?
/// </summary>
private bool CheckIsInitialized()
{
if (_isInitialized)
Debug.LogWarning(
$"The list setting of the list '{_name}' is initialized. Skip");
return _isInitialized;
}
/// <summary>
/// Initialize the setting
/// </summary>
/// <param name="listObject">The game object of the scrolling list</param>
public void Initialize(GameObject listObject)
{
var listName = listObject.name;
if (!BoxRootTransform) {
Debug.LogWarning(
$"The 'BoxRootTransform' is not assigned in the list '{listName}'. "
+ "Use itself as the 'BoxRootTransform'");
BoxRootTransform = listObject.transform;
}
if (!BoxPrefab)
throw new UnassignedReferenceException(
$"The 'BoxPrefab' is not assigned in the list '{listName}'");
if (NumOfBoxes <= 0)
throw new InvalidOperationException(
$"The 'NumOfBoxes' is 0 or negative in the list '{listName}'");
_name = listName;
_isInitialized = true;
}
}
}