123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- //==========================================================
- //
- // 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<MeshRenderer>().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;
- });
- }
- }
- }
|