当前位置:网站首页>How to click an object to play an animation
How to click an object to play an animation
2022-04-23 12:52:00 【A little dinosaur without code】
Catalog
Practical effect : Click on the object with the mouse , And play the corresponding animation ( In a certain order )
hypothesis A->B->C In the order of animation playing, we should play it in this order
Scene preparation
① Create the following three objects in the scene and add corresponding Tag
② Because my goal is to make each object play only one animation , So I chose animation
Be careful unity in ctrl+6 create The default animation is animator Animation , We can add... To the object in advance animation Components , Or will animator Change animation to animation Type of animation
I'm going to add... Directly to the object to be created now animation Components .
Code writing
① The first is to create a instanceManage Single case
② Create a script for each object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstanceManage : MonoBehaviour
{
// In order to realize the animation, it must be played in the order I want, so I chose stack
// Stack is an advanced and backward data structure
public Stack<string> StartName = new Stack<string>();
public static InstanceManage Instance { get; private set; }
private void Awake()
{
Instance = this;
IinitializeStack();
}
public void SetGUI(string str)
{
GUIStyle style = new GUIStyle();
style.fontSize = 30;
style.wordWrap = true;
GUI.Box(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, 200, 200), str, style);
}
// This is the execution order restriction of the animation I want cube -> cap -> squere
// Since the stack is first in and last out, we should put cube Put it on the top of the stack
private void IinitializeStack()
{
StartName.Push("sphere");
StartName.Push("cap");
StartName.Push("cube");
}
// Check whether the order is correct
public bool GetRightName(string tag)
{
if (StartName.Count != 0)
{
if (StartName.Peek() == tag)
{
StartName.Pop();
return true;
}
return false;
}
return false;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fatherbody : MonoBehaviour
{
public new Animation animation;
public void Start()
{
animation = GetComponent<Animation>();
}
private void OnMouseDown()
{
if (InstanceManage.Instance.GetRightName(this.tag))// Judge whether the element at the top of the stack is the currently clicked object
{
animation.Play();
Debug.Log(this.gameObject.name);
}
}
}
版权声明
本文为[A little dinosaur without code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231245553635.html
边栏推荐
- 硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
- BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
- BUUCTF WEB [BUUCTF 2018]Online Tool
- RT-thread中关键词解释及部分API
- SSL证书退款说明
- 梳理网络IP代理的几大用途
- SSM框架系列——Junit单元测试优化day2-3
- Keyword interpretation and some APIs in RT thread
- Pre competition practice of TIANTI competition
- Get the punch in record of nailing attendance machine
猜你喜欢
随机推荐
leetcode-791. Custom string sorting
Kubernetes 入門教程
SSM框架系列——Junit单元测试优化day2-3
Zigbee之CC2530最小系统及寄存器配置(1)
php生成json处理中文
STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
Introduction to metalama 4 Use fabric to manipulate items or namespaces
Markdown grammar learning
如何实现点击一下物体播放一次动画
I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
Please help me see what this is, mysql5 5. Thanks
Object.keys后key值数组乱序的问题
将新增和编辑的数据同步更新到列表
Use source insight to view and edit source code
【蓝桥杯】4月17日省赛刷题训练(前3道题)
Buuctf Web [gxyctf2019] no dolls
Stm32cubeprogrammer basic instructions
Dialogue with Bruce, author of PostgreSQL: "changing careers" is to better move forward
航芯技术分享 | ACM32 MCU安全特性概述