我用的是unity5.xunity animationclip.time为什么不能用了

关于Unity 动画绘制原理 | 查问题
汇聚最新编程技术,编程问题一网打尽
& 关于Unity 动画绘制原理
关于Unity 动画绘制原理
[ 分类: ]
动画绘制的原理:现在屏幕中确定动画的显示区域,然后将动画中的每一帧图片按固定的时间在这个区域中按顺序切换,从而实现动画的效果。建立文件夹Textures,里面放上图片,此处我截了三张图,风怒的小鸟图片放在该文件夹下,运行结果如下图,点击相关按钮小鸟做相关移动,脚本如下:
&private&var&anim:Object[];//动画数组
private&var&nowFram://帧序列
private&var&mFrameCount://动画帧总数
private&var&fps:float=5;//限制一秒多少帧
private&var&time:float=0;//限制帧的时间
var&tex:Object[];
var&bg:Texture2D;//背景图片
function&Start&()&{
&&&&anim=Resources.LoadAll(“Textures”);//得到帧动画中所有图片资源
&&&&mFrameCount=anim.L//得到动画有多少帧
&&&&bg=Resources.Load(“next_bg”);
function&Update&()&{
function&OnGUI(){
&&&//绘制背景图片
&&&GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),bg,ScaleMode.StretchToFill,true,0);
&&&//调用DrawAnimation,参数一:动画数组,参数二:动画显示区域
&&&DrawAnimation(anim,Rect(x,y,32,48));//全局变量x,y确定主角的方向
&&&if(GUILayout.RepeatButton(“up”))
&&&&&&y-=2;
&&&&&&tex=
&&&if(GUILayout.RepeatButton(“down”))
&&&&&&y+=2;
&&&&&&tex=
&&&if(GUILayout.RepeatButton(“left”))
&&&&&&x-=2;
&&&&&&tex=
&&&if(GUILayout.RepeatButton(“right”))
&&&&&&x+=2;
&&&&&&tex=
function&DrawAnimation(tex:Object[],rect:Rect)
&&&GUILayout.Label(“当前动画播放:地“+nowFram+”帧“);
&&&GUI.DrawTexture(rect,anim[nowFram],ScaleMode.StretchToFill&,true,0);//绘制动画数组
&&&//计算限制帧时间
&&&time+=Time.deltaT
&&&if(time&=1.0/fps)
&&&&&&nowFram++;//帧切换
&&&&&&time=0;//切换后限制帧的时间归零,从新计算
&&&&&&if(nowFram&=mFrameCount)
&&&&&&&&&&nowFram=0;//若当前帧到达最后一帧,那么在从第0帧开始播放Liu_guozhu 的BLOG
用户名:Liu_guozhu
文章数:33
评论数:11
访问量:10892
注册日期:
阅读量:5863
阅读量:12276
阅读量:329246
阅读量:1036846
51CTO推荐博文
&& 最近整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享。& 1:Unity4.x 项目中3D模型其材质丢失,成为“白模”?&&&&& 解决方案:手工重新赋值材质贴图。& 2:Unity4.x 项目中的NavMesh 升级报错?&&&& “NavMesh asset format has changed. Please rebake the NavMesh data.” ?&&&& 解决方案:按照字面含义,重新对静态物体进行烘焙即可。& 3:Unity4.x 天空盒子升级后显示混乱?&&&& 解决方案: 找到项目中“标准资源”(Standard Assets),点击天空盒子的材质,出现提示信息“This texture contains alpha, but is not RGBM(Incompatible with HDR[高动态光照渲染])”点击“Fix Now”进行自动修复即可。&&&& & 4:Unity4.x 在升级后出现某些3D模型不显示的“严重”问题?&&&& 解决方案: 由于Unity5.0 与Unity4.x版本的底层编码变化较大,Unity5.0已经不能正确识别部分老“预设”,从而造成不显示问题。此时我们找到对应模型的“原型”3D模型,重新建立“预设”在场景中的原位置进行重新加载即可。(注意与原来的方位需要一致才可以)。& 5:Unity4.x 项目升级后部分Animation动画失效(不动没有反应)?&&&& 解决方案:基本原理同上题,我们把Animation动画在Unity5.0中重新编辑与测试即可。&&&& & 6: 由于脚本升级过程中造成的各种异常现象?&&&&& 例如:跑酷、射击、RPG等游戏中的英雄对输入信息没有反应,射击与攻击无效等。&&&&& 解决方案: 造成以上问题的直接或者间接原因多数是脚本的升级造成的问题,详细整理如下:&&& 6.1&://Screen.lockCursor = //被Unity5 新脚本代替&&& Cursor.lockState = CursorLockMode.L& &&& &&& 6.2&://GoNeedAddScriptsObj.AddComponent("类名称");//被Unity5 新脚本代替&&& GoNeedAddScriptsObj.AddComponent&DynamicAddScripts&();//必须用泛型代替。 &&&& &&& 6.3& //goCreatObj.Renderer.Material.color=Color.//老写法已经作废。&&&&&& goCreatObj.GetComponent&Renderer&().material.color = Color.red&&&& 6.4& //this.animation.Play(); //写法禁用了&&&&&& this.GetComponent&Animation&().Play("Walking"); //Unity5自动更正。 &&& &&& 6.5& //con.gameObject.collider.//否决&&&&&& con.gameObject.GetComponent&Collider&().&&&&& 7:关于AssetBounds错误信息:&&&& “UnityEngine.AssetBundle.Load(string)' is obsolete: `Method Load has been deprecated. Script updater cannot update it as the loading behaviour has changed. Please use LoadAsset instead and check the documentation for details.”&&& 解决方案:&&& WWW downloadAsset = new WWW(path);&&& //等待下载完成&&& yield return downloadA&&& //加载复合对象,且通过名称把他们读取出来& &&& //GameObject goPrefabs1= (GameObject)downloadAsset.assetBundle.Load("Prefabs_SelfRotationCube");&& GameObject goPrefabs1 = (GameObject)downloadAsset.assetBundle.LoadAsset("Prefabs_SelfRotationCube");//Unity5方式。&&本文出自 “” 博客,请务必保留此出处
了这篇文章
类别:┆阅读(0)┆评论(0)
15:10:50 20:33:1116447人阅读
【Unity】(89)
&& 最近整理Unity4.x 项目升级Unity5.0 过程中出现的各种常见问题,与大家共享。
& 1:Unity4.x 项目中3D模型其材质丢失,成为“白模”?
&&&&& 解决方案:手工重新赋值材质贴图。
& 2:Unity4.x 项目中的NavMesh 升级报错?
&&&& “NavMesh asset format has changed. Please rebake the NavMesh data.” ?
&&&& 解决方案:按照字面含义,重新对静态物体进行烘焙即可。
& 3:Unity4.x 天空盒子升级后显示混乱?
&&&& 解决方案: 找到项目中“标准资源”(Standard Assets),点击天空盒子的材质,出现提示信息“This texture contains alpha, but is not RGBM(Incompatible with HDR[高动态光照渲染])”点击“Fix Now”进行自动修复即可。
& 4:Unity4.x 在升级后出现某些3D模型不显示的“严重”问题?
&&&& 解决方案: 由于Unity5.0 与Unity4.x版本的底层编码变化较大,Unity5.0已经不能正确识别部分老“预设”,从而造成不显示问题。此时我们找到对应模型的“原型”3D模型,重新建立“预设”在场景中的原位置进行重新加载即可。(注意与原来的方位需要一致才可以)。
& 5:Unity4.x 项目升级后部分Animation动画失效(不动没有反应)?
&&&& 解决方案:基本原理同上题,我们把Animation动画在Unity5.0中重新编辑与测试即可。
& 6: 由于脚本升级过程中造成的各种异常现象?
&&&&& 例如:跑酷、射击、RPG等游戏中的英雄对输入信息没有反应,射击与攻击无效等。
&&&&& 解决方案: 造成以上问题的直接或者间接原因多数是脚本的升级造成的问题,详细整理如下:
&&& 6.1&://Screen.lockCursor = //被Unity5 新脚本代替
&&& Cursor.lockState = CursorLockMode.L&
&&& 6.2&://GoNeedAddScriptsObj.AddComponent(&类名称&);//被Unity5 新脚本代替
&&& GoNeedAddScriptsObj.AddComponent&DynamicAddScripts&();//必须用泛型代替。
&&& 6.3& //goCreatObj.Renderer.Material.color=Color.//老写法已经作废。
&&&&&& goCreatObj.GetComponent&Renderer&().material.color = Color.red
&&& 6.4& //this.animation.Play(); //写法禁用了
&&&&&& this.GetComponent&Animation&().Play(&Walking&); //Unity5自动更正。
&&& 6.5& //con.gameObject.collider.//否决
&&&&&& con.gameObject.GetComponent&Collider&().
&&&& 7:关于AssetBounds错误信息:
&&&& “UnityEngine.AssetBundle.Load(string)' is obsolete: `Method Load has been deprecated. Script updater cannot update it as the loading behaviour has changed. Please use LoadAsset instead and check the documentation for details.”
&&& 解决方案:
&&& WWW downloadAsset = new WWW(path);
&&& //等待下载完成
&&& yield return downloadA
&&& //加载复合对象,且通过名称把他们读取出来&
&&& //GameObject goPrefabs1= (GameObject)downloadAsset.assetBundle.Load(&Prefabs_SelfRotationCube&);&& GameObject goPrefabs1 = (GameObject)downloadAsset.assetBundle.LoadAsset(&Prefabs_SelfRotationCube&);//Unity5方式。
本文出自 “刘老师讲Unity” 博客,请务必保留此出处
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:618824次
积分:14915
积分:14915
排名:第504名
原创:139篇
转载:12篇
评论:17条Animation play error. No animation attached. - Unity Answers
Navigation
Unity account
You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio.
Animation play error. No animation attached.
I've been trying to play the animation I created inside of unity. I've added the animation to an empty gameobject called &SwingAnimation&(It's no longer empty). (I created the animation using a cube which I later removed to add a sword.)
The animation is working since its looping every time I test it out.
This is the code which I've been trying to play the animation with:
swing.js Attached to the Player.
function Update () {
if (Input.GetButton (&Fire1&)) {
GameObject.Find(&SwingAnimation&).animation.Play(&swing&);
I have also tried attaching the script directly on the gameobject containing the animation directly with:
function Update () {
animation.Play();
But all I get is: There is no 'Animation' attached to the &SwingAnimation& game object, but a script is trying to access it.
I'm just scratching my head wondering how this can be? There is clearly an animation attached to the gameobject SwingAnimation. What am I doing wrong and how can I fix this?
Thank you in advance for any help you can provide and I apologize if I re-post the question on the matter but I could not find any solution for my problem amongst them. // N0tiC
Best Answer
In order to access the animation, first click the plus button on the bottom left of the animation controller (Parameters) and add a parameter Bool called Fire. Then do the following inside the cube's Update function (I guess you'll have to add a new script): Write var myAnimator: Animator= GetComponent(Animator); This connects the script to the controller. Then write: if (Input etc...) myAnimator.SetBool(&Fire&,true);
Warning: in order for this to work, you must give the cube an idle animation. So delete the orange block state, add an idle, and make sure it becomes orange. Then add back the original cube animation. You can do all this by dragging by dragging animations into the Animator Controller.
Feel free to ask questions. Also, look up on YouTube a thing called Mecanim and find a Unity tutorial on it to show you the visual tricks.
I hope this is what you meant for me to do:
Did you notice that it doesn't have a animation component attached to it? That may just be your problem.
Hint: You can notify a user about this post by typing @username
Attachments: Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
19 People are following this question.你的浏览器禁用了JavaScript, 请开启后刷新浏览器获得更好的体验!
AnimationEvent.time 和动画片段时间的对应关系
本帖最后由 yhy137084 于
17:09 编辑
//unity3d 中动画的帧事件 .
using UnityE
using System.C
public class BoxEventScript : MonoBehaviour {
void Start () {
AnimationClip gunMove = animation.GetClip("left");//动画名
AnimationEvent aevent = new AnimationEvent();
aevent.functionName = "setInfo"; //方法名
aevent.time = 1.05f; //?????????????????????
gunMove.AddEvent(aevent);
void Update () {
void setInfo()
Debug.Log("setInfo");
我要问的是:
AnimationC//这个动画片段有19秒
我要walk播放到第1秒 aevent.time = ?
我要walk播放到第9秒 aevent.time = ?
我要walk播放到第19秒 aevent.time = ?
AnimationEvent.time就是表示此事件触发的时间,当然是那个动画片段的播放时间。
要回复问题请先或
浏览: 1309
关注: 0 人}

我要回帖

更多关于 unity animation 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信