DiceUI.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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, Camera.allCameras[0])) return;
  38. // µã»÷
  39. DropDice();
  40. }
  41. public bool isDropped = false;
  42. public void DropDice() {
  43. Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(Camera.allCameras[0], transform.position);
  44. Vector2 currentPos = Camera.allCameras[0].ScreenToWorldPoint(screenPos);
  45. DiceEntity diceNew = GameObject.Instantiate(InputM.instance.dicePrefab).GetComponent<DiceEntity>();
  46. diceNew.transform.position = currentPos;
  47. diceNew.key = keybind;
  48. isDropped = true;
  49. UpdateColor();
  50. }
  51. }