Initial commit
This commit is contained in:
116
Assets/Materials/Scripts/CelestialCalendarController.cs
Normal file
116
Assets/Materials/Scripts/CelestialCalendarController.cs
Normal file
@@ -0,0 +1,116 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CelestialCalendarController : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public class PlanetCalendar
|
||||
{
|
||||
public GameObject planet;
|
||||
public GameObject calendar;
|
||||
}
|
||||
|
||||
[SerializeField] private List<PlanetCalendar> planetCalendars;
|
||||
|
||||
private GameObject _activeCalendar;
|
||||
private Camera _activeCamera;
|
||||
|
||||
void Start()
|
||||
{
|
||||
foreach (var item in planetCalendars)
|
||||
{
|
||||
if (item.calendar != null)
|
||||
item.calendar.SetActive(false);
|
||||
}
|
||||
|
||||
UpdateActiveCamera();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateActiveCamera();
|
||||
|
||||
if (_activeCamera == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i <= 9; i++)
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Alpha0 + i) ||
|
||||
Input.GetKeyDown(KeyCode.Keypad0 + i))
|
||||
{
|
||||
HideActive();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!Input.GetMouseButtonDown(0))
|
||||
return;
|
||||
|
||||
Ray ray = _activeCamera.ScreenPointToRay(Input.mousePosition);
|
||||
RaycastHit hit;
|
||||
|
||||
if (!Physics.Raycast(ray, out hit))
|
||||
{
|
||||
HideActive();
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var item in planetCalendars)
|
||||
{
|
||||
if (item.planet == null) continue;
|
||||
|
||||
if (hit.collider.gameObject == item.planet ||
|
||||
hit.collider.transform.IsChildOf(item.planet.transform))
|
||||
{
|
||||
ToggleCalendar(item.calendar);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
HideActive();
|
||||
}
|
||||
|
||||
void UpdateActiveCamera()
|
||||
{
|
||||
Camera[] cameras = Camera.allCameras;
|
||||
|
||||
foreach (var cam in cameras)
|
||||
{
|
||||
if (cam.enabled && cam.gameObject.activeInHierarchy)
|
||||
{
|
||||
_activeCamera = cam;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_activeCamera = null;
|
||||
}
|
||||
|
||||
void ToggleCalendar(GameObject calendar)
|
||||
{
|
||||
if (calendar == null) return;
|
||||
|
||||
if (_activeCalendar == calendar)
|
||||
{
|
||||
HideActive();
|
||||
return;
|
||||
}
|
||||
|
||||
HideActive();
|
||||
|
||||
calendar.SetActive(true);
|
||||
_activeCalendar = calendar;
|
||||
}
|
||||
|
||||
void HideActive()
|
||||
{
|
||||
if (_activeCalendar != null)
|
||||
{
|
||||
_activeCalendar.SetActive(false);
|
||||
_activeCalendar = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user