Unity 外观模式(实例详解)

文章目录

在Unity中使用外观模式(Facade)时,主要目的是为了简化复杂子系统间的交互,为外部提供一个更简洁、统一的接口。以下是一个简化的概念性示例,并扩展为5个不同场景下的代码实例:

示例1:初始化游戏场景中的多个子系统

csharp 复制代码
// 子系统接口或类定义
public interface ISubSystemA
{
    void Initialize();
}

public class SubSystemA : ISubSystemA
{
    public void Initialize()
    {
        Debug.Log("Subsystem A initialized.");
    }
}

public interface ISubSystemB
{
    void Initialize();
}

public class SubSystemB : ISubSystemB
{
    public void Initialize()
    {
        Debug.Log("Subsystem B initialized.");
    }
}

// 外观(Facade)类
public class GameInitializerFacade
{
    private ISubSystemA _subSystemA;
    private ISubSystemB _subSystemB;

    public GameInitializerFacade(ISubSystemA subSystemA, ISubSystemB subSystemB)
    {
        _subSystemA = subSystemA;
        _subSystemB = subSystemB;
    }

    public void InitializeGame()
    {
        _subSystemA.Initialize();
        _subSystemB.Initialize();
        Debug.Log("All subsystems initialized through Facade.");
    }
}

// 使用外观模式初始化游戏
public class GameManager : MonoBehaviour
{
    void Start()
    {
        var initializer = new GameInitializerFacade(new SubSystemA(), new SubSystemB());
        initializer.InitializeGame();
    }
}

示例2:管理音频播放

csharp 复制代码
public interface IAudioPlayer
{
    void PlayMusic(string track);
    void PlaySoundEffect(string effect);
}

public class AudioPlayer : IAudioPlayer
{
    // 实现音频播放逻辑...
}

public class AudioManagerFacade
{
    private IAudioPlayer _audioPlayer;

    public AudioManagerFacade(IAudioPlayer audioPlayer)
    {
        _audioPlayer = audioPlayer;
    }

    public void PlayMainMenuMusic()
    {
        _audioPlayer.PlayMusic("MainTheme");
    }

    public void PlayJumpSound()
    {
        _audioPlayer.PlaySoundEffect("Jump");
    }
}

// 在游戏中通过外观来控制音频
public class SceneController : MonoBehaviour
{
    private AudioManagerFacade _audioManager;

    void Start()
    {
        _audioManager = new AudioManagerFacade(new AudioPlayer());
        _audioManager.PlayMainMenuMusic();
    }

    void OnJump()
    {
        _audioManager.PlayJumpSound();
    }
}

示例3:场景加载流程

csharp 复制代码
public interface ISceneLoader
{
    void Load(SceneName scene);
    void Unload(SceneName scene);
}

public class SceneManager : ISceneLoader
{
    // 实现场景加载卸载功能...
}

public class SceneLoadingFacade
{
    private ISceneLoader _sceneLoader;

    public SceneLoadingFacade(ISceneLoader loader)
    {
        _sceneLoader = loader;
    }

    public void LoadMainMenu()
    {
        _sceneLoader.Load(SceneName.MainMenu);
    }

    public void LoadGameplay()
    {
        _sceneLoader.Unload(SceneName.MainMenu);
        _sceneLoader.Load(SceneName.Gameplay);
    }
}

// 游戏流程控制
public class TransitionController : MonoBehaviour
{
    private SceneLoadingFacade _loadingFacade;

    void Start()
    {
        _loadingFacade = new SceneLoadingFacade(new SceneManager());
        _loadingFacade.LoadMainMenu();
    }

    public void OnPlayButtonClicked()
    {
        _loadingFacade.LoadGameplay();
    }
}

示例4:UI管理器

csharp 复制代码
public interface IUIManager
{
    void ShowMenu(MenuType menu);
    void HideMenu(MenuType menu);
}

public class UIManager : IUIManager
{
    // 实现菜单显示隐藏逻辑...
}

public class UIFacade
{
    private IUIManager _uiManager;

    public UIFacade(IUIManager manager)
    {
        _uiManager = manager;
    }

    public void OpenSettings()
    {
        _uiManager.ShowMenu(MenuType.Settings);
    }

    public void CloseAllMenus()
    {
        _uiManager.HideMenu(MenuType.All);
    }
}

// 游戏中UI控制
public class MenuInteraction : MonoBehaviour
{
    private UIFacade _uiFacade;

    void Start()
    {
        _uiFacade = new UIFacade(new UIManager());
    }

    void OnSettingsButtonClick()
    {
        _uiFacade.OpenSettings();
    }

    void OnBackPressed()
    {
        _uiFacade.CloseAllMenus();
    }
}

示例5:网络服务通信

csharp 复制代码
public interface INetworkService
{
    void Connect(string address);
    void SendData(byte[] data);
    void Disconnect();
}

public class NetworkServiceImpl : INetworkService
{
    // 实现网络连接和数据发送逻辑...
}

public class NetworkServiceFacade
{
    private INetworkService _networkService;

    public NetworkServiceFacade(INetworkService service)
    {
        _networkService = service;
    }

    public void ConnectToServer(string ip, int port)
    {
        string address = $"{ip}:{port}";
        _networkService.Connect(address);
    }

    public void SendGameData(GameData data)
    {
        byte[] serializedData = SerializeGameData(data);
        _networkService.SendData(serializedData);
    }

    public void DisconnectFromServer()
    {
        _networkService.Disconnect();
    }
}

// 游戏中网络模块调用
public class NetworkController : MonoBehaviour
{
    private NetworkServiceFacade _networkFacade;

    void Start()
    {
        _networkFacade = new NetworkServiceFacade(new NetworkServiceImpl());
    }

    void OnConnectButtonClicked()
    {
        _networkFacade.ConnectToServer("192.168.0.1", 12345);
    }

    void OnSendGameData()
    {
        GameData gameData = GetGameData();
        _networkFacade.SendGameData(gameData);
    }

    void OnDisconnectButtonClicked()
    {
        _networkFacade.DisconnectFromServer();
    }
}

以上每个例子都展示了如何通过外观模式将复杂的子系统操作封装成简单易用的方法,在Unity项目中提供了更高层次的抽象和控制。

python推荐学习汇总连接:
50个开发必备的Python经典脚本(1-10)

50个开发必备的Python经典脚本(11-20)

50个开发必备的Python经典脚本(21-30)

50个开发必备的Python经典脚本(31-40)

50个开发必备的Python经典脚本(41-50)


​最后我们放松一下眼睛

相关推荐
皮皮陶1 小时前
Unity WebGL交互通信
unity·交互·webgl
程序员正茂1 小时前
PICO+Unity MR空间网格
unity·mr·pico
程序员正茂1 小时前
PICO+Unity MR空间锚点
unity·pico·空间锚点
IT规划师3 小时前
开源 - Ideal库 - 常用枚举扩展方法(一)
开源·c#·.net core·ideal库·枚举转换
龙中舞王4 小时前
Unity学习笔记(2):场景绘制
笔记·学习·unity
NetX行者9 小时前
.NET 9震撼来袭:基于.NET 8的五大功能亮点,引领开发新潮流
开发语言·microsoft·c#·.netcore
张某布响丸辣9 小时前
HTTP状态码详解
java·网络·python·网络协议·http·c#
逐·風9 小时前
Unity编辑器的高级扩展技术
unity·编辑器·游戏引擎
飞舞的哈哈10 小时前
C# 有趣的小程序—桌面精灵详细讲解
c#
Skyshin3412 小时前
C# IEnumerator,IEnumerable ,Iterator
开发语言·c#