当前位置:网站首页>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
边栏推荐
- 梳理網絡IP代理的幾大用途
- A graphic designer's fantasy world | ones characters
- 将新增和编辑的数据同步更新到列表
- STM32 project transplantation: transplantation between chip projects of different models: Ze to C8
- 使用Source Insight查看编辑源代码
- Idea的src子文件下无法创建servlet
- Calculate the past date and days online, and calculate the number of live days
- BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
- Image attribute of input: type attribute of fashion cloud learning -h5
- [csnote] ER diagram
猜你喜欢

风尚云网学习-h5的input:type属性的image属性

没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版

Deploying MySQL in cloud native kubesphere

No idle servers? Import OVF image to quickly experience smartx super fusion community version

Plato Farm-以柏拉图为目标的农场元宇宙游戏

31. 下一个排列

STM32工程移植:不同型号芯片工程之间的移植:ZE到C8

【每日一题】棋盘问题

Realize several "Postures" in which a box is horizontally and vertically centered in the parent box

STM32控制步进电机(ULN2003+28byj)
随机推荐
XinChaCha Trust SSL Organization Validated
At instruction of nbiot
leetcode-791. Custom string sorting
教你快速开发一个 狼人杀微信小程序(附源码)
风尚云网学习-h5的input:type属性的image属性
A graphic designer's fantasy world | ones characters
Number of nodes of complete binary tree
Homomorphic encryption technology learning
The continuous construction of the Internet industry platform is not only able to collect traffic
【csnote】ER图
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
洛谷P3236 [HNOI2014]画框 题解
【蓝桥杯】4月17日省赛刷题训练(前3道题)
If you were a golang interviewer, what questions would you ask?
BUUCTF WEB [BUUCTF 2018]Online Tool
Calculate the past date and days online, and calculate the number of live days
Get the punch in record of nailing attendance machine
云原生KubeSphere部署Mysql
leetcode:437. 路径总和 III【dfs 选还是不选?】
[wechat applet] Z-index is invalid