- Home /
UnityEditor.Undo in runtime
If UnityEditor.Undo calls at runtime, as shown below, it can cause problems?
What about when you have been running on the device?
public void Click()
{
Undo.DestroyObjectImmediate(this.GetComponent<BoxCollider2D>());
}
Answer by flashframe · May 16, 2016 at 02:58 PM
(Edit for clarification) Editor code cannot be used in a build of your game
You can use Preprocessor Directives to prevent editor code from running in the build of your game. If this piece of code isn't in an editor script (i.e. inside a folder named "Editor"), then you can wrap the method like this:
#if UNITY_EDITOR
public void Click()
{
Undo.DestroyObjectImmediate(this.GetComponent<BoxCollider2D>());
}
#endif
Or if you have other logic in your method that needs to run in the build, you can do something like this:
public void Click()
{
#if UNITY_EDITOR
Undo.DestroyObjectImmediate(this.GetComponent<BoxCollider2D>());
#elif
//alternate code goes here
#endif
}
no. i didn't want that.
For example the following code works fine in the editor on the PC. I wonder if this be the same result in the devices.
public void Click()
{
Undo.DestroyObjectImmediate(this.GetComponent<BoxCollider2D>());
}
@dgiim2 I have edited my answer for clarification. No, you cannot use editor code like that in your game. It won't work.