您的位置:首页 > 移动开发 > Unity3D

Unity血条效果,图片动画

2015-01-12 22:36 841 查看
欢迎来到unity学习unity培训unity企业培训教育专区,这里有很多U3D资源、U3D培训视频,我们致力于打造业内unity3d培训学习第一品牌
4000


今天开始做我们的游戏了,组长给分配了任务,我负责做剧情动画,人物血条和种植植物。

 一、剧情动画

    动画是以多个图片的形式展现的,图片是自己制作的。


    private GUITexture guiTexture;//声明GUITexture。

    public Texture[] textures;创建一个Texture数组。

    private int i = 0;设置一个变量作为数组一共有几个图片,设置一个数值。

    private float time = 2f;//显示间隔2秒

void Start () {
        guiTexture = this.GetComponent<GUITexture>();//获取GUITexture组件
}

void Update () {

        time -= Time.deltaTime;//倒计时
        if (time <= 0) 判断减到0的时候
        {
            time = 2;重置时间间隔
            if (i == 7)//播放七张图片
            {
                Application.LoadLevel("Farm");//跳转页面
            }
            else 
            {
                guiTexture.texture = textures[i];//继续播放数组像一张图片
            }
            i++;
        }

 二、血条

       public Texture2D blood_Red;//红色图片
        public Texture2D blood_Black;//黑色图片
        private int HP = 100;//默认血值
        void OnGUI() 
        {
          //根据当前学量计算红色血条的宽度
           int blood_Width = blood_Red.width * 80 / HP;
         //绘制黑色血条
          GUI.DrawTexture(new Rect(20, 10, blood_Black.width, blood_Black.height), blood_Black);
         //绘制红色血条
          GUI.DrawTexture(new Rect(20,10,blood_Width ,blood_Red .height),blood_Red);
       }

三、种植植物

     种植植物是用到射线,预设体和倒计时。
     public Transform explosion; //预设体   

     float times = 5.0f;//倒计时
     int i = 0;
     bool flag = false;//
 void Update () {
         if (Input.GetMouseButtonDown(0)) //鼠标点击
          {
              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//定义射线
              RaycastHit hit;
              if (Physics.Raycast(ray, out hit, 1000)) //如果发生碰撞
              {
                  i++;
                 flag = true;   
                 Transform theClonedExplosion = Instantiate(explosion, hit.point, transform.rotation) as Transform;
              }
          }
          times -= Time.deltaTime;
          if (times <= 0)
          {
              flag = false;
          }
  }
      void OnGUI()
      {
          if (flag == true) 
          {
              GUI.Label(new Rect(550, 300, 300, 20), "播种"+i+"颗白菜!");
           
          }            
      }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息