码迷,mamicode.com
首页 > 编程语言 > 详细

unity 分数的显示

时间:2019-02-07 19:14:44      阅读:1157      评论:0      收藏:0      [点我收藏+]

标签:shu   span   ant   cti   sys   engine   date   Once   new   

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

 

所以

一开始先增加

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

   得到分数++;

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

 

下面是写贪吃蛇的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System;
using UnityEngine.UI;

public class Snake : MonoBehaviour
{
    public GameObject weiba;.//插入一个尾巴
    public float spead = 0.3f;//浮动时间值
    Vector2 dir = Vector2.right;//默认动方向
    List<Transform> tail  =  new List<Transform> ();

    public int Fenshudangqian;
    public Text Denfenkuang;

    public static bool eat =false ;

    public Action Onloss;


    

    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("Move", spead, spead);

    }
    void Move()
    {
        Vector2 v = transform.position;
        transform.Translate(dir);
        if (eat)
        {
            GameObject g = (GameObject)Instantiate(weiba, v, Quaternion.identity);

            tail.Insert(0, g.transform);

            eat = false;
        }
        else if(tail.Count >0)

        {
            tail.Last().position = v;
            tail.Insert(0, tail.Last());
            tail.RemoveAt (tail.Count-1);
        }
    }
    // Update is called once per frame
    void Update()
    {
        //控制小蛇方向
        if (Input.GetKey(KeyCode.RightArrow))
            dir = Vector2.right;
        else if (Input.GetKey(KeyCode.DownArrow))
            dir = Vector2.down;
        else if (Input.GetKey(KeyCode.LeftArrow))
            dir = Vector2.left;
        else if (Input.GetKey(KeyCode.UpArrow))
            dir = Vector2.up;

    }
    public void OnTriggerEnter2D(Collider2D coll)
    {
        //物体碰撞计算
        Debug .Log ("get血包");

        if (coll.name.StartsWith("food"))
        {

            eat = true;

            Destroy(coll.gameObject);

            Fenshudangqian++;

            Denfenkuang.text = Fenshudangqian.ToString();
        }
        else 
        {
            //负责发送东西
            if (Onloss != null)
                Onloss();
        }

    }
}

 

unity 分数的显示

标签:shu   span   ant   cti   sys   engine   date   Once   new   

原文地址:https://www.cnblogs.com/liren66/p/10355012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!