上下文菜单和统一编辑器

Context menu and unity editor

我需要知道一种方法来代替“assetPreview.GetAssetPreview...因为我最近才发现 assetpreview 属于 Unityeditor 函数所以我无法构建我的 apk 错误是Assets\Scripts\GameManager.cs(36,24):错误 CS0103:名称 'AssetPreview' 在当前上下文

中不存在
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class GameManager : MonoBehaviour
{
    public GameObject[] spaceshipPrefabs;
    public int[] spaceshipPrices = new int[] { 0, 5, 10, 15, 20, 25, 30, 35, 40 };
    public Texture2D[] spaceshipTextures;


    public static GameManager Instance;

    private int currentSpaceshipIdx = 0;
    public int CurrentSpaceshipIdx => currentSpaceshipIdx;


    public GameObject currentSpaceship => 
    `enter code here`spaceshipPrefabs[currentSpaceshipIdx];
    

    public int currentLevelIdx = 0;


    private void Awake()
    {
        if (Instance == null) 
        {
            Instance = this;
        }

        spaceshipTextures = new Texture2D[spaceshipPrefabs.Length];
        for(int i = 0; i < spaceshipPrefabs.Length; ++i)
        {
            GameObject prefab = spaceshipPrefabs [i];
            Texture2D texture = AssetPreview.GetAssetPreview (prefab);
            spaceshipTextures [i] = texture;
        }

        DontDestroyOnLoad(gameObject);

    }

    public void ChangeCurrentSpaceship(int idx)
    {

        currentSpaceshipIdx = idx;
    }
    


}

如果我对你的理解正确,你可能会做一些事情,例如

[Tooltip("Folder path relative to \"Assets\" where to store the generated texture files")]
[SerializeField] private string previewFolder = "PreviewTextures";

#if UNITY_EDITOR
[ContextMenu("Generate Previews")]
private void GeneratePreviews()
{
    var previewFolderPath = Path.Combine(Application.dataPath, previewFolder);

    // Delete existing folder
    if(Directory.Exists(previewFolderPath))
    {
        Directory.Delete(previewFolderPath, true);
    }

    // Then create a new empty one
    Directory.CreateDirectory(previewFolderPath);

    spaceshipTextures = new Texture2D[spaceshipPrefabs.Length];
    for(int i = 0; i < spaceshipPrefabs.Length; ++i)
    {
        GameObject prefab = spaceshipPrefabs [i];
        Texture2D texture = AssetPreview.GetAssetPreview(prefab);
    
        // get the binary data for the texture (I would use PNG)
        var bytes = texture.EncodeToPNG();

        // Where to store this file
        // if the prefab name is not unique of course you could also use GUID or something else that is unique
        var fileName = prefab.name + ".png";
        var filePath = Path.Combine(previewFolderPath, fileName);

        File.WriteAllBytes(filePath, bytes);

        AssetDatabase.Refresh();

        // Now comes the important clue
        // you load and reference the newly created image as the texture
        var assetFilePath = $"Assets/{previewFolder}/{fileName}";
        spaceshipTextures[i] = (Texture2D)AssetDatabase.LoadAssetAtPath(assetFilePath, typeof(Texture2D));
    }

    // Mark dirty in order to save the array persistent
    EditorUtility.SetDirty(this);
    
    AssetDatabase.Refresh();
}
#endif