当前位置:网站首页>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
边栏推荐
- C#控制相机,旋转,拖拽观察脚本(类似Scenes观察方式)
- SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
- 12. Constraints
- Shapley Explanation Networks
- 利用网页表格导出EXCEL表格加线框及表格内部间距的问题
- Moment. Format of format method function in JS
- Game assisted script development journey
- NodeJS(四) 字符读取
- Daily question | fear dominated by reverse linked list
- 利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
猜你喜欢

Use of command line parameter passing library argparse

Configure NPM

Django uses MySQL database to solve error reporting

FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning

js之DOM事件

基于NLP的软件安全研究(二)

ABAP 从CDS VIEW 发布OData Service示例

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

ABAP 7.4 SQL Window Expression

Install and configure Taobao image NPM (cnpm)
随机推荐
Dropping Pixels for Adversarial Robustness
Page dynamic display time (upgraded version)
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
King glory - unity learning journey
SAP Excel 已完成文件级验证和修复。此工作簿的某些部分可能已被修复或丢弃。
Apache Hudi 如何加速传统的批处理模式?
Custom time format (yyyy-mm-dd HH: mm: SS week x)
C # use laida criterion (3) σ Criteria) reject abnormal data (.Net reject singular values in a group of data)
Samplecamerafilter
快速排序
Thorough inquiry -- understanding and analysis of cocos2d source code
系统与软件安全研究(二)
Understanding of STL container
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
Towords Open World Object Detection
ES6使用递归实现深拷贝
SampleCameraFilter
C# 文本文件的查找及替换(WinForm)
系统与软件安全研究(一)
自己封装unity的Debug函数