Unity3D: error CS0246: The type or namespace name `UnityEditor' could not be found.

Trabla: Unity3D: error CS0246: The type or namespace name `UnityEditor' could not be found.


Unity3D error CS0246 - howto fix tutorial


Trabla Description:

1. Simple project

2. Script "MyBuildScript.cs" to add custom menu to Unity3d Editor
- compile project for Androdi or iOS

Located in Assets folder

Unity3D error CS0246 - howto fix tutorial 1


3. Source code of  "MyBuildScript.cs" script:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

class MyBuildScript
{
static string[] SCENES = FindEnabledEditorScenes();
static string APP_NAME = "Demo";
static string TARGET_DIR = "C:\\Users\\Public\\Documents\\Unity Projects\\demo-buildScript";

[MenuItem("Custom/CI/Build Mac OS X")]  // Creates menu item in unity editor
public static void PerformMacOSXBuild()
{
string target_dir = APP_NAME + ".app";
GenericBuild(SCENES, TARGET_DIR + "/" + target_dir, BuildTarget.StandaloneOSXIntel, BuildOptions.None);
}
[MenuItem("Custom/CI/Build Android")]   // Creates menu item in unity editor
public static void PerformAndroidBuild()
{
string target_dir = APP_NAME + ".apk";
GenericBuild(SCENES, TARGET_DIR + "/" + target_dir, BuildTarget.Android, BuildOptions.None);
}

private static string[] FindEnabledEditorScenes()
{
List<string> EditorScenes = new List<string>();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (!scene.enabled) continue;
EditorScenes.Add(scene.path);
}
return EditorScenes.ToArray();
}
static void GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
{
EditorUserBuildSettings.SwitchActiveBuildTarget(build_target);
string res = BuildPipeline.BuildPlayer(scenes, target_dir, build_target, build_options);
if (res.Length > 0)
{
throw new System.Exception("BuildPlayer failure: " + res);
}
}
}

Unity3D error CS0246 - howto fix tutorial 2


4. Try to run build script from Editor  - menu "Custom" -> "CI" -> "Build Android"


5. Exception:

Assets/MyBuildScript.cs(4,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing a using directive or an assembly reference?

Unity3D error CS0246 - howto fix tutorial 3


Solving:

1. Create folder "Editor" in "Assets" folder

Unity3D error CS0246 - howto fix tutorial 4


2. Move script "MyBuildScript.cs" to "Assets" -> "Editor" folder

Unity3D error CS0246 - howto fix tutorial 5


All scripts with using UnityEditor; should be placed in "Assets" -> "Editor" folder

No comments:

Post a Comment