2024-01-23 22:17:57 +08:00
|
|
|
using System;
|
2020-08-31 08:28:38 +08:00
|
|
|
using System.Linq;
|
|
|
|
using System.Reflection;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using Object = UnityEngine.Object;
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
namespace Coffee.UIExtensions.Demo
|
2020-08-31 08:28:38 +08:00
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
public class CFX_Demo_With_UIParticle : MonoBehaviour
|
2020-08-31 08:28:38 +08:00
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
private MonoBehaviour _demo;
|
2024-01-29 08:53:24 +08:00
|
|
|
private string _demoType;
|
2023-08-17 08:43:02 +08:00
|
|
|
private Toggle _spawnOnUI;
|
|
|
|
private UIParticle _uiParticle;
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
// Start is called before the first frame update
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
_uiParticle = GetComponentInChildren<UIParticle>();
|
|
|
|
_spawnOnUI = GetComponentInChildren<Toggle>();
|
|
|
|
_demo = FindObjectOfType("CFX_Demo_New") as MonoBehaviour
|
2023-11-26 16:16:37 +08:00
|
|
|
?? FindObjectOfType("WFX_Demo_New") as MonoBehaviour
|
|
|
|
?? FindObjectOfType("CFXR_Demo") as MonoBehaviour;
|
|
|
|
_demoType = _demo?.GetType().Name;
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
SetCanvasWidth(800);
|
|
|
|
SetCanvasRenderOverlay(true);
|
|
|
|
}
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
// Update is called once per frame
|
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
if (!_spawnOnUI.isOn || !_demo || !Input.GetMouseButtonDown(0)) return;
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-11-26 16:16:37 +08:00
|
|
|
if (_demoType == "CFX_Demo_New" || _demoType == "WFX_Demo_New")
|
2023-08-17 08:43:02 +08:00
|
|
|
{
|
2023-11-26 16:16:37 +08:00
|
|
|
SpawnParticleCFX();
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2023-11-26 16:16:37 +08:00
|
|
|
else if (_demoType == "CFXR_Demo")
|
|
|
|
{
|
|
|
|
SpawnParticleCFXR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SpawnParticleCFXR()
|
|
|
|
{
|
|
|
|
var particle = _demo.GetType()
|
|
|
|
.GetField("currentEffect", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
|
|
|
?.GetValue(_demo) as GameObject;
|
|
|
|
if (!particle) return;
|
|
|
|
|
|
|
|
var instance = Instantiate(particle);
|
|
|
|
foreach (var c in instance.GetComponentsInChildren<MonoBehaviour>())
|
|
|
|
{
|
|
|
|
if (c.GetType().Name == "CFXR_Effect")
|
|
|
|
{
|
|
|
|
c.enabled = false;
|
|
|
|
}
|
|
|
|
}
|
2024-01-29 08:53:24 +08:00
|
|
|
|
2023-11-26 16:16:37 +08:00
|
|
|
_uiParticle.SetParticleSystemInstance(instance, true);
|
|
|
|
}
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-11-26 16:16:37 +08:00
|
|
|
private void SpawnParticleCFX()
|
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
var particle = _demo.GetType()
|
|
|
|
.GetMethod("spawnParticle", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
|
|
|
|
?.Invoke(_demo, Array.Empty<object>()) as GameObject;
|
|
|
|
if (!particle) return;
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
particle.transform.localScale = Vector3.one;
|
2023-11-26 16:16:37 +08:00
|
|
|
_uiParticle.SetParticleSystemInstance(particle, true);
|
2020-08-31 08:28:38 +08:00
|
|
|
}
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
private static Object FindObjectOfType(string typeName)
|
|
|
|
{
|
|
|
|
var type = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
.SelectMany(x => x.GetTypes())
|
|
|
|
.FirstOrDefault(x => x.Name == typeName);
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2024-09-30 01:12:44 +08:00
|
|
|
#if UNITY_2023_2_OR_NEWER
|
|
|
|
return type == null ? null : FindFirstObjectByType(type);
|
|
|
|
#else
|
2023-08-17 08:43:02 +08:00
|
|
|
return type == null ? null : FindObjectOfType(type);
|
2024-09-30 01:12:44 +08:00
|
|
|
#endif
|
2023-08-17 08:43:02 +08:00
|
|
|
}
|
2020-08-31 08:28:38 +08:00
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
public void SetCanvasWidth(int width)
|
2020-08-31 08:28:38 +08:00
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
var scaler = GetComponentInParent<CanvasScaler>();
|
|
|
|
scaler.screenMatchMode = CanvasScaler.ScreenMatchMode.MatchWidthOrHeight;
|
|
|
|
scaler.matchWidthOrHeight = 0;
|
|
|
|
var resolution = scaler.referenceResolution;
|
|
|
|
resolution.x = width;
|
|
|
|
scaler.referenceResolution = resolution;
|
2020-08-31 08:28:38 +08:00
|
|
|
}
|
2023-08-17 08:43:02 +08:00
|
|
|
|
|
|
|
public void SetCanvasRenderOverlay(bool enable)
|
2020-08-31 08:28:38 +08:00
|
|
|
{
|
2023-08-17 08:43:02 +08:00
|
|
|
var canvas = GetComponentInParent<Canvas>();
|
|
|
|
if (enable)
|
|
|
|
{
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
canvas.worldCamera = Camera.main;
|
|
|
|
canvas.renderMode = RenderMode.ScreenSpaceCamera;
|
|
|
|
canvas.planeDistance = 5;
|
|
|
|
}
|
2020-08-31 08:28:38 +08:00
|
|
|
}
|
|
|
|
|
2023-08-17 08:43:02 +08:00
|
|
|
public void LoadScene(string scene)
|
|
|
|
{
|
|
|
|
SceneManager.LoadScene(scene);
|
|
|
|
}
|
2020-08-31 08:28:38 +08:00
|
|
|
}
|
|
|
|
}
|