Unity 外观模式(实例详解)

文章目录

      • 示例1:初始化游戏场景中的多个子系统
      • 示例2:管理音频播放
      • 示例3:场景加载流程
      • 示例4:UI管理器
      • 示例5:网络服务通信

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

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

// 子系统接口或类定义
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:管理音频播放

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:场景加载流程

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管理器

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:网络服务通信

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)
————————————————

​最后我们放松一下眼睛
在这里插入图片描述

你可能感兴趣的:(unity,C#设计模式详解,unity,外观模式,c#)