当前位置:   article > 正文

unity 制作行李箱密码_unity密码箱

unity密码箱

一、界面搭建

二、脚本编辑

1.密码数字的移动()

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class NumMove : MonoBehaviour
  6. {
  7. public Transform moveObj;//要移动的物体
  8. public Transform upPos;
  9. public Transform startPos;
  10. public Transform downPos;
  11. Transform targetPos;//滚动的目标位置
  12. float moveSpeed;//滚动速度
  13. public bool isMove;//是否在滚动
  14. public Text middleText;
  15. public Text downText;
  16. public Text upText;
  17. public int Num;//用来计算当前格子中的数字
  18. private void Start()
  19. {
  20. moveSpeed = 5f;
  21. Num = 0;
  22. }
  23. // Update is called once per frame
  24. void Update()
  25. {
  26. if (isMove)
  27. {
  28. moveObj.position = Vector3.Lerp(moveObj.position, targetPos.position, moveSpeed * Time.deltaTime);
  29. if (Vector3.Distance(moveObj.position, targetPos.position)<0.5f)
  30. {
  31. //先让数字滚上去,然后改变文本中的数字,再归位回来
  32. moveObj.position = targetPos.position;
  33. isMove = false;
  34. middleText.text = Num.ToString();
  35. moveObj.position = startPos.position;
  36. }
  37. }
  38. }
  39. /// <summary>
  40. /// 上滚动方法
  41. /// </summary>
  42. public void PageUp()
  43. {
  44. if (!isMove)
  45. {
  46. Num += 1;
  47. if (Num>9)
  48. {
  49. Num = 0;
  50. }
  51. }
  52. targetPos = upPos;
  53. downText.text = Num.ToString();
  54. isMove = true;
  55. }
  56. /// <summary>
  57. /// 下滚动方法
  58. /// </summary>
  59. public void PageDown()
  60. {
  61. if (!isMove)
  62. {
  63. Num -= 1;
  64. if (Num<0)
  65. {
  66. Num = 9;
  67. }
  68. }
  69. targetPos = downPos;
  70. upText.text = Num.ToString();
  71. isMove = true;
  72. }
  73. }

2.检查密码()

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class EnterKey : MonoBehaviour
  5. {
  6. public GameObject[] slot;//密码格子数组
  7. string password;//正确密码
  8. public string inputPassword;//输入密码
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. password = "26";
  13. }
  14. /// <summary>
  15. /// 检查密码方法
  16. /// </summary>
  17. public void CheckPass()
  18. {
  19. //将每个密码格子中的数字拼接成字符串,然后判断下
  20. for (int i = 0; i < slot.Length; i++)
  21. {
  22. inputPassword = inputPassword + slot[i].GetComponent<NumMove>().Num;
  23. }
  24. if (inputPassword==password)
  25. {
  26. print("解锁成功");
  27. }
  28. else
  29. {
  30. print("密码错误");
  31. inputPassword = null;
  32. }
  33. }
  34. }

三、效果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/123566
推荐阅读
相关标签
  

闽ICP备14008679号