unity 用代码控制动画的播放的进度
https://answers.unity.com/questions/1225328/imported-animated-object-and-slider-tutorial.html
using UnityEngine; public class NewBehaviourScript : MonoBehaviour {
//Animation 需要将动画调为Legacy,
//自建的动画可以在Inspector面板下调为Debug模式,勾选上Legacy就可以了
//public Animation _animation; public Animator _animator; void Start () {
//_animation.Play("New Animation1");
// _animation["New Animation1"].speed = 0; _animator.speed = 0;
}
float step = 0;
float timer = 0; void Update () {
//if (Input.GetMouseButton(0))
//{
// timer += Time.deltaTime;
// if (timer>0.1f)
// {
// step += 0.1f;
// if (step <= 1)
// {
// _animation["New Animation1"].normalizedTime = step;
// Debug.Log(step);
// }
// else
// {
// step = 0;
// }
// timer = 0;
// }
//}
if (Input.GetMouseButton(1))
{
timer += Time.deltaTime;
if (timer > 0.1f)
{
step += 0.1f;
if (step <= 1)
{
_animator.Play("New Animation1", 0, step);
Debug.Log(step);
}
else
{
step = 0;
}
timer = 0;
}
}
}
}
How to control animation with slider
Hello, Im a noob to unity.
Ive searched and it seems there have been similar questions to this, but answered with advanced knowledge. I have no idea how to start this.
(attached image) I have a hand pinching a block. i want to use the slider in a way that when I slide the knob to the right, the animation will advance, pinching the block.
I have the animation exported in fbx format and placed in my canvas. I added the slider but don't know where to go from there as far as connecting it, and the correct scripting for it.So anything that is along the lines of using the slider to control the imported animation would be great. Please help! Thank you!
个解答,截止vfxjex · 2016年08月07日 05:17
Hello I think I answered this concern base on the link @KuR5 shared, but if things are not yet cleared let me try to clarify it.
First of all make sure that the attached component of your model is "Animation" not Animator.
second make sure that the animation of your hand is "Legacy". if you don't know how to make your animation Legacy just right click the inspector "Tab" of your animation clip and check the Legacy checkbox. make sure you switch back to normal after doing this so.
Third in the animation component, there is an Animations attribute. change the size to 1 and drag and drop the animation of your hand.
4th Copy and paste this Script and attached it to your model in the Hierarchy. Don't forget to drag and drop the slider into the slider variable.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AnimControl : MonoBehaviour {
Animation anim;
//Drag and drop your Slider into this variable.
public Slider slider;
// Use this for initialization
void Start () {
anim = GetComponent<Animation> ();
//Make sure you have attached your animation in the Animations attribute
anim.Play ("myAnimation");
anim ["myAnimation"].speed = 0;
}
// Attached this on Slider's On Value Change
public void AnimateOnSliderValue () {
anim["myAnimation"].normalizedTime = slider.value;
}
}
Hope this works.
Something isn't working but it looks like it almost is.
I bring my 'handAnim' fbx into the scene. The part where you say to right click for Legacy ( I think we are using different Unity versions) but I changed the Animation type to Legacy and did 'Apply', you say put back to normal but I dont see a normal option'. see screen shot below on 'answer' ( I couldnt add on reply)
Confirmed Im using Animation with size 1, used your script and where it says 'myAnimation' replaced with with 'handAnimation', and dropped it on my handAnim component hierarchy, and after bringing my slider into the scene, and also dragged and dropped the slider into the Slider variable
// Attached this on Slider's On Value Change public void AnimateOnSliderValue () { anim["myAnimation"].normalizedTime = slider.value; --For this part, On Value Change under Runtime, I connected the 'handSlider (slider) and selected 'value' (is that right?)
I hit play, notice there are no errors in my console but the slider isn't affecting the animation. Now I do realize my animation starts and ends (23.0-49.0) Will this affect the slider when the slider triggers the animation and do I need to indicate that its starts at frame 23? If so, how?
Thanks A TON!!!
个解答,截止Rodlaiz · 2017年02月22日 14:56
The solution provided by Vfxjex doesn't work for me because the Animation component has to be marked as Legacy. I try this solution using Animator instead of Animation and it works for me:
using UnityEngine;
using UnityEngine.UI;
public class ControlAnimation : MonoBehaviour
{
private Animator anim;
public Slider slider; //Assign the UI slider of your scene in this slot
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
anim.speed = 0;
}
// Update is called once per frame
void Update()
{
anim.Play("TEST_DELETE", -1, slider.normalizedValue);
}
}
I am trying to control my animation via scroll wheel, and this method works fine. Thanks for posting. :)
个解答,截止mbrako · 2016年08月10日 19:27
I got it working! Thanks @vfxjex
For anybody thats looking for how I got it to work, I put a step-by-step tutorial (Im still a noob to Unity so my explanation is very basic!!)
Lets use a simple example to demonstrate how to use a slider to control a simple animated object that has been exported (fbx)
Im using a model of fingers pinching a skin anatomical figure that I built and animated called 'handAnimationsv002'. I’ve exported the model, joints and blend shape (animated from frame 0-24). I will refer to the names I used in the project, but substitute whatever you may have. Use these images to reference the figuresImport your fbx into your Unity project (Im using the 2D option)
Figure 1. Select your model. Go into the ‘Rig’ tab. Change Animation type to Legacy and hit Apply.
Figure 2 Create your Canvas (this will add an EventSystem to your project). Drag your fbx over the canvas to create a child of Canvas.
I adjusted the scale and rotation for this just because my export came out too small. Ive also put in a directional light (default) to light this model.
Expand the fbx icon in your project folder to reveal all the parts of the model.
Ive changed name of the icon with the play button to ‘animateHand’ for the script later.
Drag the ‘animateHand’ icon into the value of Animation and make sure your size is set to 1.
Hit Play. You may not see anything. In this case. Ive adjusted both my canvas and main camera settings. I also switched over to 3D view temporarily to see how things are aligned.
Figure 3 Take note of your Pos X and Pos Y to use on your Main Camera Settings. Change the render mode to ‘World Space’ .
Figure 4 Select the Main Camera and Plug in the X and Y position to match the canvas.
Using your 3D scene, move the camera in Z-Axis to get your models in view.
Adjust the Size and Clipping Planes accordingly.
Hit Play now and the scene should be in place. If you notice the object animates, turn off ‘Play Automatically’ on ‘handAnimationsv002’ setting
Figure 5 Create the script ‘AnimControl’ and copy paste this script (courtesy of @vfxjex )
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class AnimControl : MonoBehaviour { Animation anim;
//Drag and drop your Slider into this variable.
public Slider slider;
// Use this for initialization
void Start () {
anim = GetComponent<Animation> ();
//Make sure you have attached your animation in the Animations attribute
anim.Play ("animateHand");
anim ["animateHand"].speed = 0;
}
void Update (){
anim ["animateHand"].normalizedTime = slider.value;
}
}
Select ‘handAnimationsv002’ in the hierarchy and drag the AnimControl script into the components area Then drag ‘Slider’ into the ‘slider value’
Figure 6 Select Slider in the hierarchy and drag Slider to the On Value Changed (Single) value
Figure 7 Select value for the Function
Press Play and try it!!
unity 用代码控制动画的播放的进度的更多相关文章
- Unity实现代码控制音频播放
前言 很久没说过Unity了,现在说一下Unity用代码控制音频播放 准备工作 1.需要播放的音频 2.给需要加声音的对象加Audio Source组件 3.新建Play脚本,并绑定需要播放声音的对象 ...
- 使用as3控制动画的播放与暂停
1.需要两个按钮元件 2.在属性面板为两个按钮元件分别命名为pausebutton与playButton 3.代码 stop(); pausebutton.visible = false; playB ...
- unity 动画 音频播放
采用Unity进行音频动画的播放时最常用的技术,在此进行一下简单讲解与应用. (一)动画播放(本文采用animation进行验证,关于animation和animator区别可问度娘,在此不做赘述) ...
- 关于Unity中旧版动画系统的使用
Unity在5.X以后,有一个旧版的动画系统和新版的动画系统. 新版的动画系统是使用Unity动画编辑器来调的,调动画和控制动画 旧版的动画系统是用其他的第三方软件调好后导出到一个FBX文件里面,就是 ...
- CSS3属性animation-play-state控制动画运行或暂停的技巧
animation-play-state介绍 animation-play-state 属性规定动画正在运行还是暂停. div{ animation-play-state:paused; -webki ...
- JS控制flash的播放
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- 关于Unity中Mecanim动画的动画状态代码控制与代码生成动画控制器
对于多量的.复杂的.有规律的控制器使用代码生成 动画状态代码控制 1:每个动画状态,比如进入状态,离开状态, 等都有可能需要代码来参与和处理,比如,进入这个动画单元后做哪些事情,来开这个动画单元后做哪 ...
- Unity编辑器 - 编辑器控制特效播放
编辑器控制特效播放 Unity的动画编辑器不能预览粒子系统的播放,为了方便预览特效,设想制作一个预览特效的工具,通常一个特效有三种组件: - Animation - Animator - Partic ...
- Unity NGUI实现序列帧动画播放
如题,要实现序列帧的播放导入图片的时候需要注意: (1)图片的命名要连续,如图: (2)将这些图片在NGUI中打包成Altas图集的时候图片应该在同一个Altas中: 这里以播放特效为例,满足条件时播 ...
随机推荐
- 洛谷 题解 UVA1626 【括号序列 Brackets sequence】
看还没有人发记搜的题解,赶紧来水发一篇 我们定义dp[i][j]为区间i~j内最少添加几个括号才能把这个串变成正规括号序列. 考虑四种情况 i>j不存在这种子串,返回0 i==j子串长度为1无论 ...
- 《剑指offer》查找二维数组内元素 c++
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序. 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. cl ...
- [转帖]GNU/Linux与开源文化的那些人和事
GNU/Linux与开源文化的那些人和事 时间:2015-09-24 作者:admin 分类:新手入门 阅读:167次 http://embeddedlinux.org.cn/emb-linux/ ...
- 【51nod】2590 持续讨伐
[51nod]2590 持续讨伐 挣扎着卡了卡常过了 记\(dp[i][j]\)为到第\(i\)位,和第\(i\)位相连的部分长度\(x^{j}\)乘上之前部分所有方案\(x^{K}\)总和 转移用二 ...
- python 之 Urllib库的基本使用
目录 python 之 Urllib库的基本使用 官方文档 什么是Urllib urlopen url参数的使用 data参数的使用 timeout参数的使用 响应 响应类型.状态码.响应头 requ ...
- 第十章 ZYNQ-MIZ701 DDR3 PS读写操作方案
本编文章的目的主要用简明的方法在纯PS里对DDR3进行读写. 本文所使用的开发板是Miz701 PC 开发环境版本:Vivado 2015.4 Xilinx SDK 2015.4 10.0本章难度 ...
- MySQL 聚合函数(三)MySQL对GROUP BY的处理
原文来自MySQL 5.7 官方手册:12.20.3 MySQL Handling of GROUP BY SQL-92和更早版本不允许SELECT列表,HAVING条件或ORDER BY列表引用未在 ...
- ELinq学习一
ELinq安装:在Nuget控制台中输入:install-package ELinq一.ELinq与DLinq和EF的功能差异 二.数据库对照表 三.CRUD操作1.插入(Insert)(1)简单形式 ...
- datatable 写入excel 2007
1 添加引用: NPOI NPOI.OOXML 2 private static void GenerateFile(DataTable dt) { DataSet ds = new DataSet( ...
- K2 BPM_曾经我也是996的一员_全球领先的工作流引擎
最近关于996的工作模式掀起了新一波讨论热潮.事情源于有人在知名代码托管平台GitHub上,发起了一个名为“996.ICU”的项目,意为“工作996,生病ICU”,以抵制互联网公司的996工作制,项目 ...