UnityEditor打包错误
问题描述
- Unity Editor在开发的时候固然好用,但是到打包的时候就会报错:
The type or namespace name `UnityEditor’ could not be found
解决方案
- 使用条件编译,打包的时候跳过UnityEditor里面的代码即可
1 2 3 4 5
| using UnityEditor; ... #if UNITY_EDITOR your code #endif
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #if UNITY_EDITOR
[CustomEditor(typeof(InitTrafficLights))] public class EditorInitTrafficLight : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); InitTrafficLights scriptLight = (InitTrafficLights)target;
if (GUILayout.Button("Create Light")) { scriptLight.ReadXML(); } } }
#endif
|
Tips:
- 添加条件编译后,#if与#endif中间的代码在打包时就不会被编译,所有相关逻辑代码不应该放在UnityEditor中
- UnityEditor应该进行函数调用,而不是函数的逻辑实现