hello, I'm trying to create a script that will take care of managing effects (sound, visual) when objects in my "ShorcutSlot" interface are manipulated. To put it simply, play a sound, flash the Ui elements etc... I can't get these results using the simple unity functions IPointerDownHandler, IPointerUpHandler and with the script which follows for example, the sounds play well when the Ui elements are touched but are supposed to be played when the objects are dropped, they are played when the Ui elements are moved. Furthermore I would like to know how to access the activated "dragbox" element to apply effects to it during these operations. finally I would like to know if it is possible to return the information when for example the object is dropped (to put an end to the "loop" present in the script" see below. I specify that I am not an expert in programming and I use it to create these scripts.

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;

public class UISoundHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
[System.Serializable]
public class SoundList
{
public bool isActive = true;
public List sounds;
public float delayBetweenSounds = 0f;
}

public SoundList pressSounds;
public SoundList releaseSounds;

public bool waitBetweenLists = false;
public float timeBetweenLists = 0f;

[Header("Flash HUD Settings")]
public bool flashHUD = false;
public List flashElements;
public Color flashColor = Color.white;
public float flashDuration = 1f;
public float flashDelay = 0f;
public Ease flashEase = Ease.Linear;
public int flashLoops = -1; // -1 for infinite loops

[Header("Scale HUD Settings")]
public bool scaleHUD = false;
public List scaleElements;
public Vector3 targetScale = Vector3.one * 1.5f;
public float scaleDuration = 1f;
public float scaleDelay = 0f;
public Ease scaleEase = Ease.Linear;
public int scaleLoops = -1; // -1 for infinite loops

private AudioSource audioSource;
private Coroutine pressCoroutine;
private Coroutine releaseCoroutine;

private void Awake()
{
audioSource = GetComponent();

if (audioSource == null)
{
audioSource = gameObject.AddComponent();
}
}

public void OnPointerDown(PointerEventData eventData)
{
if (pressCoroutine != null)
{
StopCoroutine(pressCoroutine);
}
pressCoroutine = StartCoroutine(HandlePress());
}

public void OnPointerUp(PointerEventData eventData)
{
if (releaseCoroutine != null)
{
StopCoroutine(releaseCoroutine);
}
releaseCoroutine = StartCoroutine(HandleRelease());
}

private IEnumerator HandlePress()
{
if (flashHUD)
{
yield return new WaitForSeconds(flashDelay);
FlashElements();
}

if (scaleHUD)
{
yield return new WaitForSeconds(scaleDelay);
ScaleElements();
}

if (pressSounds.isActive)
{
yield return PlaySounds(pressSounds);
}

if (waitBetweenLists)
{
yield return new WaitForSeconds(timeBetweenLists);
}
}

private IEnumerator HandleRelease()
{
if (releaseSounds.isActive)
{
yield return PlaySounds(releaseSounds);
}
}

private IEnumerator PlaySounds(SoundList soundList)
{
foreach (var clip in soundList.sounds)
{
if (clip != null)
{
audioSource.PlayOneShot(clip);
if (soundList.delayBetweenSounds > 0f)
{
yield return new WaitForSeconds(soundList.delayBetweenSounds);
}
else
{
yield return null; // wait for the end of frame to play next sound
}
}
}
}

private void FlashElements()
{
foreach (var element in flashElements)
{
Color originalColor = element.color;
element.DOColor(flashColor, flashDuration)
.SetEase(flashEase)
.SetLoops(flashLoops, LoopType.Yoyo)
.OnComplete(() => element.color = originalColor);
}
}

private void ScaleElements()
{
foreach (var element in scaleElements)
{
Vector3 originalScale = element.localScale;
element.DOScale(targetScale, scaleDuration)
.SetEase(scaleEase)
.SetLoops(scaleLoops, LoopType.Yoyo)
.OnComplete(() => element.localScale = originalScale);
}
}
}
  • You can access what's dragged via Maki.UI.Drag - e.g. Maki.UI.Drag.DragContent gives you acces to information on what's dragged, where it came from, etc.
    There's also event handlers to register to to get notified when a drag starts or stops.

    Maki.UI also has other stuff, e.g. content currently below the cursor, tooltips, etc.

    You can also write a custom component to handle the shortcut slot content based on ORK's built-in class (HUDShortcutSlotContentComponent), e.g. either derive from it and override the functions you need or just copy the code and add your custom stuff where you want it.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
Sign In or Register to comment.