Unity每天一个小技巧-Image组件转换RawImage组件

博客迁移

个人博客站点,欢迎访问,www.jiingfengji.tech

本文地址

源码

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class UnityComponentExtensionMenu : UnityEditor.Editor
{
    [MenuItem("CONTEXT/Image/转换成RawImage")]
    public static void ReplaceImageToRawImage()
    {
        Image imageCom = Selection.activeGameObject.GetComponent<Image>();
        if (imageCom != null)
        {
            Texture2D texture2D = imageCom.sprite.texture;
            bool ray = imageCom.raycastTarget;
            Material material = imageCom.material;
            DestroyImmediate(imageCom);
            RawImage rawImage = Selection.activeGameObject.AddComponent<RawImage>();
            rawImage.texture = texture2D;
            rawImage.raycastTarget = ray;
            rawImage.material = material;
            EditorUtility.SetDirty(Selection.activeGameObject);
        }
    }
}

Unity每天一个小技巧-Image组件转换RawImage组件_第1张图片
以上知识分享,如有错误,欢迎指出,共同学习,共同进步。

你可能感兴趣的:(Unity编辑器拓展,Unity)