当前位置:网站首页>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
边栏推荐
- Pre competition practice of TIANTI competition
- Everything can be expected in the future | one 2022 campus recruitment officially opened
- NBIOT的AT指令
- 风尚云网学习-input属性总结
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
- 标签与路径
- No idle servers? Import OVF image to quickly experience smartx super fusion community version
- 基于卷积神经网络的遥感影像分类识别系统
- 天梯赛赛前练习
- One way ANOVA of SPSS
猜你喜欢
Unable to create servlet under SRC subfile of idea
SSM framework series - JUnit unit test optimization day2-3
解决disagrees about version of symbol device_create
使用Source Insight查看编辑源代码
Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
【csnote】ER图
leetcode:437. Path sum III [DFS selected or not selected?]
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
Labels and paths
Number of nodes of complete binary tree
随机推荐
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
Hard core parsing promise object (do you know these seven common APIs and seven key questions?)
box-sizing
天梯赛赛前练习
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
NPDP|产品经理如何做到不会被程序员排斥?
Number of nodes of complete binary tree
BUUCTF WEB [BJDCTF2020]ZJCTF,不过如此
Kubernetes 入門教程
SSL certificate refund instructions
风尚云网学习-input属性总结
Unable to create servlet under SRC subfile of idea
22. 括号生成
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
MySQL function - recursive function
Introduction to kubernetes
AD20补充笔记3—快捷键+持续更新
Image attribute of input: type attribute of fashion cloud learning -h5
Wonderful review | the sixth issue of "source" - open source economy and industrial investment