经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
unity 分数的显示
来源:cnblogs  作者:liren666  时间:2019/2/11 9:37:35  对本文有异议

通常 在完成 条件之后再增加分数

 

所以

一开始先增加

  1. public int 得到分数;
  2. public Text 分数ui;
  1. 在完成条件后增加

   得到分数++;

   分数ui.text = 得到分数.ToString();

 

下面是写贪吃蛇的

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System;
  7. using UnityEngine.UI;
  8. public class Snake : MonoBehaviour
  9. {
  10. public GameObject weiba;.//插入一个尾巴
  11. public float spead = 0.3f;//浮动时间值
  12. Vector2 dir = Vector2.right;//默认动方向
  13. List<Transform> tail = new List<Transform> ();
  14. public int Fenshudangqian;
  15. public Text Denfenkuang;
  16. public static bool eat =false ;
  17. public Action Onloss;
  18. // Start is called before the first frame update
  19. void Start()
  20. {
  21. InvokeRepeating("Move", spead, spead);
  22. }
  23. void Move()
  24. {
  25. Vector2 v = transform.position;
  26. transform.Translate(dir);
  27. if (eat)
  28. {
  29. GameObject g = (GameObject)Instantiate(weiba, v, Quaternion.identity);
  30. tail.Insert(0, g.transform);
  31. eat = false;
  32. }
  33. else if(tail.Count >0)
  34. {
  35. tail.Last().position = v;
  36. tail.Insert(0, tail.Last());
  37. tail.RemoveAt (tail.Count-1);
  38. }
  39. }
  40. // Update is called once per frame
  41. void Update()
  42. {
  43. //控制小蛇方向
  44. if (Input.GetKey(KeyCode.RightArrow))
  45. dir = Vector2.right;
  46. else if (Input.GetKey(KeyCode.DownArrow))
  47. dir = Vector2.down;
  48. else if (Input.GetKey(KeyCode.LeftArrow))
  49. dir = Vector2.left;
  50. else if (Input.GetKey(KeyCode.UpArrow))
  51. dir = Vector2.up;
  52. }
  53. public void OnTriggerEnter2D(Collider2D coll)
  54. {
  55. //物体碰撞计算
  56. Debug .Log ("get血包");
  57. if (coll.name.StartsWith("food"))
  58. {
  59. eat = true;
  60. Destroy(coll.gameObject);
  61. Fenshudangqian++;
  62. Denfenkuang.text = Fenshudangqian.ToString();
  63. }
  64. else
  65. {
  66. //负责发送东西
  67. if (Onloss != null)
  68. Onloss();
  69. }
  70. }
  71. }

 

原文链接:http://www.cnblogs.com/liren66/p/10355012.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号