87 lines
1.9 KiB
C#
87 lines
1.9 KiB
C#
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
using UnityEngine;
|
|
|
|
[ExecuteAlways]
|
|
public class DoorNumber : MonoBehaviour
|
|
{
|
|
[SerializeField] private string roomNumber = "1234";
|
|
|
|
[SerializeField] private GameObject[] digitPrefabs = new GameObject[10];
|
|
|
|
[SerializeField] private Transform[] digitSlots = new Transform[4];
|
|
|
|
private void OnValidate()
|
|
{
|
|
#if UNITY_EDITOR
|
|
if (!Application.isPlaying)
|
|
{
|
|
EditorApplication.delayCall += DelayedUpdate;
|
|
return;
|
|
}
|
|
#endif
|
|
|
|
UpdateNumber();
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
private void DelayedUpdate()
|
|
{
|
|
if (this == null)
|
|
return;
|
|
|
|
UpdateNumber();
|
|
}
|
|
#endif
|
|
|
|
private void ClearSlot(Transform slot)
|
|
{
|
|
if (slot == null)
|
|
return;
|
|
|
|
for (int i = slot.childCount - 1; i >= 0; i--)
|
|
{
|
|
DestroyImmediate(slot.GetChild(i).gameObject);
|
|
}
|
|
}
|
|
|
|
private void UpdateNumber()
|
|
{
|
|
if (digitPrefabs == null || digitPrefabs.Length != 10)
|
|
return;
|
|
|
|
if (digitSlots == null || digitSlots.Length != 4)
|
|
return;
|
|
|
|
string number = roomNumber.PadLeft(4, '0');
|
|
|
|
if (number.Length > 4)
|
|
number = number.Substring(number.Length - 4);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
Transform slot = digitSlots[i];
|
|
|
|
if (slot == null)
|
|
continue;
|
|
|
|
ClearSlot(slot);
|
|
|
|
int digit = number[i] - '0';
|
|
|
|
if (digitPrefabs[digit] == null)
|
|
continue;
|
|
|
|
GameObject newDigit = Instantiate(
|
|
digitPrefabs[digit],
|
|
slot
|
|
);
|
|
|
|
newDigit.transform.localPosition = Vector3.zero;
|
|
newDigit.transform.localRotation = Quaternion.identity;
|
|
newDigit.transform.localScale = Vector3.one;
|
|
}
|
|
}
|
|
} |