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

举报
CoderZ1010 发表于 2022/09/25 07:13:18 2022/09/25
【摘要】 1.Image转RawImage: 代码如下: using UnityEditor; using UnityEngine; using UnityEngine.UI; namespace SK.Framework { public class ImageTool : Editor { /// &...

1.Image转RawImage:

代码如下:

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);
        }
    }
}

2.RawImage转Image:

代码如下:

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);
        }
    }
}

完整脚本:

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);
        }
    }
}

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/123571276

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。