Unity 编辑器开发实战【MenuItem & CONTEXT】- Image、RawImage的相互转换

2022-08-29 16:50:35 浏览数 (1)

1.Image转RawImage:

代码如下:

代码语言:javascript复制
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace SK.Framework
{
    public class ImageTool : Editor
    {
        /// <summary>
        /// Image转RawImage
        /// </summary>
        [MenuItem("CONTEXT/Image/Convert 2 RawImage")]
        public static void Image2RawImage()
        {
            Image image = Selection.activeGameObject.GetComponent<Image>();
            //Image中Sprite不为空则获取其texture
            Texture2D texture2D = image.sprite ? image.sprite.texture : null;
            var raycastTarget = image.raycastTarget;
            //销毁Image组件
            DestroyImmediate(image);
            //添加RawImage组件
            RawImage rawImage = Selection.activeGameObject.AddComponent<RawImage>();
            rawImage.texture = texture2D;
            rawImage.raycastTarget = raycastTarget;
            //SetDirty以保存
            EditorUtility.SetDirty(Selection.activeGameObject);
        }
    }
}
代码语言:javascript复制
2.RawImage转Image:

代码如下:

代码语言:javascript复制
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace SK.Framework
{
    public class ImageTool : Editor
    {
        /// <summary>
        /// RawImage转Image
        /// </summary>
        [MenuItem("CONTEXT/RawImage/Convert 2 Image")]
        public static void RawImage2Image()
        {
            RawImage rawImage = Selection.activeGameObject.GetComponent<RawImage>();
            Sprite sprite = null;
            //如果RawImage组件中的texture不为空
            if (rawImage.texture != null)
            {
                //获取texture的资源路径
                var path = AssetDatabase.GetAssetPath(rawImage.texture);
                //根据该资源路径加载Sprite
                sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
            }
            var raycastTarget = rawImage.raycastTarget;
            //销毁RawImage组件
            DestroyImmediate(rawImage);
            //添加Image组件
            Image image = Selection.activeGameObject.AddComponent<Image>();
            image.sprite = sprite;
            image.raycastTarget = raycastTarget;
            //SetDirty以保存
            EditorUtility.SetDirty(Selection.activeGameObject);
        }
    }
}

完整脚本:

代码语言:javascript复制
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

namespace SK.Framework
{
    public class ImageTool : Editor
    {
        /// <summary>
        /// Image转RawImage
        /// </summary>
        [MenuItem("CONTEXT/Image/Convert 2 RawImage")]
        public static void Image2RawImage()
        {
            Image image = Selection.activeGameObject.GetComponent<Image>();
            //Image中Sprite不为空则获取其texture
            Texture2D texture2D = image.sprite ? image.sprite.texture : null;
            var raycastTarget = image.raycastTarget;
            //销毁Image组件
            DestroyImmediate(image);
            //添加RawImage组件
            RawImage rawImage = Selection.activeGameObject.AddComponent<RawImage>();
            rawImage.texture = texture2D;
            rawImage.raycastTarget = raycastTarget;
            //SetDirty以保存
            EditorUtility.SetDirty(Selection.activeGameObject);
        }
        /// <summary>
        /// RawImage转Image
        /// </summary>
        [MenuItem("CONTEXT/RawImage/Convert 2 Image")]
        public static void RawImage2Image()
        {
            RawImage rawImage = Selection.activeGameObject.GetComponent<RawImage>();
            Sprite sprite = null;
            //如果RawImage组件中的texture不为空
            if (rawImage.texture != null)
            {
                //获取texture的资源路径
                var path = AssetDatabase.GetAssetPath(rawImage.texture);
                //根据该资源路径加载Sprite
                sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path);
            }
            var raycastTarget = rawImage.raycastTarget;
            //销毁RawImage组件
            DestroyImmediate(rawImage);
            //添加Image组件
            Image image = Selection.activeGameObject.AddComponent<Image>();
            image.sprite = sprite;
            image.raycastTarget = raycastTarget;
            //SetDirty以保存
            EditorUtility.SetDirty(Selection.activeGameObject);
        }
    }
}

0 人点赞