DiceUI.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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(247f / 255f, 142f / 255f, 48f / 255f, isDropped ? 0.3f : 1f) :
  29. new Color(1f, 1f, 1f, isDropped ? 0.3f : 1f), 0.05f);
  30. }
  31. public bool allowPress = true;
  32. void Update() {
  33. if (!Input.GetMouseButtonUp(0) || !(allowPress || Input.GetKey(KeyCode.LeftControl)) ||
  34. !RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition, AspectUtility.cam)) return;
  35. // µã»÷
  36. DropDice(true);
  37. }
  38. public bool isDropped = false;
  39. public void DropDice(bool canRestore) {
  40. DiceEntity diceNew = GameObject.Instantiate(InputM.instance.dicePrefab).GetComponent<DiceEntity>();
  41. diceNew.transform.localScale = Vector2.one * 1.11f;
  42. diceNew.transform.position = rectTransform.position;
  43. diceNew.key = keybind;
  44. diceNew.canRestore = canRestore;
  45. isDropped = true;
  46. UpdateColor();
  47. }
  48. }