DesignMode__unity__抽象工厂模式在unity中的应用、用单例模式进行资源加载 ...

打印 上一主题 下一主题

主题 1782|帖子 1782|积分 5346

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
目次

抽象工厂模式
思维导图
接口(抽象类)
工厂接口
抽象产品类
抽象武器接口
抽象人物接口
详细工厂和详细产品
详细工厂
(1)产品接口,生成详细人物
(2)武器接口,生成详细武器
详细产品的实现
Soldier类型
ShotGunA 类型
单例模式资源加载
测试
GameController
PerformanceTest


抽象工厂模式

思维导图


一个工厂内里可以生产多个产品
一个工厂可以生产一系列产品(一族产品),极大镌汰了工厂类的数量

接口(抽象类)

工厂接口

在工厂里声明创造的武器和产品
武器:创造武器方法创造详细的武器
人物:创造产品方法创造详细的人物
  1. public interface IGameFactory
  2. {
  3.     IWeapon GreateWeapon();
  4.     ICharacter CreateCharacter();
  5. }
复制代码
抽象产品类

抽象武器接口

用来生产不同的武器,武器类型
  1. /// <summary>
  2. /// 抽象武器接口-抽象产品
  3. /// </summary>
  4. public interface IWeapon
  5. {
  6.     void Use();//使用武器
  7.     void Display();//显示武器
  8. }
复制代码
抽象人物接口

用来生成不同的产品,人物类型
  1. /// <summary>
  2. /// 抽象产品
  3. /// </summary>
  4. public interface ICharacter
  5. {
  6.     void Display();//显示模型
  7. }
复制代码
详细工厂和详细产品

现代风格的详细工厂,返回详细的产品
详细工厂

(1)产品接口,生成详细人物

返回要生成的产品,Soldier类型
(2)武器接口,生成详细武器

  1. public class ModernGameFactory : IGameFactory
  2. {
  3.     public ICharacter CreateCharacter()
  4.     {
  5.         return new Soldier();
  6.     }
  7.     public IWeapon GreateWeapon()
  8.     {
  9.         return new ShotGunA();
  10.     }
  11. }
复制代码

详细产品的实现

Soldier类型

实现ICharacter接口,生产详细的人物
  1. /// <summary>
  2. /// 具体产品---士兵
  3. /// </summary>
  4. public class Soldier : ICharacter
  5. {
  6.     private GameObject _model;
  7.     public Soldier()
  8.     {
  9.         _model = ResourceManager.Instance.GetResource("Bot/SoldierA");
  10.     }
  11.     public void Display()
  12.     {
  13.         if (_model != null)
  14.         {
  15.             GameObject.Instantiate(_model);
  16.         }
  17.         else
  18.         {
  19.             Debug.LogError("Soldier model not found");
  20.         }
  21.     }
  22. }
复制代码
ShotGunA 类型

ShotGunA产品的生产
实现IWeapon接口
  1. public class ShotGunA : IWeapon
  2. {
  3.     private GameObject _model;
  4.     public ShotGunA()
  5.     {
  6.          _model = ResourceManager.Instance.GetResource("Weapon/LaserGun_A");
  7.     }
  8.     public void Display()
  9.     {
  10.         if (_model != null)
  11.         {
  12.             GameObject.Instantiate(_model);
  13.         }
  14.         else
  15.         {
  16.             Debug.LogError("ShotGunA model not found");
  17.         }
  18.     }
  19.     public void Use()
  20.     {
  21.         Debug.Log("使用武器");
  22.     }
  23. }
复制代码

单例模式资源加载

