Unity 设计模式-简单工厂模式和其他好用的通用代码块
1.

2.加法操作类
using System.Collections;
using System.Collections.Generic;
using UnityEngine; //加法操作类
public class AddOperation : Operation
{
//重写父类方法
public override double GetResult()
{
double dReult = 0;
dReult = m_dNumberA + m_dNumberB;
return dReult;
}
}
3.减法操作类
using System.Collections;
using System.Collections.Generic;
using UnityEngine; //减法操作类
public class SubOperation : Operation
{
//重写父类方法
public override double GetResult()
{
double dReult = 0;
dReult = m_dNumberA - m_dNumberB;
return dReult;
}
}
4.乘法操作类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 乘法操作类
/// </summary>
public class RideOperation : Operation
{
public override double GetResult()
{
double dResult = 0;
dResult = m_dNumberA * m_dNumberB;
return dResult;
}
}
5.公共类
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class Operation
{
public double m_dNumberA;
public double m_dNumberB;
//虚方法提供子类重写
public virtual double GetResult()
{
double dResult = 0;
return dResult;
} } public class OperationFactory
{
public Operation CreateOperate(string str)
{
Operation operation = null;
switch (str)
{
case "+":
operation = new AddOperation ();
break;
case "-":
operation = new SubOperation();
break;
case "*":
operation = new RideOperation();
break;
default:
break;
}
return operation;
}
}
6.使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class OperateTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
OperationFactory factory = new OperationFactory();
Operation operation = factory.CreateOperate("+");
operation.m_dNumberA = 5;
operation.m_dNumberB = 2;
double addResult = operation.GetResult();
Debug.Log("加:" + addResult);
Operation Suboperation = factory.CreateOperate("-");
Suboperation.m_dNumberA = 10;
Suboperation.m_dNumberB = 2;
double subResult = Suboperation.GetResult();
Debug.Log("减:" + subResult);
Operation Rideoperation = factory.CreateOperate("*");
Rideoperation.m_dNumberA = 2;
Rideoperation.m_dNumberB = 2;
double rideResult = Rideoperation.GetResult();
Debug.Log("乘:" + rideResult); //1+(1+2)+(1+2+3)+...+(1+2+3+...+10)之和
int i = 1, j = 1, s = 0, s1 =0;
while (j<=10)
{
while (i<=j)
{
s += i;
i++;
}
s1 += s;
j++;
}
Debug.Log(s1); } }
7.打印结果

