亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

錯誤 CS0103:名稱“AssetPreview”在當前上下文中不存在

錯誤 CS0103:名稱“AssetPreview”在當前上下文中不存在

C#
慕森卡 2023-06-25 14:30:28
在我的 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”在當前上下文中不存在我哪里做錯了?
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

UnityEditor.AssetPreview屬于UnityEditor命名空間。

這只存在于 Unity 編輯器 istelf 中,并在構建中被刪除。

=> 您不能UnityEditor在構建中使用命名空間中的任何內容。


基本上有兩種解決方案可以UnityEditor從構建中排除某些內容:

預處理器

在 C# 中,您可以使用 in#if preprocessor來根據全局定義排除代碼塊。Unity 提供了這樣的定義。在這種情況下使用例如

#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


查看完整回答
反對 回復 2023-06-25
  • 1 回答
  • 0 關注
  • 263 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號