75 lines
2.1 KiB
C#
75 lines
2.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class AudioSliderDragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
|
{
|
|
|
|
public RectTransform sliderBackground; // Assign in Inspector
|
|
public float minValue = 0f;
|
|
public float maxValue = 1f;
|
|
public float slideareaOffsetMultiplier = 0.9f;
|
|
public float CurrentValue { get; private set; }
|
|
|
|
private RectTransform handleRect;
|
|
private Vector2 backgroundStart;
|
|
private float backgroundWidth;
|
|
|
|
void Awake()
|
|
{
|
|
handleRect = GetComponent<RectTransform>();
|
|
|
|
if (sliderBackground != null)
|
|
{
|
|
backgroundStart = sliderBackground.position;
|
|
backgroundWidth = sliderBackground.rect.width;
|
|
}
|
|
|
|
}
|
|
|
|
public void OnBeginDrag(PointerEventData eventData)
|
|
{
|
|
UpdateSlider(eventData);
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
UpdateSlider(eventData);
|
|
}
|
|
|
|
public void OnEndDrag(PointerEventData eventData)
|
|
{
|
|
UpdateSlider(eventData);
|
|
//notify FMOD
|
|
}
|
|
private void UpdateSlider(PointerEventData eventData)
|
|
{
|
|
Debug.Log("UpDating Slider");
|
|
Vector2 localPoint;
|
|
RectTransformUtility.ScreenPointToLocalPointInRectangle(sliderBackground, eventData.position, eventData.pressEventCamera, out localPoint);
|
|
|
|
float halfWidth = sliderBackground.rect.width * 0.5f;
|
|
|
|
|
|
|
|
// Only allow dragging within 90% of the slider width, centered
|
|
float limit = halfWidth * slideareaOffsetMultiplier;
|
|
float clampedX = Mathf.Clamp(localPoint.x, -limit, limit);
|
|
|
|
Vector3 newPosition = new Vector3(clampedX, handleRect.localPosition.y, handleRect.localPosition.z);
|
|
handleRect.localPosition = newPosition;
|
|
|
|
// Normalize within the limited 90% range
|
|
float normalized = (clampedX + limit) / (limit * 2f);
|
|
//Debug.Log(normalized);
|
|
CurrentValue = Mathf.Lerp(minValue, maxValue, normalized);
|
|
|
|
//Debug.Log(warningThreshholdValue);
|
|
|
|
|
|
}
|
|
|
|
}
|