当前位置:网站首页>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
边栏推荐
猜你喜欢

庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>

将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

Protobuf use

Unity C# 单例模式 学习复习笔记

Houdini > fluid, rigid body export, learning process notes

命令行参数传递库argparse的使用

VBA调用SAP RFC实现数据读取&写入

The page displays the current time in real time

常用Markdown语法学习

Use of command line parameter passing library argparse
随机推荐
C#操作注册表全攻略
面经总结2
Towords Open World Object Detection
平面定义-平面方程
快速排序
Electronic builder package error: proxyconnect TCP: Dial TCP: 0: connectex
Houdini流体>>粒子流体导出到unity笔记
Dropping Pixels for Adversarial Robustness
系统与软件安全研究(二)
ABAP ALV显示金额与导出金额不一致
Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
Dropping Pixels for Adversarial Robustness
IT高薪者所具备的人格魅力
Nodejs (four) character reading
Unity 获取一个资源被那些资源引用
NodeJS(四) 字符读取
Moment. Format of format method function in JS
Idea shortcut
Weblux file upload and download
SAP TR手动导入系统操作手册