| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- namespace JCUnityLib.UI
- {
- public class ValidateJigsawDragBar : MonoBehaviour, IDragHandler, IEndDragHandler, IBeginDragHandler
- {
- RectTransform _self;
- RectTransform _parent;
- float _minX { get => 0; }
- float _maxX { get => _parent.rect.width - _self.rect.width; }
- bool _draged;
- float _value;
- public UnityAction<float> onDrag;
- public UnityAction<float> onEndDrag;
- void Awake()
- {
- _self = transform as RectTransform;
- _parent = transform.parent as RectTransform;
- }
- void Update()
- {
- if (!_draged && _self.anchoredPosition.x > 0)
- {
- _self.anchoredPosition += Vector2.left * 2000 * Time.deltaTime;
- if (_self.anchoredPosition.x < 0) _self.anchoredPosition = Vector3.zero;
- UpdateValue();
- onDrag?.Invoke(_value);
- }
- }
- void UpdateValue()
- {
- _value = _self.anchoredPosition.x / (_maxX - _minX);
- }
- void MoveSelf(Vector3 v3)
- {
- v3.y = 0;
- _self.Translate(v3, Space.Self);
-
- if (_self.anchoredPosition.x > _maxX)
- {
- v3.x = _maxX;
- _self.anchoredPosition = v3;
- }
- else if (_self.anchoredPosition.x < 0)
- {
- v3.x = _minX;
- _self.anchoredPosition = v3;
- }
- }
- public void OnDrag(PointerEventData eventData)
- {
- MoveSelf(eventData.delta);
- UpdateValue();
- onDrag?.Invoke(_value);
- }
- public void OnEndDrag(PointerEventData eventData)
- {
- _draged = false;
- onEndDrag?.Invoke(_value);
- }
- public void OnBeginDrag(PointerEventData eventData)
- {
- _draged = true;
- }
- }
- }
|