Unity 分数增长

来源:互联网 发布:优化人员结构 编辑:程序博客网 时间:2024/06/01 07:54

Unity 分数增长



固定时间内增长

TweenTest.cs

using UnityEngine;using System;using System.Collections;public class TweenTest : MonoBehaviour{    private GameObject prefab;    private UIButton btn;    private UILabel scoreLabel;    private float score;    private float add;    private GameObject addscore;    private float StartValue = 0.0f; //开始的值    private float EndValue = 0.0f; //终点的值    private bool isStart = false; //是否开始    private bool isAdd = false; //是否增涨    public float speed = 0.0f;  //速度    float addTime = 1.0f; // 增加所用的时间    void Awake()    {        // 预先创建好常用的得分Prefab        prefab = (GameObject)Resources.Load("Prefabs/AddScore");        Application.targetFrameRate = 60;//此处限定60帧    }    // Use this for initialization    void Start()    {        btn = GameObject.Find("Button").GetComponent<UIButton>();        scoreLabel = GameObject.Find("Score").GetComponent<UILabel>();        scoreLabel.text = "" + score;        // 设置按钮响应函数        EventDelegate.Add(btn.onClick, AddScore);            }    // Update is called once per frame    void Update()    {        if (isStart)        {            if (isAdd)            {                StartValue += speed;                if (StartValue > EndValue)                {                    isStart = false;                }            }            else            {                StartValue -= speed;                if (StartValue < EndValue)                {                    isStart = false;                }            }            scoreLabel.text = (int)StartValue + "";        }    }    void AddScore()    {        isStart = true;        isAdd = true;        StartValue = EndValue;        addscore = (GameObject)Instantiate(prefab, new Vector3(0, 0, 0), transform.rotation);        UILabel addlabel = addscore.GetComponent<UILabel>();        System.Random random = new System.Random();        // 随机得分数        add = random.Next(500, 1000);        Debug.Log("add = " + add);        EndValue += add;        speed = add / (Application.targetFrameRate * addTime);        Debug.Log("speed = " + speed);        if (speed <= 0)        {            speed = 1;        }    }        }


固定速率增长

using UnityEngine;using System.Collections;public class PowerShow : MonoBehaviour {    private float StartValue = 0; //开始的值    private int EndValue = 1000; //终点的值    private bool isStart = true; //是否开始    private bool isAdd = true; //是否增涨    public int Speed = 100;  //速度    public UILabel Number_Label;    void Update()    {        if(isStart)        {            if (isAdd)            {                StartValue += Speed * Time.deltaTime;                                if(StartValue>EndValue)                {                    isStart = false;                }            }            else            {                StartValue -= Speed * Time.deltaTime;                                if (StartValue < EndValue)                {                    isStart = false;                }            }            Number_Label.text = (int)StartValue + "";        }    }}


0 0
原创粉丝点击