版本: Unity 6 (6000.0)
语言英语
  • C#

ObjectChangeEvents.changesPublished

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实阅读了用户提出的每一项更改建议,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

参数

value 自上一帧以来记录的事件流。

描述

如果已记录任何可撤消的更改,则每帧触发一次的事件。

所有加载对象的任何可撤消更改都记录在一个事件流中,并每帧发布一次。订阅此事件以对任何这些更改做出反应。更改按应用顺序显示在流中。流中的事件不一定与用户交互一一对应,因为用户交互可能包含多个更改。请注意,传递给回调的流是由引擎拥有的临时对象。请勿对其进行释放。如果您打算将其保留的时间超过回调持续时间,请进行复制。

using UnityEditor;
using UnityEngine;

[InitializeOnLoad] public class ObjectChangeEventsExample { static ObjectChangeEventsExample() { ObjectChangeEvents.changesPublished += ChangesPublished; }

static void ChangesPublished(ref ObjectChangeEventStream stream) { for (int i = 0; i < stream.length; ++i) { var type = stream.GetEventType(i); switch (type) { case ObjectChangeKind.ChangeScene: stream.GetChangeSceneEvent(i, out var changeSceneEvent); Debug.Log($"{type}: {changeSceneEvent.scene}"); break;

case ObjectChangeKind.CreateGameObjectHierarchy: stream.GetCreateGameObjectHierarchyEvent(i, out var createGameObjectHierarchyEvent); var newGameObject = EditorUtility.InstanceIDToObject(createGameObjectHierarchyEvent.instanceId) as GameObject; Debug.Log($"{type}: {newGameObject} in scene {createGameObjectHierarchyEvent.scene}."); break;

case ObjectChangeKind.ChangeGameObjectStructureHierarchy: stream.GetChangeGameObjectStructureHierarchyEvent(i, out var changeGameObjectStructureHierarchy); var gameObject = EditorUtility.InstanceIDToObject(changeGameObjectStructureHierarchy.instanceId) as GameObject; Debug.Log($"{type}: {gameObject} in scene {changeGameObjectStructureHierarchy.scene}."); break;

case ObjectChangeKind.ChangeGameObjectStructure: stream.GetChangeGameObjectStructureEvent(i, out var changeGameObjectStructure); var gameObjectStructure = EditorUtility.InstanceIDToObject(changeGameObjectStructure.instanceId) as GameObject; Debug.Log($"{type}: {gameObjectStructure} in scene {changeGameObjectStructure.scene}."); break;

case ObjectChangeKind.ChangeGameObjectParent: stream.GetChangeGameObjectParentEvent(i, out var changeGameObjectParent); var gameObjectChanged = EditorUtility.InstanceIDToObject(changeGameObjectParent.instanceId) as GameObject; var newParentGo = EditorUtility.InstanceIDToObject(changeGameObjectParent.newParentInstanceId) as GameObject; var previousParentGo = EditorUtility.InstanceIDToObject(changeGameObjectParent.previousParentInstanceId) as GameObject; Debug.Log($"{type}: {gameObjectChanged} from {previousParentGo} to {newParentGo} from scene {changeGameObjectParent.previousScene} to scene {changeGameObjectParent.newScene}."); break;

case ObjectChangeKind.ChangeGameObjectOrComponentProperties: stream.GetChangeGameObjectOrComponentPropertiesEvent(i, out var changeGameObjectOrComponent); var goOrComponent = EditorUtility.InstanceIDToObject(changeGameObjectOrComponent.instanceId); if (goOrComponent is GameObject go) { Debug.Log($"{type}: GameObject {go} change properties in scene {changeGameObjectOrComponent.scene}."); } else if (goOrComponent is Component component) { Debug.Log($"{type}: Component {component} change properties in scene {changeGameObjectOrComponent.scene}."); } break;

case ObjectChangeKind.DestroyGameObjectHierarchy: stream.GetDestroyGameObjectHierarchyEvent(i, out var destroyGameObjectHierarchyEvent); // The destroyed GameObject can not be converted with EditorUtility.InstanceIDToObject as it has already been destroyed. var destroyParentGo = EditorUtility.InstanceIDToObject(destroyGameObjectHierarchyEvent.parentInstanceId) as GameObject; Debug.Log($"{type}: {destroyGameObjectHierarchyEvent.instanceId} with parent {destroyParentGo} in scene {destroyGameObjectHierarchyEvent.scene}."); break;

case ObjectChangeKind.CreateAssetObject: stream.GetCreateAssetObjectEvent(i, out var createAssetObjectEvent); var createdAsset = EditorUtility.InstanceIDToObject(createAssetObjectEvent.instanceId); var createdAssetPath = AssetDatabase.GUIDToAssetPath(createAssetObjectEvent.guid); Debug.Log($"{type}: {createdAsset} at {createdAssetPath} in scene {createAssetObjectEvent.scene}."); break;

case ObjectChangeKind.DestroyAssetObject: stream.GetDestroyAssetObjectEvent(i, out var destroyAssetObjectEvent); // The destroyed asset can not be converted with EditorUtility.InstanceIDToObject as it has already been destroyed. Debug.Log($"{type}: Instance Id {destroyAssetObjectEvent.instanceId} with Guid {destroyAssetObjectEvent.guid} in scene {destroyAssetObjectEvent.scene}."); break;

case ObjectChangeKind.ChangeAssetObjectProperties: stream.GetChangeAssetObjectPropertiesEvent(i, out var changeAssetObjectPropertiesEvent); var changeAsset = EditorUtility.InstanceIDToObject(changeAssetObjectPropertiesEvent.instanceId); var changeAssetPath = AssetDatabase.GUIDToAssetPath(changeAssetObjectPropertiesEvent.guid); Debug.Log($"{type}: {changeAsset} at {changeAssetPath} in scene {changeAssetObjectPropertiesEvent.scene}."); break;

case ObjectChangeKind.UpdatePrefabInstances: stream.GetUpdatePrefabInstancesEvent(i, out var updatePrefabInstancesEvent); string s = ""; s += $"{type}: scene {updatePrefabInstancesEvent.scene}. Instances ({updatePrefabInstancesEvent.instanceIds.Length}):\n"; foreach (var prefabId in updatePrefabInstancesEvent.instanceIds) { s += EditorUtility.InstanceIDToObject(prefabId).ToString() + "\n"; } Debug.Log(s); break; } } } }