added drag and drop chip, spin force needs to be randomized
This commit is contained in:
76
Assets/Scripts/DragChip.cs
Normal file
76
Assets/Scripts/DragChip.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DragChip : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public GameObject chipPrefab;
|
||||
public Image rouletteBoardImage;
|
||||
|
||||
private RectTransform canvasRect;
|
||||
private RectTransform chipRect;
|
||||
private Vector2 originalPosition;
|
||||
private GameObject currentChip;
|
||||
private Canvas canvas;
|
||||
|
||||
void Start()
|
||||
{
|
||||
// Get the RectTransform component of the canvas
|
||||
canvasRect = GetComponentInParent<Canvas>().GetComponent<RectTransform>();
|
||||
// Get the Canvas component of the parent object
|
||||
canvas = GetComponentInParent<Canvas>();
|
||||
}
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
// Create a clone of the chip image at the current position
|
||||
currentChip = Instantiate(chipPrefab, transform.position, Quaternion.identity, canvas.transform);
|
||||
// Get the RectTransform component of the clone
|
||||
chipRect = currentChip.GetComponent<RectTransform>();
|
||||
// Store the original position of the clone before dragging
|
||||
originalPosition = chipRect.anchoredPosition;
|
||||
// Bring the clone to the front
|
||||
chipRect.SetAsLastSibling();
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
// Move the clone with the mouse drag movement
|
||||
chipRect.anchoredPosition += eventData.delta / canvas.scaleFactor;
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
// If the clone exists, check if it's dropped onto the roulette board image
|
||||
if (currentChip != null)
|
||||
{
|
||||
RectTransform boardRect = rouletteBoardImage.GetComponent<RectTransform>();
|
||||
Vector2 localMousePos;
|
||||
// Check if the mouse position is within the bounds of the roulette board image
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(boardRect, Input.mousePosition, canvas.worldCamera, out localMousePos))
|
||||
{
|
||||
if (boardRect.rect.Contains(localMousePos))
|
||||
{
|
||||
// If the chip is dropped onto the roulette board image, set its parent to the image and position it at the mouse cursor
|
||||
chipRect.SetParent(rouletteBoardImage.transform);
|
||||
chipRect.anchoredPosition = localMousePos;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the chip is dropped outside of the roulette board image, destroy it
|
||||
Destroy(currentChip);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If the mouse position is not within the bounds of the canvas, destroy the clone
|
||||
Destroy(currentChip);
|
||||
}
|
||||
|
||||
// Set the clone to null to indicate that it no longer exists
|
||||
currentChip = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
Assets/Scripts/DragChip.cs.meta
Normal file
11
Assets/Scripts/DragChip.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2c386988596450b4ea43d1665ccb0900
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -2,6 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using TMPro;
|
||||
|
||||
public class Spinner : MonoBehaviour
|
||||
{
|
||||
@@ -22,6 +23,7 @@ public class Spinner : MonoBehaviour
|
||||
public int offset = -4;
|
||||
public int CurNumber;
|
||||
public float CurrentSpeed {get; private set;}
|
||||
public TMP_Text numText;
|
||||
|
||||
void Start()
|
||||
{
|
||||
@@ -66,6 +68,8 @@ public class Spinner : MonoBehaviour
|
||||
|
||||
LandedNumber = GetLandedNumber();
|
||||
Debug.Log($"Landed on {LandedNumber}");
|
||||
numText.text = LandedNumber.ToString();
|
||||
|
||||
|
||||
|
||||
OnSpinStopped.Invoke(LandedNumber);
|
||||
|
||||
Reference in New Issue
Block a user