{"id":1511,"date":"2016-06-02T12:10:01","date_gmt":"2016-06-02T04:10:01","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=1511"},"modified":"2016-06-02T12:10:01","modified_gmt":"2016-06-02T04:10:01","slug":"unity3d-ver-5-3-custom-editors-%e7%9a%84-undo-%e4%bd%bf%e7%94%a8%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/c\/1511\/","title":{"rendered":"Unity3D ver 5.3 Custom Editors \u7684 Undo \u4f7f\u7528\u65b9\u5f0f"},"content":{"rendered":"<p>\u5728 Unity3D \u7684\u5957\u4ef6\u88fd\u4f5c\u4e2d, Editor \u7d93\u5e38\u9700\u8981\u8003\u616e\u9032\u884c Undo Redo \u7684\u4f7f\u7528\u8005\u64cd\u4f5c.<\/p>\n<p>\u53c3\u8003:\u00a0<a href=\"http:\/\/docs.unity3d.com\/530\/Documentation\/ScriptReference\/Undo.html\">http:\/\/docs.unity3d.com\/530\/Documentation\/ScriptReference\/Undo.html<\/a><br \/>\n\u9019\u88e1\u7b46\u8a18\u4e00\u4e0b\u4f7f\u7528 Undo \u7684\u5e38\u7528\u65b9\u5f0f.<\/p>\n<p>\u4e00\u822c\u53c3\u6578\u6539\u8b8a (Undo\u00a0Parameter change)<\/p>\n<pre class=\"brush:csharp\">public Vector3 m_Point; \/\/ variable\r\n\r\nvoid OnSceneGUI()\r\n{\r\n\tVector3 newPosition = Handles.PositionHandle(m_Point, Quaternion.identity);\r\n\t\r\n\tif(newPosition != m_Point)\r\n\t{\r\n\t\tUndo.RecordObject(this, \"Move Point\"); \/\/ register before assign new value\r\n\t\tm_Point = newPosition; \/\/ assign new value\r\n\t}\r\n}<\/pre>\n<p>\u589e\u52a0\u5be6\u9ad4\u7684\u6539\u8b8a (Undo created object)<\/p>\n<pre class=\"brush:csharp\">void AddObject()\r\n{\r\n\tGameObject obj = new GameObject(); \/\/ &lt;- create new object\r\n\t\r\n\tobj.name = \"New Object\" + obj.GetInstanceID();\t\/\/ do what ever you want.\r\n\tCustomClass script = obj.AddComponent&lt;CustomClass&gt;(); \/\/ even add component\r\n\t\r\n\tUndo.RegisterCreatedObjectUndo(obj, \"Create Curve \" + obj.GetInstanceID()); \/\/ register the whole gameobject you created\r\n}<\/pre>\n<p>\u522a\u9664\u7269\u4ef6\u7684\u6539\u8b8a (Undo delete object)<\/p>\n<pre class=\"brush:csharp\">void RemoveObject_One()\r\n{\r\n\tUndo.DestroyObjectImmediate(obj.gameObject);\r\n}\r\n\r\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\npublic List&lt;CustomClass&gt; m_ChildObjectList = new List&lt;CustomClass&gt;();\r\nvoid RemoveObject_Two()\r\n{\r\n\t\/\/ Record the objects, variables, that will change in this action.\r\n\t\r\n\t\/\/ Method 1 \r\n\tUndo.RecordObject(m_ChildObjectList, \"Remove Object\"+ obj.GetInstanceID());\r\n\t\r\n\t\/\/ Method 2\r\n\t\/\/ Undo.RecordObjects(new object[] {\r\n\t\t\/\/ m_ChildObjectList,\r\n\t\t\/\/ another_invoke_variables,\r\n\t\t\/\/ another_invoke_variables,\r\n\t\t\/\/ another_invoke_variables,\r\n\t\/\/ }, \"Remove Object\"+ obj.GetInstanceID());\r\n\t\r\n\tm_ChildObjectList.Remove(obj); \/\/ process change\r\n\t\r\n\t\/\/ only \"Undo.DestroyObjectImmediate\" can undo\/redo the destroy object.\r\n\tUndo.DestroyObjectImmediate(obj.gameObject);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Unity3D \u7684\u5957\u4ef6\u88fd\u4f5c\u4e2d, Editor \u7d93\u5e38\u9700\u8981\u8003\u616e\u9032\u884c Undo Redo \u7684\u4f7f\u7528\u8005\u64cd\u4f5c &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,11],"tags":[],"class_list":["post-1511","post","type-post","status-publish","format-standard","hentry","category-c","category-unity3d"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1511","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1511"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/1511\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}