- Home /
Can't use UnityEditor methods in non derived class when building apk
Hi. I'm trying to use static class in my game with methods from UnityEditor namespace.
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class Utils {
public static List<T> GetAllPrefabsWithType<T>() where T : MonoBehaviour
{
string[] temp = AssetDatabase.GetAllAssetPaths();
List<string> paths = new List<string>();
List<T> result = new List<T>();
foreach (string s in temp)
{
if (s.Contains(".prefab"))
paths.Add(s);
}
foreach (string path in paths)
{
T prefab = AssetDatabase.LoadAssetAtPath(path, typeof(T)) as T;
if (prefab != null)
result.Add(prefab);
}
return result;
}
}
And I use code in different scrips directly, for example:
List<string> itemKeys = Utils.GetAllPrefabsWithType<InventoryItem>().Select(x => x.Key).ToList();
In editor it works perfectly but when I'm trying to build apk, I get an error : The type or namespace name `UnityEditor' could not be found. Are you missing an assembly reference? (It for AssetDatabase.LoadAssetAtPath...)
Anyone knows how I can resolve this? I could to transform Utils class to singleton and derive from MonoBehavior, but I would like to solve this issue more elegant.
Answer by IJM · Apr 19, 2018 at 10:21 AM
You can't use UnityEditor in your builds.
How to solve this problem?
Create a class that inherits MonoBehavior, that has a public GameObject array, add it to an object as a component.
Use your Editor script to assign that array with references to your prefabs.
You can than save the object with that component as an prefab, or keep it on the scene.
You can make a public reference to that MonoBehavior class from any other class you need your prefabs in, and assign it through Unity Editor.
If you want to load prefabs from file, take a look at AssetBundles.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity apk installing 2 icons 2 Answers
APK can download to phone, but cannot open on device 0 Answers
Signed .apk error 0 Answers