//========================================================== // // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd. // All rights reserved. // //========================================================== using GxrSdk; using UnityEngine; using UnityEngine.EventSystems; public class InteractionObjCube : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IBeginDragHandler, IEndDragHandler, IDragHandler, IDropHandler, IInitializePotentialDragHandler { [SerializeField] private InteractionObj _interactionObj = null; [SerializeField] private TextMesh _textMesh = null; private Material _cubeMaterial = null; private Color _randomColor = new Color(); private Vector3 _lastPos = Vector3.zero; private Color _lastColor; private bool _isLockColor = false; private bool _isRotating = false; void Start() { _cubeMaterial = GetComponent().material; ChangeColor(); } public void OnPointerEnter(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { _lastColor = _cubeMaterial.color; ChangeColor(); } } public void OnPointerExit(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { ChangeColor(_lastColor); } } public void OnPointerDown(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { _interactionObj.transform.localScale += Vector3.one * 0.06f; } } public void OnPointerUp(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { _interactionObj.transform.localScale -= Vector3.one * 0.06f; } } public void OnPointerClick(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { ChangeColor(); } } public void OnBeginDrag(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { } } public void OnEndDrag(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { } } public void OnDrag(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { Vector3 currentPos = pointerEventData.Position3D + (pointerEventData.Rotation3D * Vector3.forward) * pointerEventData.PressDistance; if (_lastPos == Vector3.zero) { _lastPos = currentPos; } Vector3 deltaPos = currentPos - _lastPos; deltaPos.z = 0; _interactionObj.transform.position += deltaPos; _lastPos = currentPos; } } public void OnDrop(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { _lastPos = Vector3.zero; InteractionObjManager.Instance.DropObj(_interactionObj); } } public void OnInitializePotentialDrag(PointerEventData eventData) { if (eventData is GxrPointerEventData pointerEventData) { _interactionObj.Add(); } } private Color GetRandomColor() { _randomColor.r = Random.Range(0, 1.0f); _randomColor.g = Random.Range(0, 1.0f); _randomColor.b = Random.Range(0, 1.0f); return _randomColor; } public void ChangeColor(Color color = default) { if (_isLockColor) { return; } if (color == default) { color = GetRandomColor(); } _cubeMaterial.color = color; Color textColor = Color.white - color; textColor.a = 1; _textMesh.color = textColor; } public void LockColor(bool isLock) { if (isLock) { ChangeColor(Color.green); } else { ChangeColor(); } _isLockColor = isLock; } public void Rotate() { if (_interactionObj == null) { return; } int row = _interactionObj.Row; int column = _interactionObj.Column; if (!_isRotating) { LeanTween.rotateAroundLocal(gameObject, Vector3.up, 90, 0.5f + column * 0.15f) .setEase(LeanTweenType.easeOutQuad) .setOnStart(() => { _isRotating = true; }).setOnComplete(() => { _isRotating = false; }); } } }