Search Unity

Targetting Unity and WPF

Discussion in 'Scripting' started by MaxPalmer, Sep 12, 2017.

  1. MaxPalmer

    MaxPalmer

    Joined:
    Mar 9, 2013
    Posts:
    27
    I have a 2D / 3D library that I have written in C# that I would like to use for procedural / runtime mesh creation in Unity as part of a VR application. I have updated the code to strip out any Windows dependencies so it's now callable from Unity. However, going forwards I would like to still be able to compile it for use on Windows as well as using it in Unity - maintaining a single code base but with optimised geometry / types for the build target.

    One thing I want to do is switch my code to use some of Unity math / geometry types - e.g. vectors, etc. to make the code fast / efficient and to remove unnecessary data transformations. I can think of some approaches to doing this, including:

    * Using pre-processor definitions and build targets. What's the easiest way of doing this if there are lots of instances of a class type in use that needs to be substituted?
    * Refactoring the code to use interfaces, factories and some sort of IOC container and switching out the concrete type at runtime based on Unity or Windows.
    * Using some form of template generation - e.g. T4?
    * Give up and create a dedicated Unity version of the code base.

    I also have a strong suspicion that this code would be ideal for the C# job system and a data driven design. I certainly want to push a lot of the processing to background tasks or worker threads and will be operating on transform / mesh data.

    Thoughts?
     
  2. MaxPalmer

    MaxPalmer

    Joined:
    Mar 9, 2013
    Posts:
    27
    To clarify - by way of an example I have vector/point classes in the code base, but would like to use the native vector classes when targeting Unity. I also believe I will have some (Unity) native assemblies where it makes sense to do so. However I believe that most of the existing project would naturally support multiple targets.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    If your type names and API mirror Unity's, you can do this simply through platform-specific imports:

    #if UNITY
    using Vector3 = UnityEngine.Vector3
    #else
    using Vector3 = MyLib.Vector3
    #endif

    The problem there is that becomes a pretty large import statement, and you have to do it in every file.

    An alternative is to have files containing your own definitions for those types, but only include those files on non-Unity platforms.
     
  4. MaxPalmer

    MaxPalmer

    Joined:
    Mar 9, 2013
    Posts:
    27
    Thanks. I'm making reasonably good progress with a custom build target, #if #endif and the above.