Unity 命令模式(实例详解)

文章目录

在Unity中使用命令模式(Command Pattern)是一种常见的设计模式,用于实现对游戏或应用中一系列动作的记录、撤销和重做操作。下面通过五个简化的C#代码示例来详细说明命令模式在Unity中的应用:

示例1:基础命令类结构

csharp 复制代码
// 基础命令接口
public interface ICommand
{
    void Execute();
    void Undo();
}

// 具体命令类 - 例如移动角色命令
public class MoveCharacterCommand : ICommand
{
    private readonly Transform _character;
    private Vector3 _previousPosition;

    public MoveCharacterCommand(Transform character, Vector3 newPosition)
    {
        _character = character;
        _previousPosition = character.position;
    }

    public void Execute()
    {
        _character.position = newPosition; // 假设newPosition是目标位置
    }

    public void Undo()
    {
        _character.position = _previousPosition;
    }
}

// 命令管理器类,负责执行和回滚命令
public class CommandManager
{
    private List<ICommand> _commandHistory = new List<ICommand>();

    public void ExecuteCommand(ICommand command)
    {
        command.Execute();
        _commandHistory.Add(command);
    }

    public void UndoLastCommand()
    {
        if (_commandHistory.Count > 0)
        {
            var lastCommand = _commandHistory[_commandHistory.Count - 1];
            lastCommand.Undo();
            _commandHistory.RemoveAt(_commandHistory.Count - 1);
        }
    }
}

示例2:旋转对象命令

csharp 复制代码
public class RotateObjectCommand : ICommand
{
    private readonly Transform _target;
    private Quaternion _originalRotation;

    public RotateObjectCommand(Transform target, Quaternion newRotation)
    {
        _target = target;
        _originalRotation = target.rotation;
    }

    public void Execute()
    {
        _target.rotation = newRotation;
    }

    public void Undo()
    {
        _target.rotation = _originalRotation;
    }
}

// 使用:
var rotateCmd = new RotateObjectCommand(someGameObject.transform, Quaternion.Euler(0, 90, 0));
commandManager.ExecuteCommand(rotateCmd);

示例3:增加道具命令

csharp 复制代码
public class AddInventoryItemCommand : ICommand
{
    private readonly PlayerInventory _inventory;
    private readonly Item _itemToAdd;
    private bool _wasAdded;

    public AddInventoryItemCommand(PlayerInventory inventory, Item item)
    {
        _inventory = inventory;
        _itemToAdd = item;
    }

    public void Execute()
    {
        _wasAdded = _inventory.AddItem(_itemToAdd);
    }

    public void Undo()
    {
        if (_wasAdded)
        {
            _inventory.RemoveItem(_itemToAdd);
        }
    }
}

// 使用:
var addItemCmd = new AddInventoryItemCommand(player.Inventory, newItem);
commandManager.ExecuteCommand(addItemCmd);

示例4:切换场景命令

csharp 复制代码
public class ChangeSceneCommand : ICommand
{
    private readonly string _sceneName;
    private readonly Scene _previousScene;

    public ChangeSceneCommand(Scene currentScene, string newSceneName)
    {
        _sceneName = newSceneName;
        _previousScene = currentScene;
    }

    public void Execute()
    {
        SceneManager.LoadScene(_sceneName);
    }

    public void Undo()
    {
        SceneManager.LoadScene(_previousScene.name);
    }
}

// 使用(假设有一个SceneManager实例提供加载场景的方法)
var changeSceneCmd = new ChangeSceneCommand(SceneManager.GetActiveScene(), "NextScene");
commandManager.ExecuteCommand(changeSceneCmd);

示例5:播放音效命令

csharp 复制代码
public class PlaySoundCommand : ICommand
{
    private readonly AudioSource _audioSource;
    private AudioClip _previousClip;
    private float _previousTime;

    public PlaySoundCommand(AudioSource audioSource, AudioClip clipToPlay)
    {
        _audioSource = audioSource;
        if (_audioSource.isPlaying)
        {
            _previousClip = _audioSource.clip;
            _previousTime = _audioSource.time;
        }
    }

    public void Execute()
    {
        _audioSource.clip = clipToPlay;
        _audioSource.Play();
    }

    public void Undo()
    {
        if (_previousClip != null)
        {
            _audioSource.clip = _previousClip;
            _audioSource.time = _previousTime;
            if (_audioSource.isPlaying)
            {
                _audioSource.Pause();
            }
        }
    }
}

// 使用:
var playSoundCmd = new PlaySoundCommand(someAudioSource, newSoundClip);
commandManager.ExecuteCommand(playSoundCmd);

这些示例展示了如何定义不同的命令,并且每个命令都实现了ICommand接口以确保它们都能执行和撤销操作。在实际项目中,命令管理器会存储这些命令并根据需要执行撤销/重做功能。

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

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

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

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

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


​最后我们放松一下眼睛

相关推荐
林杜雨都6 小时前
Action和Func
开发语言·c#
工程师0076 小时前
TPL如何自动调整执行效率
c#·tpl
CreasyChan7 小时前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
c#上位机7 小时前
halcon求区域交集——intersection
图像处理·人工智能·计算机视觉·c#·halcon
布谷歌8 小时前
在java中实现c#的int.TryParse方法
java·开发语言·python·c#
用户44884667106013 小时前
.NET进阶——深入理解Lambda表达式(2)手搓LINQ语句
c#·.net
iFlow_AI16 小时前
iFlow CLI 实战案例|生产级 Agent 聊天应用——Chatbot
交互·ai编程·命令模式·iflow·iflow cli·iflowcli
云中飞鸿19 小时前
wpf 类图
c#
世洋Blog19 小时前
SiYangUnityEventSystem,一个Unity中的事件系统
观察者模式·unity·c#·游戏引擎·事件系统
呆呆敲代码的小Y19 小时前
【Unity实战篇】| 游戏滑动框添加特殊效果,如实时高亮显示、曲线滑动等
游戏·unity·游戏引擎·实战·u3d·免费游戏·unity实战技巧