当前位置: 首页 > news >正文

༺༽༾ཊ—Unity之-01-工厂方法模式—ཏ༿༼༻

首先创建一个项目,

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

建基础通用文件夹,

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

任务:使用工厂方法模式 创建 飞船模型,

首先资源商店下载飞船模型,

拖拽三种类型飞船模型至unity场景中,

将三种模型完全解压缩后放进自己的Prefabs包,

在unity场景中删除三个飞船模型,

接下来编写代码:

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

双击AbsShip.cs编写代码:

using UnityEngine;
public abstract class AbsShip{
    public GameObject Ship { get; set; }
    public abstract void Load();
}
2.创建脚本【具体产品类】

双击ShipA.cs编写代码:

using UnityEngine;
public class ShipA : AbsShip{
    public override void Load(){
        Ship = Resources.Load<GameObject>("Prefabs/ship1");
        if (Ship != null)
            Ship = GameObject.Instantiate(Ship, new Vector3(0, 0, 0), Quaternion.identity);
    }
}
3.创建脚本【工厂方法类】

public abstract class AbsFactory{
    public abstract AbsShip GetShip(string type);
}
public class Factory : AbsFactory{
    public override AbsShip GetShip(string type){
        AbsShip ship;
        switch (type){
            case "shipA":
                ship = new ShipA();
                break;
            default:
                ship = null;
                break;
        }
        return ship;
    }
}

4.创建脚本【主类】

using UnityEngine;
public class Main : MonoBehaviour{
    public AbsShip ship;
    public string type;
    void Start(){
        AbsFactory shipFactory = new Factory();
        ship = shipFactory.GetShip("shipA"); 
        if (ship != null)
            ship.Load(); 
        else
            Debug.LogError("空引用");
    }
}
回到unity中修改预制体文件名为ship1

将Main类挂载在地面Plane上,

运行项目即可生成ship1飞船,

如果需要拓展,添加ShipB具体产品类,

using UnityEngine;
public class ShipB : AbsShip{
    public override void Load(){
        Ship = Resources.Load<GameObject>("Prefabs/ship2");
        if (Ship != null)
            Ship = GameObject.Instantiate(Ship, new Vector3(3, 0, 0), Quaternion.identity);
    }
}

只需修改工厂类,

public abstract class AbsFactory{
    public abstract AbsShip GetShip(string type);
}
public class Factory : AbsFactory{
    public override AbsShip GetShip(string type){
        AbsShip ship;
        switch (type){
            case "shipA":
                ship = new ShipA();
                break;
            case "shipB":
                ship = new ShipB();
                break;
            default:
                ship = null;
                break;
        }
        return ship;
    }
}

运行项目即可完成,

End.

http://www.lryc.cn/news/291731.html

相关文章:

  • QT仪表盘小工具
  • 【2024】大三寒假再回首:缺乏自我意识是毒药,反思和回顾是解药
  • 计算机网络——网络层(3)
  • ROS2 学习笔记12:使用 colcon 构建软件包
  • 基于JAVA+SpringBoot+Vue的前后端分离的医院管理系统
  • npm淘宝镜像过期解决办法
  • Arduino 官网上下载和使用开发板
  • k8s学习-DaemonSet和Job
  • 【开源】SpringBoot框架开发海南旅游景点推荐系统
  • Windows10更新失败 错误 0x80070643、KB5034441的解决方法之二
  • SQL中LIMIT的简单用法
  • canvas自定义扩展方法:文字自动换行
  • 【2024全网最详细】Google 搜索命令终极指南
  • R-kknn包-类别插值可视化绘制
  • 探究HMAC算法:消息认证与数据完整性的完美结合
  • 10s 内得到一个干净、开箱即用的 Linux 系统
  • 轮转数组[中等]
  • 【SpringBoot系列】自动装配的魅力:Spring Boot vs 传统Spring
  • idea自动生成实体类
  • uniapp -- picker民族选择器
  • 生信学习笔记1:学习如何用OPLS-DA分析代谢组数据(从入门到掌握)
  • CDR2024最新版本怎么下载?Coreldraw相关快捷键教程分享
  • C语言实战项目<贪吃蛇>
  • 人工智能时代:AI提示工程的奥秘 —— 驾驭大语言模型的秘密武器
  • Idea编写mapper.xml文件提示表名和字段
  • 解密人工智能:探索机器学习奥秘
  • C语言第十四弹---函数递归
  • etcd自动化安装配置教程
  • 时间序列预测——GRU模型
  • 通用CI/CD软件平台TeamCity全新发布v2023.11——增强Git托管平台的集成