user3915050
user3915050

Reputation:

Platform Dependent Excecution

Is there a way to tell if a project is used by Unity whether that be compiled to XBOX, Windows, Android... or still in the editor

I am creating a class that will allow either System.Numerics.Vector3 or UnityEngine.Vector3 based on whether or not UnityEngine is available, but it needn't be platform dependent just from or in Unity, I am creating a set of extensions that I want to be able to use inside and outside of Unity without having to define custom #define UNITY

I have no way to compile for many platforms so is it as easy as #if UNITY_5

To Clarify what I an looking for the software I am creating is NOT going to be exlusively used within a Unity Project, It is being designed to run virtually anywhere, Unity, UWP, WFP, Windows Console etc.

Upvotes: 0

Views: 239

Answers (3)

derHugo
derHugo

Reputation: 90679

Not quite sure why the UnityEngine namespace should not not exist in any Unity made project - on any target platform.

It is the main engine core so why do you think it should not be there?


Pre-Processor that covers any Unity version (since 5.3.4)

Anyway from using the general #if pre-processors (See Unity Manual - Platform dependent compilation) starting from Unity version 5.3.4 you can check whether you are in any Unity version by simply using

#if UNITY_5_3_OR_NEWER
    // Use UnityEngine
#else
    // Use something else
#endif

which as the name says covers all Unity versions above 5.3.4 and any project/app built with it regardless of the target device.


Use a global define

Also instead of your own

#define UNITY

in each and every script you can go to the Player Settings (EditProject SettingsPlayer) and add your own global custom define in the Unity Editor (and a build of course) for all scripts:

Open the Other Settings panel of the Player Settings and navigate to the Scripting Define Symbols text box.

enter image description here

Here you can add a ;-separated list of your own global defines. There you could also simply enter UNITY and then use

#if UNITY
    // Use Unity stuff
#else
    // Use other stuff
#endif

Add global define automatically via Editor script

And now the cool thing: You can add such a global define also via editor script!

Therefore e.g. simply add a script in your library within a folder called Editor:

#if !YOURDEFINE
    // We only need this class once if the global define is not added yet
    // The above YOURDEFINE has to exactly match the DEFINE declared below
    // Once it is already defined in the PlayerSettings it stays defined permanently
    // so we don't need to run this class on every project load
    using UnityEngine;
    using UnityEditor;

    internal class StartUp
    {
        /// <summary> 
        /// The define to be added to the global defines list.
        /// <para>This needs to match exactly the #if pre-processor
        /// on top of this file!</para>
        /// </summary>
        private const DEFINE = "YOURDEFINE";

        /// <summary>
        /// automatically executed once the project is loaded and after
        /// every re-compile in the UnityEditor
        /// </summary>
        [InitializeOnLoadMethod]
        private static void Initialize()
        {
            // Get current defines from PlayerSettings
            var defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup);
            // Split into list at ';'
            var defineList = defines.Split(';').ToList();

            // Check if already contains your define
            // Actually this should never happen since if this is the case
            // The entire class should already be stripped of due to the 
            // pre-processor on top of this file
            // Therefore if this happens something is wrong
            if (defineList.Contains(Constants.Define))
            {
                Debug.LogError("Huh?! This should not happen! Please check the DEFINE const and the pro-processor on top of StartUp.cs!");
                return;
            }

            // if not simply append it
            // Unity doesn't care about a leading ;
            // if this is the first entry it is removed automatically
            defines += $";{DEFINE}";

            // This writes back the new defines and evtl. causes a re-compile
            // be careful with that though because if you mess it up
            // you might end up in a permanent re-compile ;)
            PlayerSettings.SetScriptingDefineSymbolsForGroup(EditorUserBuildSettings.selectedBuildTargetGroup, defines);
        }
    }
#endif

Upvotes: 0

Christopher
Christopher

Reputation: 9804

I am creating a class that will allow either System.Numerics.Vector3 or UnityEngine.Vector3 based on whether or not UnityEngine is available, but it needn't be platform dependent just from or in Unity

The usual way to do cross-platform, is to find the "smalest common denominator". Unity was designed to be cross-platform. And vectors are a core type for every game engine. .NET was a late adopter of Vector3, only getting it in Framework 4.6/Standart 2.1. But again, for a 3D game engine it is a fundamental piece. So I would bet real money on UnityEngine.Vector3 being avalible in every version of the Unity engine.

If that is somehow not the case? Just write your own Vector3. It is a struct with 3 numbers - not exactly a hard thing to copy. Here is a example one:

//structs should be inmutable. readonly makes sure of that
public readonly struct Vector3 {
    int X;
    int Y;
    int Z;
}

I asumed it is integers, but apparently the vector classes actually use Single - a 32-bit float type. But the precise type is a trivial difference.

Upvotes: 1

Quickz
Quickz

Reputation: 1836

You can check out platform dependent compilation on Unity docs - https://docs.unity3d.com/Manual/PlatformDependentCompilation.html

I think that might be what you're looking for. They already have bunch of premade directives ready for use so you don't have to invent your own.

Example:

#if UNITY_EDITOR
            // Do some stuff
#elif UNITY_STANDALONE_WIN
            // Do some stuff
#endif

Upvotes: 1

Related Questions