༺༽༾ཊ—Unity之-05-抽象工厂模式—ཏ༿༼༻

首先创建一个项目,

在这个初始界面我们需要做一些准备工作,

建基础通用文件夹,

创建一个Plane 重置后 缩放100倍 加一个颜色,

任务:使用 抽象工厂模式 创建 人物与宠物 模型,

首先资源商店下载 人物与宠物 模型,

拖拽至场景中,并完全解压缩

重命名为Role1放进自己的预制体包Prefabs后在场景中删除,

同样手法下载宠物模型最后放进自己的预制体包Prefabs中,

接下来编写代码:

1.创建脚本【抽象产品类之人物类】

双击AbsRole.cs编写代码:

using UnityEngine;

public abstract class AbsRole{

public GameObject Role { get; set; }

public abstract void Load();

}

2.创建脚本【具体产品类之人物类】

双击RoleA.cs编写代码:

using UnityEngine;

public class RoleA : AbsRole{

public override void Load(){

Role = Resources.Load<GameObject>("Prefabs/role1");

if (Role != null)

Role = GameObject.Instantiate(Role, new Vector3(0, 0, 0), Quaternion.identity);

}

}

3.创建脚本【抽象工厂类】

public abstract class AbstractFactory{

public abstract AbsRole GetRole(string type);

}

public class Factory : AbstractFactory{

public override AbsRole GetRole(string type){

AbsRole role;

switch (type){

case "RoleA":

role = new RoleA();

break;

default:

role = null;

break;

}

return role;

}

}

4.创建脚本【主类】

using UnityEngine;

public class Main : MonoBehaviour{

public AbsRole role;

public string type;

void Start(){

AbstractFactory roleFactory = new Factory();

role = roleFactory.GetRole("RoleA");

if (role != null)

role.Load();

else

Debug.LogError("空引用");

}

}

回到unity中将主类Main类挂载在地面上Plane,

运行即生成,

接下来添加宠物类,抽象工厂真正用到的地方,

创建脚本:

using UnityEngine;

public abstract class AbsPet{

public GameObject Pet { get; set; }

public abstract void Load();

}

创建脚本:

using UnityEngine;

public class PetA : AbsPet{

public override void Load(){

Pet = Resources.Load<GameObject>("Prefabs/pet1");

if (Pet != null)

Pet = GameObject.Instantiate(Pet, new Vector3(3, 0, 0), Quaternion.identity);

}

}

修改脚本:

public abstract class AbstractFactory{

public abstract AbsRole GetRole(string type);

public abstract AbsPet GetPet(string type);

}

修改脚本:

public class Factory : AbstractFactory{

public override AbsPet GetPet(string type){

AbsPet pet;

switch (type){

case "PetA":

pet = new PetA();

break;

default:

pet = null;

break;

}

return pet;

}

public override AbsRole GetRole(string type){

AbsRole role;

switch (type){

case "RoleA":

role = new RoleA();

break;

default:

role = null;

break;

}

return role;

}

}

修改主类:

using UnityEngine;

public class Main : MonoBehaviour{

public AbsRole role;

public AbsPet pet;

public string type;

void Start(){

AbstractFactory roleFactory = new Factory();

role = roleFactory.GetRole("RoleA");

if (role != null)

role.Load();

else

Debug.LogError("人物空引用");

AbstractFactory petFactory = new Factory();

pet = petFactory.GetPet("PetA");

if (pet != null)

pet.Load();

else

Debug.LogError("宠物空引用");

}

}

运行项目即完成,

End.

相关推荐
张晓~183399481212 小时前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
almighty274 小时前
C# DataGridView表头自定义设置全攻略
数据库·c#·winform·datagridview·自定义表头
arbboter6 小时前
【自动化】深入浅出UIAutomationClient:C#桌面自动化实战指南
运维·c#·自动化·inspect·uiautomation·uia·桌面自动化
文弱书生6567 小时前
5.后台运行设置和包设计与实现
服务器·开发语言·c#
小咪一会8 小时前
JVM 基础
jvm·1024程序员节
大飞pkz10 小时前
【设计模式】题目小练2
开发语言·设计模式·c#·题目小练
csdn_aspnet12 小时前
MongoDB C# .NetCore 驱动程序 序列化忽略属性
mongodb·c#·.netcore
浪扼飞舟12 小时前
c#基础二(类和对象,构造器调用顺序、访问级别、重写和多态、抽象类和接口)
java·开发语言·c#
好望角雾眠16 小时前
第四阶段C#通讯开发-1:通讯基础理论,串口,通讯模式,单位转换,代码示例
开发语言·笔记·c#·串口·通讯
薄荷撞~可乐1 天前
C#Task(Api)应用
开发语言·c#