Files
2026-03-04 10:03:45 +08:00

227 lines
5.3 KiB
C#

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityStandardAssets.Effects;
namespace UnityStandardAssets.SceneUtils
{
public class ParticleSceneControls : MonoBehaviour
{
public enum Mode
{
Activate = 0,
Instantiate = 1,
Trail = 2
}
public enum AlignMode
{
Normal = 0,
Up = 1
}
[Serializable]
public class DemoParticleSystem
{
public Transform transform;
public Mode mode;
public AlignMode align;
public int maxCount;
public float minDist;
public int camOffset = 15;
public string instructionText;
}
[Serializable]
public class DemoParticleSystemList
{
public DemoParticleSystem[] items;
}
public DemoParticleSystemList demoParticles;
public float spawnOffset = 0.5f;
public float multiply = 1f;
public bool clearOnChange;
public Text titleText;
public Transform sceneCamera;
public Text instructionText;
public Button previousButton;
public Button nextButton;
public GraphicRaycaster graphicRaycaster;
public EventSystem eventSystem;
private ParticleSystemMultiplier m_ParticleMultiplier;
private List<Transform> m_CurrentParticleList = new List<Transform>();
private Transform m_Instance;
private static int s_SelectedIndex;
private Vector3 m_CamOffsetVelocity = Vector3.zero;
private Vector3 m_LastPos;
private static DemoParticleSystem s_Selected;
private void Awake()
{
Select(s_SelectedIndex);
previousButton.onClick.AddListener(Previous);
nextButton.onClick.AddListener(Next);
}
private void OnDisable()
{
previousButton.onClick.RemoveListener(Previous);
nextButton.onClick.RemoveListener(Next);
}
private void Previous()
{
s_SelectedIndex--;
if (s_SelectedIndex == -1)
{
s_SelectedIndex = demoParticles.items.Length - 1;
}
Select(s_SelectedIndex);
}
public void Next()
{
s_SelectedIndex++;
if (s_SelectedIndex == demoParticles.items.Length)
{
s_SelectedIndex = 0;
}
Select(s_SelectedIndex);
}
private void Update()
{
KeyboardInput();
sceneCamera.localPosition = Vector3.SmoothDamp(sceneCamera.localPosition, Vector3.forward * -s_Selected.camOffset, ref m_CamOffsetVelocity, 1f);
if (s_Selected.mode == Mode.Activate || CheckForGuiCollision())
{
return;
}
bool num = Input.GetMouseButtonDown(0) && s_Selected.mode == Mode.Instantiate;
bool flag = Input.GetMouseButton(0) && s_Selected.mode == Mode.Trail;
if (!(num || flag) || !Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out var hitInfo))
{
return;
}
Quaternion rotation = Quaternion.LookRotation(hitInfo.normal);
if (s_Selected.align == AlignMode.Up)
{
rotation = Quaternion.identity;
}
Vector3 vector = hitInfo.point + hitInfo.normal * spawnOffset;
if (!((vector - m_LastPos).magnitude > s_Selected.minDist))
{
return;
}
if (s_Selected.mode != Mode.Trail || m_Instance == null)
{
m_Instance = UnityEngine.Object.Instantiate(s_Selected.transform, vector, rotation);
if (m_ParticleMultiplier != null)
{
m_Instance.GetComponent<ParticleSystemMultiplier>().multiplier = multiply;
}
m_CurrentParticleList.Add(m_Instance);
if (s_Selected.maxCount > 0 && m_CurrentParticleList.Count > s_Selected.maxCount)
{
if (m_CurrentParticleList[0] != null)
{
UnityEngine.Object.Destroy(m_CurrentParticleList[0].gameObject);
}
m_CurrentParticleList.RemoveAt(0);
}
}
else
{
m_Instance.position = vector;
m_Instance.rotation = rotation;
}
if (s_Selected.mode == Mode.Trail)
{
ParticleSystem.EmissionModule emission = m_Instance.transform.GetComponent<ParticleSystem>().emission;
emission.enabled = false;
m_Instance.transform.GetComponent<ParticleSystem>().Emit(1);
}
m_Instance.parent = hitInfo.transform;
m_LastPos = vector;
}
private void KeyboardInput()
{
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Previous();
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Next();
}
}
private bool CheckForGuiCollision()
{
PointerEventData pointerEventData = new PointerEventData(eventSystem);
pointerEventData.pressPosition = Input.mousePosition;
pointerEventData.position = Input.mousePosition;
List<RaycastResult> list = new List<RaycastResult>();
graphicRaycaster.Raycast(pointerEventData, list);
return list.Count > 0;
}
private void Select(int i)
{
s_Selected = demoParticles.items[i];
m_Instance = null;
DemoParticleSystem[] items = demoParticles.items;
foreach (DemoParticleSystem demoParticleSystem in items)
{
if (demoParticleSystem != s_Selected && demoParticleSystem.mode == Mode.Activate)
{
demoParticleSystem.transform.gameObject.SetActive(value: false);
}
}
if (s_Selected.mode == Mode.Activate)
{
s_Selected.transform.gameObject.SetActive(value: true);
}
m_ParticleMultiplier = s_Selected.transform.GetComponent<ParticleSystemMultiplier>();
multiply = 1f;
if (clearOnChange)
{
while (m_CurrentParticleList.Count > 0)
{
UnityEngine.Object.Destroy(m_CurrentParticleList[0].gameObject);
m_CurrentParticleList.RemoveAt(0);
}
}
instructionText.text = s_Selected.instructionText;
titleText.text = s_Selected.transform.name;
}
}
}