objectToUndo | 对要修改对象的引用。 |
name | 要在撤消历史记录中显示(即在撤销菜单中可见)的操作的名称。 |
记录执行 RecordObject 函数之后对对象所做的任何更改。
使用这一函数可以记录几乎所有属性更改。无法使用这一函数记录变换组件的父项、AddComponent 和对象销毁,应该使用专用函数来记录。
在内部,这会创建对象状态的临时副本。在帧结束时,Unity 会对状态进行区分并检测已更改的内容。更改过的属性会记录在撤销堆栈上。如果未进行任何更改(对所有属性都进行二进制精确比较),则不会在堆栈上存储任何撤销操作。
重要信息:要在 objectToUndo 为预制件实例的情况下正确处理实例,必须在 RecordObject 之后调用 PrefabUtility.RecordPrefabInstancePropertyModifications。
下面是一个编辑器脚本的示例,可用于更改特效的半径变量。系统会记录撤销状态,这样您就可以使用撤销系统来恢复更改。
//Name this script "EffectRadiusEditor" using UnityEngine; using UnityEditor;
[CustomEditor(typeof(EffectRadius))] public class EffectRadiusEditor : Editor { public void OnSceneGUI() { EffectRadius t = (target as EffectRadius);
EditorGUI.BeginChangeCheck(); float areaOfEffect = Handles.RadiusHandle(Quaternion.identity, t.transform.position, t.areaOfEffect); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Changed Area Of Effect"); t.areaOfEffect = areaOfEffect; } } }
将以下脚本放在游戏对象上,查看效果范围句柄,并在 Scene 视图中使用辅助图标来更改相应值。
//Name this script "EffectRadius" using UnityEngine; using System.Collections;
public class EffectRadius : MonoBehaviour { public float areaOfEffect = 1; }
另请参阅:Undo.RecordObjects
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.