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); } }
9.单例脚本 这种单例可以挂载到物体身上使用 ,用的时候也是直接调用
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ToolSingleCase<T> : MonoBehaviour where T :MonoBehaviour { private static T my_instance = null; public static T instance_ { get { if (my_instance ==null) { my_instance = FindObjectOfType<T>(); if (my_instance ==null) { GameObject go = new GameObject(typeof(T).Name); my_instance = go.AddComponent<T>(); DontDestroyOnLoad(my_instance); } return my_instance; } return my_instance; } } }
9.1 脚本绑定所示方法如下所示
//脚本写成如下所示,就可以调用本脚本里面的方法了 public class AllPanel : ToolSingleCase<AllPanel>{ }
简单记录一下
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2021-01-14 Unity 实现双屏显示或多个屏幕显示