{"id":229,"date":"2025-06-30T18:03:43","date_gmt":"2025-06-30T10:03:43","guid":{"rendered":"https:\/\/www.idcyz.com\/blog\/?p=229"},"modified":"2025-06-30T18:03:43","modified_gmt":"2025-06-30T10:03:43","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8unity%e4%b8%ad%e5%ae%89%e8%a3%85%e5%92%8c%e4%bd%bf%e7%94%a8%e5%8e%86%e5%8f%b2%e8%ae%b0%e5%bd%95%e5%8a%9f%e8%83%bd%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.idcyz.com\/blog\/229.html","title":{"rendered":"\u5982\u4f55\u5728Unity\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u5386\u53f2\u8bb0\u5f55\u529f\u80fd\u5b9e\u73b0"},"content":{"rendered":"<p><h2>Unity \u5b9e\u73b0\u5386\u53f2\u8bb0\u5f55<\/h2>\n<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.idcyz.com\/blog\/wp-content\/uploads\/2025\/06\/ZCfsosOtc.jpg\" alt=\"\u5982\u4f55\u5728Unity\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u5386\u53f2\u8bb0\u5f55\u529f\u80fd\u5b9e\u73b0\" title=\"\u5982\u4f55\u5728Unity\u4e2d\u5b89\u88c5\u548c\u4f7f\u7528\u5386\u53f2\u8bb0\u5f55\u529f\u80fd\u5b9e\u73b0\"><\/p>\n<p><h3>\u6280\u672f\u7b80\u4ecb<\/h3>\n<\/p>\n<p>\u5728\u6e38\u620f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u73a9\u5bb6\u7684\u64cd\u4f5c\u548c\u6e38\u620f\u72b6\u6001\u7684\u53d8\u5316\u5f80\u5f80\u9700\u8981\u88ab\u8ddf\u8e2a\u548c\u8bb0\u5f55\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u64a4\u56de\u64cd\u4f5c\u6216\u91cd\u505a\u64cd\u4f5c\u65f6\u3002Unity \u4e3a\u6b64\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\uff0c\u7ed3\u5408\u5408\u7406\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u53ef\u9760\u7684\u5386\u53f2\u8bb0\u5f55\u7cfb\u7edf\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728 Unity \u4e2d\u5b9e\u73b0\u5386\u53f2\u8bb0\u5f55\uff0c\u786e\u4fdd\u73a9\u5bb6\u64cd\u4f5c\u7684\u6bcf\u4e00\u6b65\u90fd\u53ef\u4ee5\u88ab\u8ffd\u8e2a\u548c\u7ba1\u7406\u3002<\/p>\n<p><h3>\u4efb\u52a1\u76ee\u6807<\/h3>\n<\/p>\n<p>\u672c\u6587\u7684\u76ee\u6807\u662f\u6307\u5bfc\u5f00\u53d1\u8005\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5386\u53f2\u8bb0\u5f55\u7ba1\u7406\u5668\uff0c\u4ee5\u4fbf\u80fd\u591f\u6267\u884c\u4ee5\u4e0b\u529f\u80fd\uff1a<\/p>\n<ul>\n<li>\u8bb0\u5f55\u7528\u6237\u7684\u64cd\u4f5c<\/li>\n<li>\u5b9e\u73b0\u64a4\u56de\u4e0e\u91cd\u505a\u529f\u80fd<\/li>\n<li>\u652f\u6301\u4e0d\u540c\u7c7b\u578b\u7684\u64cd\u4f5c\u65e5\u5fd7\u8bb0\u5f55<\/li>\n<\/ul>\n<p><h3>\u5b9e\u73b0\u6b65\u9aa4<\/h3>\n<\/p>\n<p><h4>1. \u521b\u5efa\u5386\u53f2\u8bb0\u5f55\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7ba1\u7406\u5668\u7c7b\uff0c\u4e13\u95e8\u7528\u4e8e\u7ba1\u7406\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n<p><pre><code>using System.Collections.Generic;<\/p>\r\n<p>using UnityEngine;<\/p>\r\n\r\n<p>public class HistoryManager : MonoBehaviour<\/p>\r\n<p>{<\/p>\r\n<p>    private Stack<ICommand> undoStack = new Stack<ICommand>();<\/p>\r\n<p>    private Stack<ICommand> redoStack = new Stack<ICommand>();<\/p>\r\n\r\n<p>    public void ExecuteCommand(ICommand command)<\/p>\r\n<p>    {<\/p>\r\n<p>        command.Execute();<\/p>\r\n<p>        undoStack.Push(command);<\/p>\r\n<p>        redoStack.Clear(); \/\/ \u6267\u884c\u65b0\u547d\u4ee4\u540e\u6e05\u7a7a\u91cd\u505a\u6808<\/p>\r\n<p>    }<\/p>\r\n\r\n<p>    public void Undo()<\/p>\r\n<p>    {<\/p>\r\n<p>        if (undoStack.Count > 0)<\/p>\r\n<p>        {<\/p>\r\n<p>            ICommand command = undoStack.Pop();<\/p>\r\n<p>            command.Undo();<\/p>\r\n<p>            redoStack.Push(command);<\/p>\r\n<p>        }<\/p>\r\n<p>    }<\/p>\r\n\r\n<p>    public void Redo()<\/p>\r\n<p>    {<\/p>\r\n<p>        if (redoStack.Count > 0)<\/p>\r\n<p>        {<\/p>\r\n<p>            ICommand command = redoStack.Pop();<\/p>\r\n<p>            command.Execute();<\/p>\r\n<p>            undoStack.Push(command);<\/p>\r\n<p>        }<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5b9a\u4e49\u547d\u4ee4\u63a5\u53e3<\/h4>\n<\/p>\n<p>\u4e3a\u4e86\u5b9e\u73b0\u5386\u53f2\u8bb0\u5f55\u7684\u901a\u7528\u6027\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u547d\u4ee4\u63a5\u53e3\uff0c\u6240\u6709\u547d\u4ee4\u90fd\u4f1a\u5b9e\u73b0\u6b64\u63a5\u53e3\u3002<\/p>\n<p><pre><code>public interface ICommand<\/p>\r\n<p>{<\/p>\r\n<p>    void Execute();<\/p>\r\n<p>    void Undo();<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u521b\u5efa\u5177\u4f53\u547d\u4ee4\u7c7b<\/h4>\n<\/p>\n<p>\u57fa\u4e8e ICommand \u63a5\u53e3\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e9b\u5177\u4f53\u547d\u4ee4\u7c7b\u3002\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u547d\u4ee4\u6765\u5904\u7406\u5bf9\u8c61\u7684\u79fb\u52a8\u3002<\/p>\n<p><pre><code>public class MoveCommand : ICommand<\/p>\r\n<p>{<\/p>\r\n<p>    private GameObject gameObject;<\/p>\r\n<p>    private Vector3 oldPosition;<\/p>\r\n<p>    private Vector3 newPosition;<\/p>\r\n\r\n<p>    public MoveCommand(GameObject gameObject, Vector3 newPosition)<\/p>\r\n<p>    {<\/p>\r\n<p>        this.gameObject = gameObject;<\/p>\r\n<p>        this.oldPosition = gameObject.transform.position;<\/p>\r\n<p>        this.newPosition = newPosition;<\/p>\r\n<p>    }<\/p>\r\n\r\n<p>    public void Execute()<\/p>\r\n<p>    {<\/p>\r\n<p>        gameObject.transform.position = newPosition;<\/p>\r\n<p>    }<\/p>\r\n\r\n<p>    public void Undo()<\/p>\r\n<p>    {<\/p>\r\n<p>        gameObject.transform.position = oldPosition;<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4. \u4f7f\u7528\u5386\u53f2\u8bb0\u5f55\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p>\u5728\u6e38\u620f\u7684\u67d0\u4e2a\u63a7\u5236\u5668\u6216\u573a\u666f\u7ba1\u7406\u5668\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u5386\u53f2\u8bb0\u5f55\u7ba1\u7406\u5668\uff0c\u6355\u6349\u73a9\u5bb6\u7684\u64cd\u4f5c\u3002<\/p>\n<p><pre><code>public class GameController : MonoBehaviour<\/p>\r\n<p>{<\/p>\r\n<p>    private HistoryManager historyManager;<\/p>\r\n<p>    public GameObject player;<\/p>\r\n\r\n<p>    private void Start()<\/p>\r\n<p>    {<\/p>\r\n<p>        historyManager = gameObject.AddComponent<HistoryManager>();<\/p>\r\n<p>    }<\/p>\r\n\r\n<p>    private void Update()<\/p>\r\n<p>    {<\/p>\r\n<p>        if (Input.GetKeyDown(KeyCode.W))<\/p>\r\n<p>        {<\/p>\r\n<p>            var command = new MoveCommand(player, player.transform.position + Vector3.forward);<\/p>\r\n<p>            historyManager.ExecuteCommand(command);<\/p>\r\n<p>        }<\/p>\r\n<p>        if (Input.GetKeyDown(KeyCode.Z))<\/p>\r\n<p>        {<\/p>\r\n<p>            historyManager.Undo();<\/p>\r\n<p>        }<\/p>\r\n<p>        if (Input.GetKeyDown(KeyCode.Y))<\/p>\r\n<p>        {<\/p>\r\n<p>            historyManager.Redo();<\/p>\r\n<p>        }<\/p>\r\n<p>    }<\/p>\r\n<p>}<\/p>\r\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ul>\n<li><strong>\u547d\u4ee4\u8bbe\u8ba1\uff1a<\/strong>\u786e\u4fdd\u6bcf\u4e2a\u547d\u4ee4\u90fd\u80fd\u72ec\u7acb\u5904\u7406\u81ea\u5df1\u7684\u72b6\u6001\uff0c\u4ee5\u4fbf\u4e8e\u6b63\u786e\u6267\u884c\u548c\u64a4\u56de\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u8651\uff1a<\/strong>\u5bf9\u4e8e\u9700\u8981\u8bb0\u5f55\u5927\u91cf\u64cd\u4f5c\u7684\u60c5\u51b5\uff0c\u8003\u8651\u662f\u5426\u53ef\u4ee5\u5408\u5e76\u76f8\u4f3c\u7684\u64cd\u4f5c\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u3002<\/li>\n<li><strong>\u53cd\u9988\u673a\u5236\uff1a<\/strong>\u63d0\u4f9b\u73a9\u5bb6\u53cd\u9988\uff0c\u4f8b\u5982\u5728\u6267\u884c\u64a4\u56de\u6216\u91cd\u505a\u64cd\u4f5c\u65f6\uff0c\u66f4\u65b0 UI \u72b6\u6001\u3002<\/li>\n<\/ul>\n<p><h3>\u5b9e\u7528\u6280\u5de7<\/h3>\n<\/p>\n<ul>\n<li><strong>\u591a\u79cd\u64cd\u4f5c\uff1a<\/strong>\u53ef\u4ee5\u6269\u5c55 ICommand \u63a5\u53e3\u6765\u652f\u6301\u66f4\u591a\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u65cb\u8f6c\u3001\u7f29\u653e\u7b49\uff0c\u63d0\u4f9b\u66f4\u4e3a\u4e30\u5bcc\u7684\u5386\u53f2\u8bb0\u5f55\u529f\u80fd\u3002<\/li>\n<li><strong>\u81ea\u5b9a\u4e49\u547d\u4ee4\uff1a<\/strong>\u4f60\u53ef\u4ee5\u4e3a\u4e0d\u540c\u7c7b\u7684\u5bf9\u8c61\u521b\u5efa\u7279\u5b9a\u7684\u547d\u4ee4\uff0c\u4f8b\u5982\u9488\u5bf9 UI \u5143\u7d20\u548c3D\u6a21\u578b\u7684\u64cd\u4f5c\u5404\u81ea\u5b9a\u4e49\u547d\u4ee4\u3002<\/li>\n<li><strong>\u94fe\u5f0f\u547d\u4ee4\uff1a<\/strong>\u53ef\u4ee5\u8bbe\u8ba1\u4e00\u4e2a CompositeCommand \u6765\u5904\u7406\u591a\u4e2a\u547d\u4ee4\u7684\u6267\u884c\u548c\u64a4\u56de\uff0c\u4fbf\u4e8e\u7ba1\u7406\u590d\u6742\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u5c06\u80fd\u591f\u5728 Unity \u6e38\u620f\u4e2d\u5b9e\u73b0\u4e00\u4e2a\u57fa\u7840\u7684\u5386\u53f2\u8bb0\u5f55\u7cfb\u7edf\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u5728\u5386\u53f2\u8bb0\u5f55\u529f\u80fd\u7684\u5b9e\u73b0\u4e0a\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unity \u5b9e\u73b0\u5386\u53f2\u8bb0\u5f55 \u6280\u672f\u7b80\u4ecb \u5728\u6e38\u620f\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u73a9\u5bb6\u7684\u64cd\u4f5c\u548c\u6e38\u620f\u72b6\u6001\u7684\u53d8\u5316\u5f80\u5f80\u9700\u8981\u88ab\u8ddf\u8e2a\u548c\u8bb0\u5f55\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u64a4\u56de\u64cd\u4f5c\u6216\u91cd\u505a\u64cd\u4f5c\u65f6\u3002Unity \u4e3a\u6b64\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5de5\u5177\uff0c\u7ed3\u5408\u5408\u7406\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u53ef\u9760\u7684\u5386\u53f2\u8bb0\u5f55\u7cfb\u7edf\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982&#8230;<\/p>\n","protected":false},"author":1,"featured_media":230,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[131,130,132],"topic":[],"class_list":["post-229","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jiushu","tag-unity","tag-130","tag-132"],"_links":{"self":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":1,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions"}],"predecessor-version":[{"id":232,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/posts\/229\/revisions\/232"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media\/230"}],"wp:attachment":[{"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/media?parent=229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/categories?post=229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/tags?post=229"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.idcyz.com\/blog\/wp-json\/wp\/v2\/topic?post=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}