NewQuestionDragController.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class NewQuestionDragController : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
  8. public static GameObject itemBeeingDragged;
  9. public GameObject NewQuestion;
  10. public GameObject NewQuestionStartParent;
  11. private static Text answerText;
  12. private Vector3 startPosition;
  13. public Text AnswerText { get => answerText; set => answerText = value; }
  14. private void Start() {
  15. AnswerText = GameObject.FindGameObjectWithTag("AnswerText").GetComponent<Text>();
  16. }
  17. public void OnDrag(PointerEventData eventData) {
  18. NewQuestion.gameObject.transform.position = eventData.position;
  19. }
  20. public void OnEndDrag(PointerEventData eventData) {
  21. itemBeeingDragged = null;
  22. GetComponent<CanvasGroup>().blocksRaycasts = true;
  23. // Kontrollera vart man släpper kortet och skicka upp fråga om position ok
  24. if (NewQuestionStartParent != transform.parent) {
  25. NewQuestion.gameObject.transform.position = startPosition;
  26. }
  27. if (GetAnswerText().text != "???? - ????") { // Om man svarar nej måste man återställa answerText
  28. answerText.text = "???? - ????";
  29. // transform.parent.GetComponent<NewQuestionCardController>().GenerateNewQuestion();
  30. }
  31. }
  32. void IBeginDragHandler.OnBeginDrag(PointerEventData eventData) {
  33. itemBeeingDragged = gameObject;
  34. GetComponent<CanvasGroup>().blocksRaycasts = false;
  35. startPosition = NewQuestion.gameObject.transform.position;
  36. // Instanciate new object to drag
  37. // Save current Position on object (to return to)
  38. }
  39. public static Text GetAnswerText() {
  40. return answerText;
  41. }
  42. public static void SetAnswerText(string value) {
  43. if (NewQuestionDragController.itemBeeingDragged != null) {
  44. answerText.text = value;
  45. }
  46. }
  47. }