8.好用的代码块
单例模式 代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 单例模式基类模块的作用主要是——减少单例模式重复代码的书写
/// </summary>
/// <typeparam name="T"></typeparam>
public class BaseManager<T> where T :new ()
{
private static T _instance;
public static T GetInstance()
{
if (_instance ==null)
{
_instance = new T();
}
return _instance;
} }
8.1 怎么使用单例,新建脚本继承它就行了 需要新建Resources文件夹 放两个预制体测试 一个Cube一个Sphere 具体使用情况自行扩展即可

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class CeShi2 : BaseManager <CeShi2>
{
public int oko = 0;
}
怎么继承单例的

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class CeShi3 : BaseManager <CeShi3>
{
public bool bol = false;
}
怎么继承单例的

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class CeShi1 : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
CeShi2.GetInstance().oko++;
CeShi3.GetInstance().bol = true;
Debug.Log(CeShi2.GetInstance().oko
+" " + CeShi3.GetInstance().bol);
} // Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//向缓存池中拿东西
PoolManager.GetInstance().GetObj("Cube");
}
if (Input.GetMouseButtonDown(1))
{
PoolManager.GetInstance().GetObj("Sphere");
}
Debug.Log(PoolManager.GetInstance().pool1Dic.Count);
}
}
怎么使用单例的
8.2 缓存池模块 代码如下 怎么调用的在上面脚本Update里面点击鼠标调用的缓存池拿东西

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 缓存池模块
/// </summary>
public class PoolManager : BaseManager <PoolManager>
{
//这里是缓存池模块 //创建字段存储容器
public Dictionary<string, List<GameObject>> pool1Dic = new Dictionary<string, List<GameObject>>(); private GameObject poolObj;
//取得游戏物体
public GameObject GetObj(string name)
{
GameObject obj = null;
//ContainsKey判断是否包含指定的“键名”
//.count 获得符合条件的个数
if (pool1Dic.ContainsKey(name) && pool1Dic[name].Count > 0)
{
//取得List中的第一个
obj = pool1Dic[name][0];
//移除第零个(这样才能允许同时创建多个物体)
//这样才是真正的“拿出来”
pool1Dic[name].RemoveAt(0);
}
else
{
//缓存池中没有该物体,我们去目录中加载
//外面传一个预设体的路径和名字,我内部就去加载它
//Resources类允许你从指定的路径查找或访问资源(api)
obj = GameObject.Instantiate(Resources.Load<GameObject>(name));
//创建对象后,将对象的名字与池中名字相符
obj.name = name;
}
//让物体显示出来
obj.SetActive(true);
obj.transform.parent = null;
return obj;
} //外界返还游戏物体
public void PushObj(string name, GameObject obj)
{
if (poolObj == null)
{
poolObj = new GameObject("Pool"); }
//将这个物体设置父亲为空物体
obj.transform.parent = poolObj.transform; //让物体失活
obj.SetActive(false);
//里面有记录这个键(有这个抽屉)
if (pool1Dic.ContainsKey(name))
{
pool1Dic[name].Add (obj);
}
//未曾记录这个键(没有这个抽屉)
else
{
pool1Dic.Add(name, new List<GameObject>() { obj });
}
}
}
缓存池代码
8.3 下面加一个缓存池返还东西 脚本直接挂在物体上即可

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 挂到物体身上的
/// </summary>
public class DelPush : MonoBehaviour
{
void OnEnable()
{
//unity自带的延迟方法 在time秒后,延迟调用方法methodName
Invoke("Push", 1);
} //放回去
void Push()
{
PoolManager.GetInstance().PushObj(transform.name, this.gameObject);
}
}
挂在物体上的代码
简单记录一下
Unity 设计模式-简单工厂模式和其他好用的通用代码块的更多相关文章
- C++设计模式——简单工厂模式
简单工厂模式(Simple Factory Pattern) 介绍:简单工厂模式不能说是一个设计模式,说它是一种编程习惯可能更恰当些.因为它至少不是Gof23种设计模式之一.但它在实际的编程中经常被用 ...
- 3. 星际争霸之php设计模式--简单工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 设计模式 — 简单工厂模式(staticFactory)
这篇博文介绍简单工厂模式,设计模式并不是固定的二十三种,不同的书介绍的可能有出入,这篇介绍的简单工厂模式在有些书上就忽略不介绍了.设计模式是一套被反复使用的.多数人知晓的.经过分类编目的.代码设计经验 ...
- Golang设计模式—简单工厂模式(Simple Factory Pattern)
Golang设计模式--简单工厂模式 背景 假设我们在做一款小型翻译软件,软件可以将德语.英语.日语都翻译成目标中文,并显示在前端. 思路 我们会有三个具体的语言翻译结构体,或许以后还有更多,但现在分 ...
- 深入浅出设计模式——简单工厂模式(Simple Factory)
介绍简单工厂模式不能说是一个设计模式,说它是一种编程习惯可能更恰当些.因为它至少不是Gof23种设计模式之一.但它在实际的编程中经常被用到,而且思想也非常简单,可以说是工厂方法模式的一个引导,所以我想 ...
- C#设计模式--简单工厂模式
简单工厂模式是属于创建型模式,但不属于23种GOF设计模式之一. 举一个例子:一个公司有不同的部门,客户根据需要打电话到不同的部门.客户相当于上端,不同部门相当于下端.不使用简单工厂模式来实现的例子如 ...
- 设计模式 | 简单工厂模式(static factory method)
按理说应该把书全都看完一遍,再开始写博客比较科学,会有比较全面的认识. 但是既然都决定要按规律更新博客了,只能看完一个设计模式写一篇了. 也算是逼自己思考了,不是看完就过,至少得把代码自己都敲一遍. ...
- Yii2设计模式——简单工厂模式
除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成"耦合"问题. 应用举例 yii\db\mysql\Sc ...
- Yii2 设计模式——简单工厂模式
除了使用 new 操作符之外,还有更多的制造对象的方法.你将了解到实例化这个活动不应该总是公开进行,也会认识到初始化经常造成“耦合”问题. 应用举例 yii\db\mysql\Schema 中: // ...
- 在商城系统中使用设计模式----简单工厂模式之在springboot中使用简单工厂模式
1.前言: 不了解简单工厂模式请先移步:在商城中使用简单工厂.在这里主要是对springboot中使用简单工厂模式进行解析. 2.问题: 什么是简单工厂:它的实现方式是由一个工厂类根据传入的参数,动态 ...
随机推荐
- 使用Promethues和Grafana监控Flink
之前使用Influxdb储存Metrics经常会出现数据写不进去的问题,当Influxdb重启之后又能写进去,遂将数据存储部分换成Promethues,因为Flink采用PutGateway的方式,需 ...
- 第二性 合卷本 横本.EPUB
书本详情 第二性台版 作者: 西蒙.德.波娃(Simone de Beauvoir)出版社: 貓頭鷹原作名: Le Deuxième Sexe译者: 邱瑞鑾出版年: 2013-10页数: 1136装帧 ...
- 第八章用matplotlib、seaborn、pyecharts绘制散点图
文章目录 散点图 matplotlib绘制散点图 seaborn绘制散点图 pyecharts绘制散点图 源码地址 本文可以学习到以下内容: matplotlib 中文乱码解决办法 seaborn 中 ...
- IC杂记
BNF(Backus-Naur Form) 巴科斯范式, 以美国人巴科斯(Backus)和丹麦人诺尔(Naur)的名字命名的一种形式化的语法表示方法,用来描述语法的一种形式体系,是一种典型的元语言.又 ...
- typora文件中不显示公式
行内公式在typora中不显示 解决办法 打开typora--文件(F)--偏好设置--markdown--内联公式--打勾选中 若改后没有反应,关闭重新打开.
- go 的internal 目录
Go 语言中的软件包推荐按照:组织名/项目名 的形式安排软件包的文件目录结构,一般「项目名」文件目录下还会按照功能.抽象约定.具体实现等维度再划分一些子目录.在 Go 语言里包的导入路径不同则被判定为 ...
- MyBatis_04(MyBatis获取“参数值”的两种方式)
MyBatis获取"参数值"的两种方式 (重点!!!) MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串拼接 , #{}的本质就是占位符赋值 ${}使用字 ...
- vue 项目中引入图片使用相对路径,图片不显示的问题
在 vue 项目中引入图片,路径为相对路径时,会显示 src="[object Module]" 采用 import 方式引入图片,再设置到 src 中 会正常显示 file-lo ...
- shell - scriptreplay timing.log output.session
script -t 2> timing.log -a output.session cmd cmd cmd exit scriptreplay timing.log output.session ...
- locust中的监听器
locust的master相关的几个监听器: 心跳监听器: 一个while循环,不断判断所有client当前的心跳状况,如果有一个client失去了心跳,就打印了一个警告日志,如果所有client都失 ...