【问题标题】:error CS0103: The name 'AssetPreview' does not exist in the current context错误 CS0103:当前上下文中不存在名称“AssetPreview”
【发布时间】:2019-07-15 16:54:45
【问题描述】:

在我的 Unity 项目中,我使用 C# 代码来获取 png 格式的预制件预览图像。

在 Unity Play 模式下,我的脚本没有错误,一切正常,但是当我尝试构建我的项目时,我收到了错误。

我花了很多时间试图了解我在哪里犯了错误,但没有结果。

一些身体可以帮助解决这个问题吗?

C# 脚本

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

public class LoadTexture : MonoBehaviour
{
    [System.Serializable]
    public class LoadTex
    {
        public string name;
        public GameObject texture;
        public Texture2D gameObjectTex;
        public Texture gameObjTex;
    }

    public List<LoadTex> ItemTablezz = new List<LoadTex>();

    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < ItemTablezz.Count; i++)
        {
            var getImage = UnityEditor.AssetPreview.GetAssetPreview(ItemTablezz[i].texture);
            print(getImage);

            ItemTablezz[i].gameObjectTex = getImage;
        }
    }

    // Update is called once per frame
    void Update()
    {
        CheckIfNull();
    }

    void CheckIfNull()
    {
        for (int k = 0; k < ItemTablezz.Count; k++)
        {
            Texture2D tex = new Texture2D(128, 128, TextureFormat.RGBA32, false);
            Color[] colors = ItemTablezz[k].gameObjectTex.GetPixels();
            int i = 0;
            Color alpha = colors[i];
            for (; i < colors.Length; i++)
            {
                if (colors[i] == alpha)
                {
                    colors[i].a = 0;
                }
            }

            tex.SetPixels(colors);
            tex.Apply();
            byte[] png = tex.EncodeToPNG();
            File.WriteAllBytes(Application.persistentDataPath + "/" + ItemTablezz[k].name + ".png", png);
        }
    }
}

错误 CS0103:当前上下文中不存在名称“AssetPreview”

我哪里做错了?

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    UnityEditor.AssetPreview 属于 UnityEditor 命名空间。

    这仅存在于 Unity 编辑器 istelf 中,并在构建中被剥离。

    => 您不能在构建中使用来自 UnityEditor 命名空间的任何内容。


    基本上有两种解决方案可以从构建中排除UnityEditor 的东西:

    预处理器

    在 c# 中,您可以使用 #if preprocessor 来根据全局定义排除代码块。 Unity 提供了这样的defines。在这种情况下使用例如

    #if UNITY_EDITOR
        // CODE USING UnityEditor
    #endif
    

    Editor文件夹

    如果要从构建中排除整个脚本,您只需将其放在名为Editor 的文件夹中。这将使它从构建中完全剥离。


    要在构建中使用它,您要么必须使用另一个库,要么在 Unity 编辑器中运行此脚本一次,然后存储这些引用以在构建中使用它们,例如使用[ContextMenu] 属性:

        void Start()
        {
    #if UNITY_EDITOR
            LoadPreviewImages();
    #endif
    
            // if nothing more comes here
            // rather remove this method entirely
            ...
        }
    
    #if UNITY_EDITOR
        // This allows you to call this method 
        // from the according components context menu in the Inspector
        [ContextMenu("LoadPreviewImages")]
        private void LoadPreviewImages()
        {
            foreach (var loadText in ItemTablezz)
            {
                var getImage = UnityEditor.AssetPreview.GetAssetPreview(loadText.texture);
                print(getImage);
                loadText.gameObjectTex = getImage;
            }
        }
    #endif
    

    【讨论】:

    • 我稍后会尝试您的解决方案并反馈
    猜你喜欢
    • 2019-01-05
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多