简体   繁体   English

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

[英]error CS0103: The name 'AssetPreview' does not exist in the current context

In my Unity Project I've C# code to get Prefabs preview image in png format.在我的 Unity 项目中,我使用 C# 代码来获取 png 格式的预制件预览图像。

In Unity Play mode I have not errors with this Script and everything it's ok, but when I trie to build my project I've receive error.在 Unity Play 模式下,我的脚本没有错误,一切正常,但是当我尝试构建我的项目时,我收到了错误。

I spend a lot of times to try understand where I'd made a mistake, but no result.我花了很多时间试图了解我在哪里犯了错误,但没有结果。

Can some body help with this problem?一些身体可以帮助解决这个问题吗?

C# Script 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);
        }
    }
}

UnityEditor.AssetPreview belongs to the UnityEditor namespace. UnityEditor.AssetPreview属于UnityEditor命名空间。

This only exists in the Unity Editor istelf and is stripped of in a build. 它仅存在于Unity Editor istelf中,并在构建中删除。

=> You can't use anything fromt the UnityEditor namespace in a build. =>您不能在构建中使用UnityEditor名称空间中的任何内容。


There are basically two solutions in order to exclude UnityEditor stuff from builds: 为了从版本中排除UnityEditor内容,基本上有两种解决方案:

Pre-processors 预处理器

In c# you can use the #if preprocessor in order to exclude code blocks depending on global defines. 在c#中,可以使用#if preprocessor ,以便根据全局定义排除代码块。 Unity offers such defines . Unity提供了这样的定义 In this case use eg 在这种情况下,使用例如

#if UNITY_EDITOR
    // CODE USING UnityEditor
#endif

Editor folder Editor文件夹

If an entire script shall be excluded from a build you can simply place it in a folder named Editor . 如果要从构建中排除整个脚本,则可以将其放置在名为Editor的文件夹中。 This will make it completely stripped of from a build. 这将使它完全脱离构建。


For using this in a build you would either have to use another library or run this script once within the Unity Editor and store those references for using them in a build eg using the [ContextMenu] attribute: 为了在构建中使用它,您要么必须使用另一个库,要么在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

嗨阿克塞尔你有没有找到解决这个问题的方法?

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误cs0103名称&#39;IEnumerator&#39;在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context Unity错误CS0103:当前上下文中不存在名称`&#39; - Unity error CS0103: The name `' does not exist in the current context 错误 CS0103: 当前上下文中不存在名称“ ” - error CS0103: The name ' ' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“x” - error CS0103: The name 'x' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“IsInRole” - ERROR CS0103: The name 'IsInRole' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM