当前位置:网站首页>如何实现点击一下物体播放一次动画
如何实现点击一下物体播放一次动画
2022-04-23 12:46:00 【一个不会码代码的小恐龙】
目录
实际作用:依靠鼠标的点击物体,并播放对应的动画(按照一定的顺序)
假设A->B->C 动画播放的顺序我们就要按照这个顺序播放
场景准备
①在场景中创建如下三个物体并给他们分别加上对应的Tag
②由于我的目的是让每一个物体只播放一段动画,所以我选用的是animation
注意 unity 中ctrl+6 create的动画默认的是animator的动画,我们可以提前在物体上添加animation组件,或者是将animator动画更改为animation类型的动画
我是直接现在要创建的物体上先加上animation组件。
代码编写
①首先是创建一个instanceManage单例
②给每个物体创建脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InstanceManage : MonoBehaviour
{
//为了能实现动画必须按照我想要的顺序播放所以我选择了栈
//栈是一种先进后出的数据结构
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);
}
//这是我想要的动画的执行顺序限制性 cube -> cap -> squere
//由于栈是先进后出的所以这里要把cube放在栈的最上面
private void IinitializeStack()
{
StartName.Push("sphere");
StartName.Push("cap");
StartName.Push("cube");
}
//查看顺序是否正确
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))//判断栈顶元素是否为当前点击的物体
{
animation.Play();
Debug.Log(this.gameObject.name);
}
}
}
版权声明
本文为[一个不会码代码的小恐龙]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_52021450/article/details/124307785
边栏推荐
- Homomorphic encryption technology learning
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
- [daily question] chessboard question
- bert-base-chinese下载(智取)
- On using go language to create websocket service
- Please help me see what this is, mysql5 5. Thanks
- Calculate the past date and days online, and calculate the number of live days
- Remote sensing image classification and recognition system based on convolutional neural network
- Markdown grammar learning
- Lesson 25 static member variables of classes
猜你喜欢
随机推荐
BUUCTF WEB [GXYCTF2019]禁止套娃
RT-thread中关键词解释及部分API
The continuous construction of the Internet industry platform is not only able to collect traffic
uni-app 原生APP-云打包集成极光推送(JG-JPUSH)详细教程
Uni app native app cloud packaging integrated Aurora push (jg-jpush) detailed tutorial
Keyword interpretation and some APIs in RT thread
QT one process runs another
Kubernetes 入门教程
Qt绘制图像
传统企业如何应对数字化转型?这些书给你答案
Sort out several uses of network IP agent
标签与路径
98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request
How to prevent the website from being hacked and tampered with
A graphic designer's fantasy world | ones characters
BUUCTF WEB [BJDCTF2020]The mystery of ip
0基础可以考CPDA数据分析师证书吗
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
NPDP | how can product managers not be excluded by programmers?
Message queuing overview