单例模式(Singleton Pattern):是一种创建对象的设计模式,确保一个类只有一个实例,并提供全局访问点。
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 单例模式
  6. /// </summary>
  7. public class ResourceManager
  8. {
  9.     //本类实例的引用
  10.     private static ResourceManager _instance;
  11.     //资源缓存器
  12.     private Dictionary<string, GameObject> _resourceCache = new Dictionary<string, GameObject>();
  13.     //为私有的字段准备的属性
  14.     public static ResourceManager Instance
  15.     {
  16.         get
  17.             //保证有且只有一个实例
  18.         {
  19.             if (_instance == null)
  20.             {
  21.                 _instance = new ResourceManager();
  22.             }
  23.             return _instance;
  24.         }
  25.     }
  26.     //获取资源的工作代码,从硬盘或者缓存中获取模型资源
  27.     //传入路径path
  28.     public GameObject GetResource(string path)
  29.     {
  30.         //询问资源存储器中是否包含当前路径(不需要重复加载)
  31.         if (!_resourceCache.ContainsKey(path))
  32.         {
  33.             GameObject resource = Resources.Load<GameObject>(path);
  34.             if (resource == null)
  35.             {
  36.                 Debug.LogError($"Failed to load resource at path: {path}");
  37.                 return null;
  38.             }
  39.             _resourceCache[path] = resource;
  40.         }
  41.         return _resourceCache[path];
  42.     }
  43. }
复制代码

测试

GameController

建个空物体挂上即可
功能:创建产品进行测试
  1. public class GameController : MonoBehaviour
  2. {
  3.     private ICharacter _character;
  4.     private IWeapon _weapon;
  5.     public void StartGame(IGameFactory factory)
  6.     {
  7.         try
  8.         {
  9.             _character = factory.CreateCharacter();         
  10.             _weapon = factory.GreateWeapon();
  11.             _character.Display();
  12.             _weapon.Use();
  13.         }
  14.         catch (System.Exception e)
  15.         {
  16.             Debug.LogError($"Error starting game: {e.Message}");
  17.         }
  18.     }
  19. }
复制代码
PerformanceTest

建个空物体挂上即可
功能:测试用抽象工厂模式创建物体和直接实例化物体的时间性能区别
(直接创建会快)
  1. public class PerformanceTest : MonoBehaviour
  2. {
  3.     private void Start()
  4.     {
  5.         //TestDirectInstantiation(500);
  6.         TestFactoryPattern(500);
  7.     }
  8.     //直接实例化
  9.     void TestDirectInstantiation(int count)
  10.     {
  11.         Stopwatch stopwatch = new Stopwatch();
  12.         stopwatch.Start();
  13.         List<GameObject> objects = new List<GameObject>();
  14.         GameObject prefab = Resources.Load<GameObject>("Bot/SoldierA");
  15.         for (int i = 0; i < count; i++)
  16.         {
  17.             objects.Add(GameObject.Instantiate(prefab));
  18.         }
  19.         stopwatch.Stop();
  20.         UnityEngine.Debug.Log($"Direct Instantiation ({count} objects): {stopwatch.ElapsedMilliseconds} ms");
  21.         
  22.         
  23.         // Clean up
  24.         foreach (var obj in objects)
  25.         {
  26.             GameObject.Destroy(obj);
  27.         }
  28.         objects.Clear();
  29.         Resources.UnloadUnusedAssets();
  30.     }
  31.     //抽象工厂的实例化测试
  32.     void TestFactoryPattern(int count)
  33.     {
  34.         Stopwatch stopwatch = new Stopwatch();
  35.         stopwatch.Start();
  36.         IGameFactory factory = new ModernGameFactory();
  37.         List<ICharacter> characters = new List<ICharacter>();
  38.         for (int i = 0; i < count; i++)
  39.         {
  40.             characters.Add(factory.CreateCharacter());
  41.             characters[i].Display();
  42.         }
  43.         stopwatch.Stop();
  44.         UnityEngine.Debug.Log($"Factory Pattern ({count} objects): {stopwatch.ElapsedMilliseconds} ms");
  45.         //Clean up
  46.         characters.Clear();
  47.         Resources.UnloadUnusedAssets();
  48.     }
  49. }
复制代码






免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

数据人与超自然意识

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表