Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional compilation for Editor vs. VRChat #33

Closed
ArcanoxDragon opened this issue May 24, 2020 · 3 comments · Fixed by #53
Closed

Conditional compilation for Editor vs. VRChat #33

ArcanoxDragon opened this issue May 24, 2020 · 3 comments · Fixed by #53
Labels
enhancement New feature or request

Comments

@ArcanoxDragon
Copy link

Feature Description:
In order to test interaction in the Unity editor, one must use OnMouseDown instead of Interact to capture clicks. This also captures mouse clicks in VRChat, but not VR interaction, so it cannot actually be used in a VRChat world. If you use both OnMouseDown and Interact, you get double invocations for desktop VRChat users.

Because of this, I would like a way to only declare OnMouseDown when the script is compiled during Unity's "Run" mode, but not when the world is built to be published to VRChat (or tested in a local VRC instance for that matter).

The solution I propose is to simply define a compiler symbol that can be used like so:

public class WhateverBehavior : UdonBehaviour
{

#if UNITY_PLAY

    private void OnMouseDown()
    {
        this.Interact();
    }

#endif

    public override void Interact()
    {
        // ...
    }
}

Forgive me if this is already possible somehow; I tried using #if UNITY_EDITOR but it seems that either UdonSharp doesn't support conditional compilation yet, or the UNITY_EDITOR symbol is not defined at play-time inside Unity, because OnMouseDown was never called unless I removed the conditional compilation.

@ArcanoxDragon ArcanoxDragon added the enhancement New feature or request label May 24, 2020
@MerlinVR
Copy link
Owner

MerlinVR commented May 24, 2020

U# supports the preprocessor directives #if, #elif, #else, and #endif by default as part of the Roslyn AST parsing. But at the moment it does not copy the define symbols such as UNITY_EDITOR from the project settings. This will be added eventually, but requires separate builds for in-game vs in-editor. Which means build pre-process steps that compile the game version of the scripts, and then clean up and build the editor scripts again after the upload is finished. It also requires storing distinct debugging information for each type of build.

In the mean time if you want to check if some code is running in the editor, you can check if Networking.localPlayer is null. For the Interact event specifically, you also have a button in the UdonBehaviour inspector that will show up when you have the Interact event defined that you can click while in play mode to trigger the interact event.

button

@ArcanoxDragon
Copy link
Author

Oh, nice! I have my game window set to maximize-on-play so I've never noticed that button before.

@GotoFinal
Copy link

Also you can use my GotoUdon tool, it will by default call onInteract on mouse button press in game window

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants