当前位置:网站首页>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
边栏推荐
- 云原生KubeSphere部署Mysql
- Plato farm - a game of farm metauniverse with Plato as the goal
- Process virtual address space partition
- STM32控制步进电机(ULN2003+28byj)
- Remote access to raspberry pie at home (Part 1)
- SSL证书退款说明
- [Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
- If you were a golang interviewer, what questions would you ask?
- Image attribute of input: type attribute of fashion cloud learning -h5
- Zigbee之CC2530最小系统及寄存器配置(1)
猜你喜欢

Resolve disagrees about version of symbol device_ create

box-sizing

Trier les principales utilisations de l'Agent IP réseau

4. DRF permission & access frequency & filtering & sorting

云原生KubeSphere部署Mysql

Introduction to servlet listener & filter

Object. The disorder of key value array after keys

Customize the shortcut options in El date picker, and dynamically set the disabled date

使用Source Insight查看编辑源代码

QT one process runs another
随机推荐
QT interprocess communication
Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
Jiachen chapter Genesis "inner universe" joint Edition
大家帮我看一下这是啥情况,MySQL5.5的。谢了
[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
[vulnhub range] - DC2
Introduction to servlet listener & filter
有趣的IDEA插件推荐,给你的开发工作增添色彩
Customize the shortcut options in El date picker, and dynamically set the disabled date
将新增和编辑的数据同步更新到列表
风尚云网学习-h5的input:type属性的image属性
No idle servers? Import OVF image to quickly experience smartx super fusion community version
0基础可以考CPDA数据分析师证书吗
SSM framework series - annotation development day2-2
世界读书日:我想推荐这几本书
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
Servlet监听器&过滤器介绍
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
leetcode:437. Path sum III [DFS selected or not selected?]
如何实现点击一下物体播放一次动画