Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Proper Use of #if UNITY_EDTOR

Discussion in 'Scripting' started by ElroyToscano, Jan 14, 2021.

  1. ElroyToscano

    ElroyToscano

    Joined:
    Jan 8, 2018
    Posts:
    1
    Hello Community,

    I've got a public method in a class to whom I make calls to from the target object of the editor script of the same class. However, I don't want the method to be public as other classes have access to it. So I want it to be specifically public to the editor class of that specific class.
    While researching, I came across #if UNITY_EDITOR as it is made specifically for Editor calls. But while implementing it, inspite of placing the methods inside the #if UNITY_EDITOR those methods are being able to be called from other classes.
    Is there a proper example other than this: https://docs.unity3d.com/Manual/PlatformDependentCompilation.html ?
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    What you are talking about is Platform Dependent Compilation. You are telling the compiler that, this portion of code will only exist while it is used in a certain platform. A better example will be #if_IOS, which means the portion of code within it will only exist for apple users but no one else. So your code is still accessible because it is being run in the Unity editor.

    So using platform dependent compilation may or may not fit your needs, its impossible to tell without knowing your situation or intentions.
     
    Vryken likes this.