文章目录
- 说明
- 实例1:构建游戏角色
- 实例2:构建游戏场景
- 实例3:构建UI界面
说明
在Unity中,建造者模式(Builder Pattern)是一种创建型设计模式,它通过分离对象构建过程的复杂性,允许您以更灵活和可扩展的方式创建不同变体的对象。这种模式尤其适用于需要构造具有多个可选或必须组件的对象,并且希望客户端代码无需了解内部构造细节的情况下。
以下是Unity中使用建造者模式的三个实例:
实例1:构建游戏角色
假设我们正在开发一个角色扮演游戏,游戏角色由多种部件组成,如身体、头部、装备、技能等。我们可以为游戏角色创建一个Builder类来逐个添加这些部分:
// 角色接口
public interface ICharacterBuilder
{void SetBody(BodyPart body);void SetHead(HeadPart head);void SetEquipment(Equipment equipment);void SetSkills(List<Skill> skills);// 构建完整角色的方法Character Build();
}// 具体的建造者实现
public class PlayerCharacterBuilder : ICharacterBuilder
{private BodyPart _body;private HeadPart _head;private Equipment _equipment;private List<Skill> _skills;public void SetBody(BodyPart body) => _body = body;public void SetHead(HeadPart head) => _head = head;public void SetEquipment(Equipment equipment) => _equipment = equipment;public void SetSkills(List<Skill> skills) => _skills = skills;public Character Build(){return new Character(_body, _head, _equipment, _skills);}
}// 客户端代码调用
var builder = new PlayerCharacterBuilder();
builder.SetBody(someBody);
builder.SetHead(someHead);
builder.SetEquipment(someEquipment);
builder.SetSkills(characterSkills);
var player = builder.Build();
实例2:构建游戏场景
在Unity中,构建复杂的场景也是一个可以利用建造者模式的过程。例如,一个SceneBuilder类可以用来逐步地添加游戏对象、灯光、摄像机和其他组件到场景中:
// 场景构建器接口
public interface ISceneBuilder
{void AddGameObject(GameObject go);void SetMainCamera(Camera camera);void AddLight(Light light);Scene BuildScene();
}// 具体场景构建器实现
public class LevelOneSceneBuilder : ISceneBuilder
{private List<GameObject> _gameObjects = new List<GameObject>();private Camera _mainCamera;private List<Light> _lights = new List<Light>();public void AddGameObject(GameObject go) => _gameObjects.Add(go);public void SetMainCamera(Camera camera) => _mainCamera = camera;public void AddLight(Light light) => _lights.Add(light);public Scene BuildScene(){var scene = new Scene(); // Unity 不直接支持这样创建场景,此处仅做示意foreach (var go in _gameObjects)SceneManager.MoveGameObjectToScene(go, scene);_mainCamera.gameObject.SetActive(true); // 设置主摄像机foreach (var light in _lights)SceneManager.MoveGameObjectToScene(light.gameObject, scene);return scene; // 返回构建完成的场景(实际应用中会有所不同)}
}// 使用示例
var levelBuilder = new LevelOneSceneBuilder();
levelBuilder.AddGameObject(playerGO);
levelBuilder.AddGameObject-enemyGO);
levelBuilder.SetMainCamera(mainCam);
levelBuilder.AddLight(sceneLight);
var scene = levelBuilder.BuildScene();
实例3:构建UI界面
在Unity UI系统中,构建用户界面可能涉及到很多不同的元素,如面板、按钮、文本标签等。通过Builder Pattern,可以简化这个过程:
// UI构建器接口
public interface IUIMenuBuilder
{void AddButton(string text, Action onClick);void AddLabel(string labelText);void AddSlider(float min, float max, float value);UnityEngine.UI.Canvas BuildCanvas();
}// 具体UI构建器实现
public class MainMenuBuilder : IUIMenuBuilder
{private CanvasGroup canvasGroup;private RectTransform container;private List<UnityEngine.UI.Button> buttons = new List<UnityEngine.UI.Button>();private List<UnityEngine.UI.Text> labels = new List<UnityEngine.UI.Text>();private List<UnityEngine.UI.Slider> sliders = new List<UnityEngine.UI.Slider>();public MainMenuBuilder(Canvas canvas){canvasGroup = canvas.GetComponent<CanvasGroup>();container = new GameObject("Menu Container").AddComponent<RectTransform>();container.SetParent(canvas.transform);}public void AddButton(string text, Action onClick){var button = Instantiate(buttonPrefab, container);button.GetComponentInChildren<Text>().text = text;button.onClick.AddListener(() => onClick());buttons.Add(button);}public void AddLabel(string labelText){var label = Instantiate(labelPrefab, container);label.text = labelText;labels.Add(label);}public void AddSlider(float min, float max, float value){var slider = Instantiate(sliderPrefab, container);slider.minValue = min;slider.maxValue = max;slider.value = value;sliders.Add(slider);}public UnityEngine.UI.Canvas BuildCanvas(){return canvasGroup.canvas;}
}// 使用示例
var menuBuilder = new MainMenuBuilder(mainCanvas);
menuBuilder.AddButton("Start Game", StartGame);
menuBuilder.AddLabel("Welcome to the game!");
menuBuilder.AddSlider(0f, 100f, 50f);
var builtCanvas = menuBuilder.BuildCanvas();
请注意,以上示例中的Build
方法返回的是构建的产品,在Unity中可能是具体的Unity Object或者是一个逻辑结构,实际操作时要根据Unity API的特点进行调整。
python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)
50个开发必备的Python经典脚本(11-20)
50个开发必备的Python经典脚本(21-30)
50个开发必备的Python经典脚本(31-40)
50个开发必备的Python经典脚本(41-50)
————————————————
最后我们放松一下眼睛