首先我们打开一个项目

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

建基础通用包

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

更换天空盒(个人喜好)


任务:使用【UI】点击生成6种车零件组装不同类型车 【建造者模式】
首先资源商店下载车模型

将Car导入资源拖拽至场景 将导入包放进WBag外包方便整理

将Car完全解压缩 为了取出小车各部分零件 方便我们组装车

拆分第一个组件 车体 命名为CarBody 放进预制体包Prefabs

删除场景中的 CarBody

拆分第二个组件 车座命名为 CarShadle


将剩余物体 设置 成空父物体的子物体 作为一个整体 命名为 CarWheels


准备工作做完后 接下我们做【建造者模式】
建造者模式属于23设计模式中的一种 是一种创建型模式
通常是为了 通过多种组件 由指挥者 拼装出 多种不同所需产品
建造者模式最低需要四个类就可以完成加上Main类中调用需要五个
首先需要 【产品类】
其实需要 【抽象建造类】
再其次需要 【建造具体类】
再再其次需要 【指挥者类】
最后我们通过Main继承Mono挂载再脚本上调用即可
实现:
1.创建脚本【产品类】:


using UnityEngine;
public class Car{
public GameObject CarPart {
get;
set;
}
}
2.【抽象建造类】:

public abstract class IBuildCar{
public abstract void SetCarBody();
public abstract Car GetCar();
}
3.【建造具体类】:

using UnityEngine;
public class BuildCarA : IBuildCar{
Car car;
public BuildCarA(Car car){
this.car = car;
}
public override void SetCarBody(){
car.CarPart = Resources.Load<GameObject>("Prefabs/CarBody");
if(car.CarPart != null)
GameObject.Instantiate(car.CarPart,new Vector3(0,0,0),Quaternion.identity);
}
public override Car GetCar(){
return car;
}
}
4.【指挥者类】:

public class Director{
public IBuildCar type;
public Director(IBuildCar type){
this.type = type;
}
public void GetCarBody() {
type.SetCarBody();
}
}
5.【控制挂载类】

using UnityEngine;
public class Main : MonoBehaviour{
private void Start(){
Car car = new Car();
//里氏替换
IBuildCar type = new BuildCarA(car);
Director d1 = new Director(type);
d1.GetCarBody();
}
}
我们回到unity场景中创建一个空物体改名Obj 重置位置 将Main脚本挂载
运行即可生成

接下来我们利用UI + 事件 控制【建造者模式】
1.【产品类】 不动代码
2.【抽象建造类】 不动代码
3.【建造具体类】 修改代码:

using UnityEngine;
public class BuildCarA : IBuildCar{
//按钮计数器
int count = 0;
Car car;
public BuildCarA(Car car){
this.car = car;
}
public override void SetCarBody(){
count++;
car.CarPart = Resources.Load<GameObject>("Prefabs/CarBody");
if(car.CarPart != null && count % 2 != 0)
GameObject.Instantiate(car.CarPart,new Vector3(0,1,0),new Quaternion(0,10,10,0));
}
public override Car GetCar(){
return car;
}
}
4.【指挥者类】 不动代码
5.【控制挂载类】 修改代码:

using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour{
//开放Button按钮
public Button setCarBodyButton;
//按钮计数器
int count = 0;
private void Start(){
Car car = new Car();
//里氏替换
IBuildCar type = new BuildCarA(car);
Director d1 = new Director(type);
setCarBodyButton.onClick.AddListener(() => d1.GetCarBody());
}
public void DestroyCarBody(){
if (count % 2 == 0)
Destroy(GameObject.Find("CarBody(Clone)"));
}
}
我们回到unity场景中创建button 【做一个折叠按钮】








ctrl + d复制 Button

将未开启的Button 改名为 Close 开启的Button改名为 open
修改Panel尺寸



新建Button 作为 创建车体的按钮 改名为 ButtonA

开启 按钮事件:

将Obj 拖拽至 框选栏 并选择

选择我们脚本代码中自定义的函数

最后将ButtonA 代码 绑定在 Main中

运行
点击ButtonA 按钮

再点消失

关闭选项

目前已完成
如果想在建造其他组件则需要:
1.【产品类】 不动代码
2.【抽象建造类】 增加代码:

public abstract class IBuildCar{
public abstract void SetCarBody();
public abstract void SetCarWheels();
public abstract Car GetCar();
}
3.【建造具体类】 增加代码:

using UnityEngine;
public class BuildCarA : IBuildCar{
//按钮计数器
int count = 0;
Car car;
public BuildCarA(Car car){
this.car = car;
}
public override void SetCarBody(){
count++;
car.CarPart = Resources.Load<GameObject>("Prefabs/CarBody");
if(car.CarPart != null && count % 2 != 0)
GameObject.Instantiate(car.CarPart,new Vector3(-14,1,0),new Quaternion(0,10,10,0));
}
public override void SetCarWheels()
{
count++;
car.CarPart = Resources.Load<GameObject>("Prefabs/CarWheels");
if (car.CarPart != null && count % 2 != 0)
GameObject.Instantiate(car.CarPart, new Vector3(-14, 0.24f, 25.29f), Quaternion.identity);
}
public override Car GetCar(){
return car;
}
}
4.【指挥者类】 增加代码:

public class Director{
public IBuildCar type;
public Director(IBuildCar type){
this.type = type;
}
public void GetCarBody() {
type.SetCarBody();
}
public void GetCarWheels()
{
type.SetCarWheels();
}
}
5.【控制挂载类】 增加代码:

using UnityEngine;
using UnityEngine.UI;
public class Main : MonoBehaviour{
//开放Button按钮
public Button setCarBodyButton;
public Button setCarWheelsButton;
//按钮计数器
int count = 0;
private void Start(){
Car car = new Car();
//里氏替换
IBuildCar type = new BuildCarA(car);
Director d1 = new Director(type);
setCarBodyButton.onClick.AddListener(() => d1.GetCarBody());
setCarWheelsButton.onClick.AddListener(() => d1.GetCarWheels());
}
public void DestroyCarBody(){
if (count % 2 == 0)
Destroy(GameObject.Find("CarBody(Clone)"));
}
public void DestroyCarWheels()
{
if (count % 2 == 0)
Destroy(GameObject.Find("CarWheels(Clone)"));
}
}
回到unity场景中 重复事件操作即可完成运行
