Reputation:
I am having problem with building a game. it keep saying 'Error building Player because scripts have compile errors in the editor'
I followed tips from this and still got nothing on working.
a friend suggested me to add
public static class EditorUtil
{
#if UNITY_EDITOR
//Editr code here
#endif
}
and it still continues with same error.
this is the full code:
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public static class EditorUtil
{
#if UNITY_EDITOR
//Editr code here
static string m_EditorResourcesPath = string.Empty;
private static string path = string.Empty;
internal static string editorResourcesPath
{
get
{
if (string.IsNullOrEmpty(m_EditorResourcesPath))
{
string path;
if (SearchForEditorResourcesPath(out path))
m_EditorResourcesPath = path;
else
Debug.LogError("Unable to locate editor resources. Make sure the PostProcessing package has been installed correctly.");
}
return m_EditorResourcesPath;
}
}
static bool SearchForEditorResourcesPath(out string path)
{
path = string.Empty;
string searchStr = EditorUtil.path;
string str = null;
foreach (var assetPath in AssetDatabase.GetAllAssetPaths())
{
if (assetPath.Contains(searchStr))
{
str = assetPath;
break;
}
}
if (str == null)
return false;
path = str.Substring(0, str.LastIndexOf(searchStr) + searchStr.Length);
return true;
}
internal static T Load<T>(string path, string name) where T : Object
{
EditorUtil.path = path;
return AssetDatabase.LoadAssetAtPath<T>(editorResourcesPath + name);
}
private static List<string> layers;
private static string[] layerNames;
public static LayerMask LayerMaskField(string label, LayerMask selected)
{
if (layers == null)
{
layers = new List<string>();
layerNames = new string[4];
}
else
{
layers.Clear ();
}
int emptyLayers = 0;
for (int i = 0; i < 32; i++)
{
string layerName = LayerMask.LayerToName (i);
if (layerName != "")
{
layers.Add (layerName);
}
else
{
emptyLayers++;
}
}
if (layerNames.Length != layers.Count)
{
layerNames = new string[layers.Count];
}
for (int i=0; i < layerNames.Length; i++)
layerNames[i] = layers[i];
selected.value = EditorGUILayout.MaskField (label, selected.value, layerNames);
return selected;
}
public static Rect GetCurrentRect (float fieldSize = 18)
{
return GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(fieldSize));
}
public static GameObject GetSelectedGameObject ()
{
return Selection.activeGameObject;
}
public static AnimationClip GetAnimationClipFromAnimator(Animator animator, string name)
{
if (animator == null)
return null;
foreach (AnimationClip animClip in animator.runtimeAnimatorController.animationClips)
{
if (animClip.name == name)
return animClip;
}
return null;
}
#endif
}
whats a solution for this? I've searched many posts in here and other forums and yet I keep getting the same error.
Edit: second pic
Upvotes: 0
Views: 2312
Reputation: 131
Since above script is using UnityEditor
, either u have to put this script inside folder named Editor or you have to use defines #if UNITY_Editor
. What you have done is also correct but i figured that you might be calling those methods from somewhere, maybe that's why it is still giving compile error. What it does is it will make those methods to exist in Editor only, it won't be there in build. So instead of whole methods, i just used #if UNITY_EDITOR
for block of code.
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
public static class EditorUtil
{
static string m_EditorResourcesPath = string.Empty;
private static string path = string.Empty;
internal static string editorResourcesPath {
get {
if (string.IsNullOrEmpty (m_EditorResourcesPath)) {
string path;
if (SearchForEditorResourcesPath (out path))
m_EditorResourcesPath = path;
else
Debug.LogError ("Unable to locate editor resources. Make sure the PostProcessing package has been installed correctly.");
}
return m_EditorResourcesPath;
}
}
static bool SearchForEditorResourcesPath (out string path)
{
path = string.Empty;
string searchStr = EditorUtil.path;
string str = null;
#if UNITY_EDITOR
foreach (var assetPath in AssetDatabase.GetAllAssetPaths()) {
if (assetPath.Contains (searchStr)) {
str = assetPath;
break;
}
}
#endif
if (str == null)
return false;
path = str.Substring (0, str.LastIndexOf (searchStr) + searchStr.Length);
return true;
}
internal static T Load<T> (string path, string name) where T : Object
{
#if UNITY_EDITOR
EditorUtil.path = path;
return AssetDatabase.LoadAssetAtPath<T> (editorResourcesPath + name);
#else
return null
#endif
}
private static List<string> layers;
private static string[] layerNames;
public static LayerMask LayerMaskField (string label, LayerMask selected)
{
if (layers == null) {
layers = new List<string> ();
layerNames = new string[4];
} else {
layers.Clear ();
}
int emptyLayers = 0;
for (int i = 0; i < 32; i++) {
string layerName = LayerMask.LayerToName (i);
if (layerName != "") {
layers.Add (layerName);
} else {
emptyLayers++;
}
}
if (layerNames.Length != layers.Count) {
layerNames = new string[layers.Count];
}
for (int i = 0; i < layerNames.Length; i++)
layerNames [i] = layers [i];
#if UNITY_EDITOR
selected.value = EditorGUILayout.MaskField (label, selected.value, layerNames);
#endif
return selected;
}
public static Rect GetCurrentRect (float fieldSize = 18)
{
return GUILayoutUtility.GetRect (GUIContent.none, GUIStyle.none, GUILayout.Height (fieldSize));
}
public static GameObject GetSelectedGameObject ()
{
#if UNITY_EDITOR
return Selection.activeGameObject;
#else
return null
#endif
}
public static AnimationClip GetAnimationClipFromAnimator (Animator animator, string name)
{
if (animator == null)
return null;
foreach (AnimationClip animClip in animator.runtimeAnimatorController.animationClips) {
if (animClip.name == name)
return animClip;
}
return null;
}
}
Upvotes: 1
Reputation: 820
Judging from from your screenshot, you put the script into
Assets/Renegades/Scripts/Utils
However, an Editor script must be put into the Editor folder, not into the Assets folder.
See: https://docs.unity3d.com/Manual/SpecialFolders.html
The UnityEditor Assembly is not accessible to scripts outside the Editor folder. The #if UNITY_EDITOR preprocessor directive does not solve this, because it evaluates to true when the script is run inside the Unity Editor.
Upvotes: 1