DiceUI.cs 1.9 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(49f / 255f, 38f / 255f, 31f / 255f, isDropped ? 0.3f : 1f) :
  29. new Color(111f / 255f, 93f / 255f, 80f / 255f, 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, Camera.allCameras[0])) return;
  35. // µã»÷
  36. DropDice();
  37. }
  38. public bool isDropped = false;
  39. public void DropDice() {
  40. Vector2 screenPos = RectTransformUtility.WorldToScreenPoint(Camera.allCameras[0], transform.position);
  41. Vector2 currentPos = Camera.allCameras[0].ScreenToWorldPoint(screenPos);
  42. DiceEntity diceNew = GameObject.Instantiate(InputM.instance.dicePrefab).GetComponent<DiceEntity>();
  43. diceNew.transform.position = currentPos;
  44. diceNew.key = keybind;
  45. isDropped = true;
  46. UpdateColor();
  47. }
  48. }