首页 > 其他分享 >unity创建物体的编辑器的回调

unity创建物体的编辑器的回调

时间:2023-02-17 12:15:06浏览次数:41  
标签:InstanceIDToObject stream 物体 scene 编辑器 unity EditorUtility var type

注意:

需要Unity2021以上版本

 

参考

https://forum.unity.com/threads/editor-callbacks-for-gameobject-creation-deletion-duplication-by-user-or-user-script.788792/

 

示例1

using System.Text;
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;
            }
        }
    }
}

 

 

示例2:

using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

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

    private static void ChangesPublished(ref ObjectChangeEventStream stream) {
        if (Application.isPlaying) {
            return;
        }
        for (int i = 0; i < stream.length; ++i) {
            switch (stream.GetEventType(i)) {
                case ObjectChangeKind.CreateGameObjectHierarchy:
                    //UI父节点自动挂上CanvasGroup
                    stream.GetCreateGameObjectHierarchyEvent(i, out var changeGameObjectParent);
                    var newGameObject = EditorUtility.InstanceIDToObject(changeGameObjectParent.instanceId) as GameObject;
                    if (newGameObject.transform.parent != null) {
                        if (newGameObject.transform.parent.name.Equals("Canvas") && newGameObject.GetComponent<Image>() != null) {
                            newGameObject.AddComponent<CanvasGroup>();
                        }
                    }
                    break;
            }
        }
    }
}

 

标签:InstanceIDToObject,stream,物体,scene,编辑器,unity,EditorUtility,var,type
From: https://www.cnblogs.com/sanyejun/p/17129698.html

相关文章

  • Unity在使用UI接口时,遇到拖拽位置错误的问题
     IDragHandler,IEndDragHandler,IBeginDragHandler在使用UI拖拽接口的时候,发现拖拽时会异常的偏移最后发现是因为是直接使用了transform.position导致的后来改成Re......
  • 富文本编辑器实现从word中复制图片
    项目需求可发布文章需求涉及到富文本编辑器经过查阅我选择了较为简便不需要后端支持可独立完成的tinymce框架官方文档也是相当完整虽然都是全英文但是有强大的谷歌......
  • 富文本编辑器实现从PowerPoint中复制图片
    ​ 如何做到ueditor批量上传word图片?1、前端引用代码<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml......
  • Linux 中设置vim编辑器编写shell脚本自动缩进
     001、打开vim的配置文件:vim~/.vimrc然后在其末尾增加如下内容,然后保存退出:ifhas("autocmd")filetypeindentonautocmdFileType*setlocalts=4sts......
  • linux 中vim 编辑器 退格键 无法删除
     001、问题vim进入插入模式后,按退格键无法删除:  002、解决方法echo"setbackspace=2">>~/.vimrc##启用退格键的删除功能 003、测试,没有问题 ......
  • Unity IL2cpp内存管理
    分配关键类hblkstructhblk{charhb_body[HBLKSIZE];};BoehmGC是按照block来分配内存的,HBLKSIZE表示每个block大小默认值是4096,和页大小一致,猜测合理的值......
  • 富文本编辑器实现从ppt中复制图片
    ​ 项目需求可发布文章需求涉及到富文本编辑器经过查阅我选择了较为简便不需要后端支持可独立完成的tinymce框架官方文档也是相当完整虽然都是全英文但是有强大的......
  • Unity资源路径
    1.Resource:在Unity编辑器的Project窗口里创建,Resources文件夹下的资源全部会打包进.apk或者.ipa,并且打包时会将里面的资源压缩处理。加载方法是Resources.Load(文件名),需要......
  • 富文本编辑器实现导入ppt
    ​ ueditor粘贴不能粘贴word中的图片是一个很头疼的问题,在我们的业务场景中客户要求必须使用ueditor并且支持word的图片粘贴,因为这个需求头疼了半个月,因为前端方面因为安......
  • 富文本编辑器实现导入PowerPoint
    ​ 百度ueditor新增的将word内容导入到富文本编辑框的功能怎么没有啊,...ueditor实现word文档的导入和下载功能的方法:1、UEditor没有提供word的导入功能,只能说是粘贴复......