DiceUI.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using DG.Tweening;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using System.Linq;
  7. public class DiceUI : MonoBehaviour {
  8. private Image spriteBg, spriteLetter;
  9. private RectTransform rectTransform;
  10. public InputM.KeyType keybind;
  11. void Start() {
  12. spriteBg = GetComponent<Image>();
  13. rectTransform = GetComponent<RectTransform>();
  14. spriteLetter = GetComponentsInChildren<Image>().Where(sp => sp.gameObject.name == "ImgText").First();
  15. UpdateLetter();
  16. }
  17. public void UpdateLetter() {
  18. spriteLetter.sprite = InputM.GetKeyTextSprite(InputM.GetKeyRaw(keybind));
  19. }
  20. private bool isKeyDown = false;
  21. void LateUpdate() {
  22. if (isKeyDown == InputM.GetKeyUI(keybind)) return;
  23. isKeyDown = !isKeyDown;
  24. UpdateColor();
  25. }
  26. public void UpdateColor() {
  27. spriteBg.DOColor(isKeyDown ?
  28. new Color(168f / 255f, 255f / 255f, 177f / 255f, isDropped ? 0.3f : 1f) :
  29. new Color(159f / 255f, 174f / 255f, 186f / 255f, isDropped ? 0.3f : 1f), 0.05f);
  30. spriteLetter.DOColor(isKeyDown ?
  31. new Color(168f / 255f, 255f / 255f, 177f / 255f, isDropped ? 0.3f : 1f) :
  32. new Color(1f, 1f, 1f, isDropped ? 0.3f : 1f), 0.05f);
  33. }
  34. public bool allowPress = true;
  35. void Update() {
  36. if (!Input.GetMouseButtonUp(0) || !(allowPress || Input.GetKey(KeyCode.LeftControl)) ||
  37. !RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, AspectUtility.cam)) return;
  38. // µã»÷
  39. DropDice();
  40. }
  41. public bool isDropped = false;
  42. public void DropDice() {
  43. Vector2 currentPos = AspectUtility.cam.ScreenToWorldPoint((Vector2) rectTransform.localPosition);
  44. Vector2 currentPos2 = AspectUtility.cam.ScreenToWorldPoint((Vector2) rectTransform.localPosition + rectTransform.sizeDelta);
  45. Vector2 currentScale = Vector2.one * 1.11f; //(currentPos2.x - currentPos.x);
  46. Debug.Log("Dice scale: " + currentScale);
  47. DiceEntity diceNew = GameObject.Instantiate(InputM.instance.dicePrefab).GetComponent<DiceEntity>();
  48. diceNew.transform.localScale = currentScale;
  49. diceNew.transform.position = rectTransform.position;
  50. diceNew.key = keybind;
  51. isDropped = true;
  52. UpdateColor();
  53. }
  54. }