Project_WL/Assets/Script/Main/ParticleGroupView.cs

235 lines
7.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class ParticleGroupView : MonoBehaviour
{
[SerializeField] Text displayName;
[SerializeField] List<ParticleSystem> _targets;
[SerializeField] Dictionary<ParticleSystem, GameObject> _childParticleSaver = new();
[SerializeField] int _currentIndex;
//== 만들거 캔버스 만들고, 페어런츠 만들고, 텍스트 만들고 세팅
[SerializeField] private Canvas _canvas;
[SerializeField] private RectTransform _uiParent;
private void OnValidate()
{
if (Application.isEditor)
{
_targets = new List<ParticleSystem>();
_childParticleSaver = new Dictionary<ParticleSystem, GameObject>();
int count = this.transform.childCount;
for (int i = 0; i < count; i++)
{
Transform child = this.transform.GetChild(i);
ParticleSystem particleSystem = child.GetComponent<ParticleSystem>();
if (particleSystem != null)
{
_targets.Add(particleSystem);
}
else
{
bool loopBreak = false;
for (int inner = 0; inner < child.childCount; inner++)
{
Transform nextChild = child.GetChild(inner);
particleSystem = nextChild.GetComponent<ParticleSystem>();
if (particleSystem != null)
{
_targets.Add(particleSystem);
_childParticleSaver.Add(particleSystem, child.gameObject);
break;
}
else
{
for (int tail = 0; tail < nextChild.childCount; tail++)
{
Transform last = nextChild.GetChild(tail);
particleSystem = last.GetComponent<ParticleSystem>();
if (particleSystem != null)
{
_targets.Add(particleSystem);
_childParticleSaver.Add(particleSystem, child.gameObject);
loopBreak = true;
break;
}
}
if(loopBreak) break;
}
}
}
}
}
if(_canvas == null)
{
//== Canvas Create
GameObject canvasObject = new GameObject("UI Main Canvas");
_canvas = canvasObject.AddComponent<Canvas>();
_canvas.renderMode = RenderMode.ScreenSpaceOverlay;
_canvas.pixelPerfect = false;
_canvas.sortingOrder = 0;
_canvas.targetDisplay = 0;
_canvas.additionalShaderChannels = AdditionalCanvasShaderChannels.None;
//== Canvas scaler set
CanvasScaler scaler = canvasObject.AddComponent<CanvasScaler>();
scaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize;
scaler.referenceResolution = new Vector2(1920, 1080);
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.Expand;
scaler.referencePixelsPerUnit = 100;
//== Graphic raycaster set
GraphicRaycaster raycaster = canvasObject.AddComponent<GraphicRaycaster>();
raycaster.ignoreReversedGraphics = true;
raycaster.blockingObjects = GraphicRaycaster.BlockingObjects.None;
}
if(_uiParent == null)
{
GameObject parent = new GameObject("UI Items", typeof(RectTransform));
parent.transform.SetParent(_canvas.transform);
_uiParent = parent.transform as RectTransform;
_uiParent.localScale = Vector3.one;
_uiParent.anchorMin = Vector3.zero;
_uiParent.anchorMax = Vector3.one;
_uiParent.offsetMin = Vector3.zero;
_uiParent.offsetMax = Vector3.zero;
}
if(displayName == null)
{
GameObject textViewer = new GameObject("Text Viewer");
textViewer.transform.SetParent(_uiParent.transform);
Text text = textViewer.AddComponent<Text>();
text.rectTransform.anchorMin = new Vector2(0,1);
text.rectTransform.anchorMax = new Vector2(1,1);
text.rectTransform.pivot = new Vector2(0.5f, 1);
text.rectTransform.anchoredPosition = new Vector3(0,-50);
text.fontSize = 55;
text.alignment = TextAnchor.MiddleCenter;
displayName = text;
}
EventSystem findEventSystem = GameObject.FindObjectOfType<EventSystem>(true);
if (findEventSystem == null)
{
GameObject eventSystem = new GameObject("Event System");
eventSystem.transform.SetParent(null);
eventSystem.transform.localPosition = Vector3.zero;
eventSystem.transform.localScale = Vector3.one;
EventSystem system = eventSystem.AddComponent<EventSystem>();
eventSystem.AddComponent<StandaloneInputModule>();
}
}
private void Start()
{
_currentIndex = 0;
foreach (var particle in _targets)
{
particle.gameObject.SetActive(false);
}
if (_targets.Count != 0)
{
_targets[_currentIndex].gameObject.SetActive(true);
_targets[_currentIndex].Play(true);
UpdateDisplayParticleName();
}
}
private void UpdateDisplayParticleName()
{
displayName.text = _targets[_currentIndex].name;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
if(_childParticleSaver.TryGetValue(_targets[_currentIndex], out var prev))
{
prev.SetActive(false);
}
_targets[_currentIndex].gameObject.SetActive(false);
if (_currentIndex == 0)
{
_currentIndex = _targets.Count - 1;
}
else
{
_currentIndex--;
}
ParticleSystem particle = _targets[_currentIndex];
if(_childParticleSaver.TryGetValue(particle, out var current))
{
current.SetActive(true);
}
particle.gameObject.SetActive(true);
particle.Play(true);
UpdateDisplayParticleName();
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
if(_childParticleSaver.TryGetValue(_targets[_currentIndex], out var prev))
{
prev.SetActive(false);
}
_targets[_currentIndex].gameObject.SetActive(false);
if (_currentIndex == _targets.Count - 1)
{
_currentIndex = 0;
}
else
{
_currentIndex++;
}
ParticleSystem particle = _targets[_currentIndex];
if(_childParticleSaver.TryGetValue(particle, out var current))
{
current.SetActive(true);
}
particle.gameObject.SetActive(true);
particle.Play(true);
UpdateDisplayParticleName();
}
if (Input.GetKeyDown(KeyCode.Space))
{
ParticleSystem particle = _targets[_currentIndex];
particle.Simulate(0, true, true);
particle.Play(true);
}
}
}