当前位置:网站首页>Unity C# 单例模式 学习复习笔记
Unity C# 单例模式 学习复习笔记
2022-04-23 06:28:00 【Allen7474】
Unity C# 单例模式 学习复习笔记
什么是单例模式:优缺,理解
Unity单例模式+例子_就一枚小白的博客-CSDN博客_unity 单例
学习链接:开关门 案例【Unity3D学习】Unity3D用单例模式实现简单交互功能、开关门 开关灯_哔哩哔哩_bilibili
单例脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sc03Singletion : MonoBehaviour
{
//单例模式
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);
}
}
门开关脚本:


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()
{
//简单方法
/* if (Ani_Door.speed==0)
{
Ani_Door.speed = 1;
}
Ani_Door.SetBool(state, true);*/
//单例模式
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);
}
}
灯控制脚本:


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://blog.csdn.net/Allen7474/article/details/124320178
边栏推荐
- 利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
- url转成对象
- Install and configure Taobao image NPM (cnpm)
- js之DOM学习三种创建元素的方式
- Apache Hudi 如何加速传统的批处理模式?
- 读取修改resource文件夹下的json文件
- Redis connection error err auth < password > called without any password configured for the default user
- SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。
- ABAP 实现发布RESTful服务供外部调用示例
- js之DOM事件
猜你喜欢

对js中argumens的简单理解

SAP SALV14 后台输出SALV数据可直接保存文件,发送Email(带排序、超链接、筛选格式)

Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments

C# 多个矩形围成的多边形标注位置的问题

Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间

Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight

js之排他思想及案例

移动端布局(3D转换、动画)

SAP CR传输请求顺序、依赖检查

对复杂字典Dictionary&lt;T1,T2&gt;排序问题
随机推荐
平面定义-平面方程
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
electron-builder打包报错:proxyconnect tcp: dial tcp :0: connectex
防抖和节流
Custom time format (yyyy-mm-dd HH: mm: SS week x)
ABAP CDS VIEW WITH ASSOCIATION示例
promise all的实现
Scrapy modifies the time in the statistics at the end of the crawler as the current system time
双面显示的shader
层次输出二叉树
Thorough inquiry -- understanding and analysis of cocos2d source code
踩坑日记:Unable to process Jar entry [module-info.class]
ES6 uses recursion to implement deep copy
instanceof的实现原理
Rethink | open the girl heart mode of station B and explore the design and implementation of APP skin changing mechanism
反转链表练习
Page dynamic display time (upgraded version)
Moment. Format of format method function in JS
Unity 获取一个资源被那些资源引用
Date object (JS built-in object)