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

unity使用AnimationCurve实现RPG中攻击与受伤时的分数的动态效果ScoreFlash

时间:2015-02-08 10:24:28      阅读:2931      评论:0      收藏:0      [点我收藏+]

标签:ugui   animation   ngui   unity   ui   

??

孙广东:2015-2-8/2:19  转载请注明出处:http://blog.csdn.net/u010019717

更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/space-uid-18602.html 

先看一下很普通的实现方式:??

技术分享技术分享

随便命名一个javascript脚本。然后复制如下的代码之后随便挂在一个对象上,观察效果。

var Point : float;      //要显示的分数
private var GetHitEffect : float;     
private var targY : float;         //分数所要显示的位置Y值
private var PointPosition : Vector3;   //分数的显示位置

var PointSkin : GUISkin;
var PointSkinShadow : GUISkin;

function Start() {
 Point = Mathf.Round(Random.Range(Point/2,Point*2));
 PointPosition = transform.position + Vector3(Random.Range(-1,1),0,Random.Range(-1,1));
 targY = Screen.height /2;
}

function OnGUI() {
 var screenPos2 : Vector3 = Camera.main.camera.WorldToScreenPoint (PointPosition);
 GetHitEffect += Time.deltaTime*30;
 GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7);//改变分数的透明度逐渐消失
 GUI.skin = PointSkinShadow;
 GUI.Label (Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString());
 GUI.skin = PointSkin;
 GUI.Label (Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString());
}

function Update() {     //分数向上移动
 targY -= Time.deltaTime*200;
}


然后来介绍 曲线的运动方式。

新建一个脚本如下:

// -------------------------------------------------------------------------------------
// <copyright file = "MyTween.cs" company = "Game Developer">
// <author = Sunguangdong>
// </copyright>
//
// <summary>
// 可以挂在UI上,进行砍杀时受击,分数的动态显示
// </summary>
// -------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System;

public class MyTween : MonoBehaviour {

    public bool m_Pingpong = false;
    public bool UseTweenPos = false;

    public bool PosSpeedCurve = false;
    public AnimationCurve PosCurve;
    public Vector3 m_BeginPos = Vector3.zero;
    public Vector3 m_EndPos = Vector3.zero;

    public bool UseTweenScale = false;
    public AnimationCurve ScaleCurve;
    public Vector3 m_BeginScale = Vector3.one;
    public Vector3 m_EndScale = Vector3.one;

    public bool UseTweenRot = false;
    public AnimationCurve RotCurve;
    public Vector3 m_BeginRot = Vector3.zero;
    public Vector3 m_EndRot = Vector3.zero;

    /// <summary>
    /// tween的持续时间
    /// </summary>
    public float TweenTime = 1f;

    // 接口区域
    public void Play(Vector3 offset, bool reverse = false , Transform target = null, Action callback = null )
    {
        m_BeginTime = Time.time;
        m_Reverse = reverse;
        m_Target = target;
        m_EndCallback = callback;
        m_Offset = offset;
        m_PlayTween = true;
    }

    /// <summary>
    /// tween的起始时间
    /// </summary>
    private float m_BeginTime = 0f;

    private Vector3 PosDist
    {
        get
        {
            return ToPos - FromPos;
        }
    }

    private Vector3 ScaleDist
    {
        get
        {
            return ToScale - FromScale;
        }
    }

    private Vector3 RotDist
    {
        get
        {
            return ToRot - FromRot;
        }
    }
    private Vector3 UILocalPos
    {
        get
        {
            return m_Target == null ? Vector3.zero : m_Offset + UITool.PosToLocal(m_Target.position, m_RootScale);

        }
    }
    private Vector3 FromPos
    {
        get
        {
            return m_Reverse ? UILocalPos + m_EndPos : UILocalPos + m_BeginPos;
        }
    }
    private Vector3 ToPos
    {
        get
        {
            return m_Reverse ? UILocalPos + m_BeginPos : UILocalPos + m_EndPos;
        }
    }
    private Vector3 FromScale
    {
        get
        {
            return m_Reverse ? m_EndScale : m_BeginScale;
        }
    }
    private Vector3 ToScale
    {
        get
        {
            return m_Reverse ? m_BeginScale : m_EndScale;
        }
    }
    private Vector3 FromRot
    {
        get
        {
            return m_Reverse ? m_EndRot : m_BeginRot;
        }
    }
    private Vector3 ToRot
    {
        get
        {
            return m_Reverse ? m_BeginRot : m_EndRot;
        }
    }
    private float EndTime
    {
        get
        {
            return m_BeginTime + TweenTime;
        }
    }
    private float PosVal
    {
        get
        {
            return m_Reverse ? 1 - PosCurve.Evaluate(CurTime) : PosCurve.Evaluate(CurTime);
        }
    }
    private float ScaleVal
    {
        get
        {
            return m_Reverse ? 1 - ScaleCurve.Evaluate(CurTime) : ScaleCurve.Evaluate(CurTime);
        }
    }
    private float RotVal
    {
        get
        {
            return m_Reverse ? 1 - RotCurve.Evaluate(CurTime) : RotCurve.Evaluate(CurTime);
        }
    }
    private float CurTime
    {
        get
        {
            return m_Reverse ? (EndTime - Time.time) / TweenTime : (Time.time - m_BeginTime) / TweenTime;
        }
    }

    private void UpdatePos()
    {
        if( !UseTweenPos || PosCurve == null )
        {
            return;
        }

        if (PosSpeedCurve )
        {
            transform.localPosition = FromPos + PosDist * PosVal;
        }
        else
        {
            m_CurVect.x = FromPos.x + PosDist.x * (Time.time - m_BeginTime) / TweenTime;
            m_CurVect.y = FromPos.y + PosDist.y * PosVal;
            m_CurVect.z = FromPos.z + PosDist.z * (Time.time - m_BeginTime) / TweenTime;
            transform.localPosition = m_CurVect;
        }
    }
    private void UpdateScale()
    {
        if (!UseTweenScale || ScaleCurve == null )
        {
            return;
        }
        transform.localScale = FromScale + ScaleDist * ScaleVal;
    }
    private void UpdateRot()
    {
        if(!UseTweenRot || RotCurve == null)
        {
            return;
        }
        transform.localRotation = Quaternion.Euler(FromRot + RotDist * RotVal);
    }
    void Start()
    {
        m_BeginTime = Time.time;
        m_RootScale = UIRoot.FindObjectOfType<UIRoot>().transform.localScale;
    }
    void Update()
    {
        if(m_PlayTween)
        {
            if(Time.time > EndTime)
            {
                m_PlayTween = false;
                if(m_Pingpong)
                {
                    m_Reverse = !m_Reverse;
                    m_PlayTween = true;
                    m_BeginTime = Time.time;
                }
                else
                {
                    if(m_EndCallback != null)
                    {
                        m_EndCallback();
                    }
                }
                return;
            }
            UpdatePos();
            UpdateRot();
            UpdateScale();
        }
    }
}
注意、:程序是针对NGUI的

技术分享

设置曲线如下:

技术分享

效果从图片中是看不出来的。  自己动手吧!


??

unity使用AnimationCurve实现RPG中攻击与受伤时的分数的动态效果ScoreFlash

标签:ugui   animation   ngui   unity   ui   

原文地址:http://blog.csdn.net/u010019717/article/details/43616665

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