DiceUI.cs 1.9 KB

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