Search code examples
c#unity-game-enginecontextmenuandroid-build

Context menu and unity editor


i need to know of a method to use instead of "assetPreview.GetAssetPreview...because i only found out recently that assetpreview belongs to the function Unityeditor so i am not able to build my apk the error is Assets\Scripts\GameManager.cs(36,24): error CS0103: The name 'AssetPreview' does not exist in the current context

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


}

Solution

  • If I understand you correctly you could probably do something like e.g.

    [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
    

    enter image description here