Image组件转换为RawImage组件,方便快捷~

写在开头

本篇文章,如果有不懂,欢迎留言~click here!

源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
}
}
}

示意图

写在结尾

以上知识分享,如有错误,欢迎指出,共同学习,共同进步。如果有不懂,欢迎留言评论!