首先创建一个项目,

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

建基础通用文件夹,

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

任务1:使用 建造者模式 创建三种 金刚猿猴 零部件 拼接组合

首先资源商店下载 金刚猿猴 模型,

将资源对象拖拽至场景并完全解压缩,

将金刚组件拆分为三份重命名为Ape1-3放进自己的预制体包Prefabs并在场景中删除,

1.创建脚本【产品类】:

using UnityEngine;
public class Ape{
public GameObject ApeBody{ get;set; }
}

2.【抽象建造类】:

public abstract class AbsBuildApe{
public abstract void SetApeBody();
public abstract Ape GetApe();
}

3.【建造具体类】:

using UnityEngine;
public class BuildApeA : AbsBuildApe{
Ape _ape;
public BuildApeA(Ape ape){
_ape = ape;
}
public override void SetApeBody(){
_ape.ApeBody = Resources.Load(“Prefabs/ape1”);
if (_ape.ApeBody != null)
GameObject.Instantiate(_ape.ApeBody, new Vector3(0, 0, 0), Quaternion.identity);
}
public override Ape GetApe(){
return _ape;
}
}

4.【指挥者类】:

public class Director{
public AbsBuildApe _type;
public Director(AbsBuildApe type){
_type = type;
}
public void GetApeBody(){
_type.SetApeBody();
}
}

5.【主类】

using UnityEngine;
public class Main : MonoBehaviour{
void Start(){
Ape ape = new Ape();
//里氏替换
AbsBuildApe type = new BuildApeA(ape);
Director d1 = new Director(type);
d1.GetApeBody();
}
}

修改代码:

public abstract class AbsBuildApe{
public abstract void SetApeBody();
public abstract void SetApeBody1();
public abstract Ape GetApe();
}

修改代码:

using UnityEngine;
public class BuildApeA : AbsBuildApe{
Ape _ape;
public BuildApeA(Ape ape){
_ape = ape;
}
public override void SetApeBody(){
_ape.ApeBody = Resources.Load(“Prefabs/ape1”);
if (_ape.ApeBody != null)
GameObject.Instantiate(_ape.ApeBody, new Vector3(0, 0, 0), Quaternion.identity);
}
public override Ape GetApe(){
return _ape;
}
public override void SetApeBody1(){
_ape.ApeBody = Resources.Load(“Prefabs/ape2”);
if (_ape.ApeBody != null) {
// 设置旋转角度,例如绕y轴旋转90度
Quaternion rotation = Quaternion.Euler(-90, 0, 0);
// 实例化物体并设置位置和旋转
GameObject.Instantiate(_ape.ApeBody, new Vector3(0.5f, 0.5f, -0.38f), rotation);
}
}
}

修改代码:

public class Director{
public AbsBuildApe _type;
public Director(AbsBuildApe type){
_type = type;
}
public void GetApeBody(){
_type.SetApeBody();
}
public void GetApeBody1() {
_type.SetApeBody1();
}
}

修改代码:

运行即可完成 建造者模式任务,

下面搭配原型模式进行修改代码:

【解析】通常对于这种比较繁琐复杂的建造者模式好不容易建造了一个金刚类型,如果再有需要,再次使用建造者模式还会如此麻烦,为了用简单的方式实现,需要配合原型模式,让这个复杂的类完成自我复制,也就是说在这个复杂的类中按一个原型方法,让这个类可以自我复制,

修改代码:

using UnityEngine;
public class Ape {
public GameObject ApeBody { get; set; }
public Ape(GameObject apeBody){
ApeBody = apeBody;
}
public Ape Clone(){
GameObject clonedApeBody = null;
if (ApeBody != null)
clonedApeBody = GameObject.Instantiate(ApeBody);
return new Ape(clonedApeBody);
}
}

修改代码:

public abstract class AbsBuildApe{
public abstract void SetApeBody();
public abstract void SetApeBody1();
public abstract Ape GetApe();
public abstract void SetApeBody2();
}

using UnityEngine;
public class BuildApeA : AbsBuildApe{
Ape _ape;
public BuildApeA(Ape ape){
_ape = ape;
}
public override void SetApeBody(){
_ape.ApeBody = Resources.Load(“Prefabs/ape1”);
if (_ape.ApeBody != null)
GameObject.Instantiate(_ape.ApeBody, new Vector3(0, 0, 0), Quaternion.identity);
}
public override Ape GetApe(){
return _ape;
}
public override void SetApeBody1(){
_ape.ApeBody = Resources.Load(“Prefabs/ape2”);
if (_ape.ApeBody != null) {
// 设置旋转角度,例如绕y轴旋转90度
Quaternion rotation = Quaternion.Euler(-90, 0, 0);
// 实例化物体并设置位置和旋转
GameObject.Instantiate(_ape.ApeBody, new Vector3(0.5f, 0.5f, -0.38f), rotation);
}
}
public override void SetApeBody2(){
_ape.ApeBody = Resources.Load(“Prefabs/ape3”);
if (_ape.ApeBody != null)
GameObject.Instantiate(_ape.ApeBody, new Vector3(1, 0, 0), Quaternion.identity);
}
}

修改代码:

using System;
public class Director : ICloneable{
public AbsBuildApe _type;
public Director(AbsBuildApe type){
_type = type;
}
public object Clone(){
// 调用 AbsBuildApe 类的 Clone 方法
return _type.GetApe().Clone();
}
public void GetApeBody(){
_type.SetApeBody();
}
public void GetApeBody1() {
_type.SetApeBody1();
}
public void GetApeBody2(){
_type.SetApeBody2();
}
}
修改代码:

运行项目即用原型模式实现了 金刚的自我复制 ,即与建造者模式的结合

end.