Pouzivate tu niekto
DOT tween?
Mam taky problem, ten kod dole je pre objekt ktory ked sa klikne prehra sa casticova animacia a tiez scale animacia a potom to ma fade out. Vsetko funguje pomerne dobre, az na to ze obcas sa stava ze mi ten object ne fadne out. proste len tak ostane visiet z full opacity az kym sa neznici cez ten nacasovany destroy. Ked ale komentnem out co je v OnDestroy handlery, tak to funguje ok...
netusite v tom by to mohlo byt?
using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Sirenix.OdinInspector;
using TheraBytes.BetterUi;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;
using UnityEngine.UI.Extensions;
public class Hunt : MonoBehaviour
{
[PreviewField] [SerializeField] private List<Sprite> availableSprites;
[SerializeField] private List<Sprite> frames;
[SerializeField] private Button btn;
[SerializeField] private RectTransform huntRT;
[SerializeField] private BetterImage coin;
[SerializeField] private ParticleSystem sparkles_ps;
[SerializeField] private ParticleSystem glow_ps;
[NonSerialized] public Action<Hunt> onItemClicked;
[NonSerialized] public Vector2 pos;
private Tween fadeTween;
private int nextFrame;
private int totalFrames;
private Color col;
private ParticleSystem.MainModule mainPs;
private Tweener punchScaleTween;
void Start()
{
btn.onClick.AddListener(showFoundAnimation);
totalFrames = frames.Count;
var rnd = GuessItemsPanel.rnd;
var rndInt = rnd.Next(0, availableSprites.Count);
var randomSprite = availableSprites[rndInt];
coin.sprite = randomSprite;
nextFrame = int.Parse(randomSprite.name);
var wid = Screen.width;
var webWid = wid * .1f;
//resize
huntRT.sizeDelta = new Vector2(webWid, webWid);
//first color
var randomAlpha = rnd.Next(25, 65);
col = Color.white;
col.a = randomAlpha / 100f;
coin.color = col;
//second color
randomAlpha = rnd.Next(30, 60);
col = Color.white;
col.a = randomAlpha / 100f;
coin.SecondColor = col;
//Particle Setup
//resize the radius and startsizes
var containWid = huntRT.rect.width;
//set aprticle radius
mainPs = sparkles_ps.main;
var shape = sparkles_ps.shape;
shape.radius = containWid * .5f;
//set particle size
mainPs.startSize = new ParticleSystem.MinMaxCurve(containWid * .2f, containWid * .5f); //10 - 50 ?
glow_ps.startSize = wid * .25f;
pos = transform.position;
}
private void showFoundAnimation()
{
nextFrame = 0;
//change alpha to 1
col.a = 1;
coin.ColoringMode = ColorMode.Color;
coin.color = col;
//start 3d rotation
InvokeRepeating(nameof(rotate3D), 0, .015f);
sparkles_ps.Simulate(0);
sparkles_ps.Play();
glow_ps.Simulate(0);
glow_ps.Play();
int vibrato = 4;
float duration = 1.2f;
float elasticity = 0.2f;
int maxFrames = 50;
Vector3 targetScale = new Vector3(1.2f, 1.2f, 1.2f);
punchScaleTween = huntRT.DOPunchScale(targetScale, duration, vibrato, elasticity);
fadeTween = coin.DOFade(0, .4f).SetEase(Ease.OutQuad).SetDelay(0.8f);
}
private void rotate3D()
{
coin.sprite = frames[(nextFrame) % totalFrames];
nextFrame++;
if (nextFrame == 25) onItemClicked?.Invoke(this);
if (nextFrame == 50) CancelInvoke(nameof(rotate3D));
}
private void OnDestroy()
{
if (sparkles_ps)
{
sparkles_ps.Clear();
Destroy(sparkles_ps.GetComponent<UIParticleSystem>());
Destroy(sparkles_ps);
}
if (glow_ps)
{
glow_ps.Clear();
Destroy(glow_ps.GetComponent<UIParticleSystem>());
Destroy(glow_ps);
}
CancelInvoke(nameof(rotate3D));
fadeTween?.Kill();
punchScaleTween?.Kill();
btn.onClick.RemoveListener(showFoundAnimation);
}
}