NewQuestionDragController.cs 2.0 KB

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