当前位置:网站首页>Unity C single case mode learning review notes
Unity C single case mode learning review notes
2022-04-23 07:53:00 【Allen7474】
Unity C# The singleton pattern Study review notes
What is singleton mode : excellent vacancy , understand
Unity The singleton pattern + Example _ Just a little white blog -CSDN Blog _unity Single case
Learning links : Open and close the door Case study 【Unity3D Study 】Unity3D Use singleton mode to realize simple interactive function 、 Open and close the door Turn on and off the lights _ Bili, Bili _bilibili
Singleton script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sc03Singletion : MonoBehaviour
{
// The singleton pattern
static Sc03Singletion instance;
public static Sc03Singletion Instance()
{
if (instance==null)
{
instance = new Sc03Singletion();
}
return instance;
}
public void AnimBegin(GameObject obj,Animator ani,string state)
{
ani = obj.GetComponent<Animator>();
if (ani.speed==0)
{
ani.speed = 1;
}
ani.SetBool(state,true);
}
public void AnimEnd(GameObject obj, Animator ani, string state)
{
if (ani.speed == 0)
{
return;
}
ani.SetBool(state, false);
}
}
Door switch script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sc03Door : MonoBehaviour
{
Animator Ani_Door;
string state;
private void Start()
{
state = "OpenOrClose";
Ani_Door = GetComponent<Animator>();
Ani_Door.speed = 0;
}
public void OpenDoor()
{
// Simple method
/* if (Ani_Door.speed==0)
{
Ani_Door.speed = 1;
}
Ani_Door.SetBool(state, true);*/
// The singleton pattern
Sc03Singletion.Instance().AnimBegin(this.gameObject,Ani_Door,state);
}
public void CloseDoor()
{
/*
if (Ani_Door.speed == 0)
{
return;
}
Ani_Door.SetBool(state, false);
*/
Sc03Singletion.Instance().AnimEnd(this.gameObject, Ani_Door, state);
}
}
Light control script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sc03Light : MonoBehaviour
{
Animator Ani_Light;
string state;
private void Start()
{
state = "lightOpenClose";
Ani_Light = GetComponent<Animator>();
Ani_Light.speed = 0;
}
public void LightOn()
{
Sc03Singletion.Instance().AnimBegin(this.gameObject, Ani_Light, state);
}
public void LightOff()
{
Sc03Singletion.Instance().AnimEnd(this.gameObject, Ani_Light, state);
}
}
版权声明
本文为[Allen7474]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230627431327.html
边栏推荐
猜你喜欢
Apache Hudi 如何加速传统的批处理模式?
linux下mysql数据库备份与恢复(全量+增量)
Houdini流体>>粒子流体导出到unity笔记
Protobuf 使用
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
常用Markdown语法学习
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
Protobuf use
Use of command line parameter passing library argparse
Dropping Pixels for Adversarial Robustness
随机推荐
VBA调用SAP RFC实现数据读取&写入
linux下mysql数据库备份与恢复(全量+增量)
MySQL8.0 安装/卸载 教程【Window10版】
快速排序
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
如何展示您的数字作品集:来自创意招聘人员的建议
The problem of exporting excel form with wireframe and internal spacing of form by using web form
Houdini > fluid, rigid body export, learning process notes
颜色转换公式大全及转换表格(31种)
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
05数组的使用
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
C#操作注册表全攻略
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
SVG中年月日相关的表达式
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
webflux文件上传下载
.NET 5 的新功能 What‘s new in .NET 5
Custom time format (yyyy-mm-dd HH: mm: SS week x)
Houdini fluid > > particle fluid export to unity note