Search Unity

C# - Check if namespace or method exists before using it

Discussion in 'Scripting' started by TwiiK, Nov 23, 2014.

  1. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    I'm making a game which is using Eric5h5's Vectrosity, but I want to give away the entire project at the end, obviously without including Vectrosity. I want to create the project so that if the user doesn't add Vectrosity to the project it will just fall back to using line renderers instead.

    My code looks something like this:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class DrawObject : MonoBehaviour {
    6.  
    7.     List<Vector3> linePoints = new List<Vector3>();
    8.     Vectrosity.VectorLine myLine;
    9.  
    10.     public void CreateVectrocityLine() {
    11.         myLine = new Vectrosity.VectorLine("Line", linePoints, null, 2.0f, Vectrosity.LineType.Continuous);
    12.         myLine.drawTransform = transform;
    13.     }
    14.    
    15.     void Update() {
    16.         myLine.Draw3D();
    17.     }
    18.  
    19.     public void SetVertexPosition(int i, Vector3 pos) {
    20.         linePoints.Add(new Vector3(pos.x, pos.y, 0));
    21.     }
    22. }
    Currently I'm getting a bunch of these errors obviously:
    So I want all my Vectrocity code to be conditional in some way, but I'm not sure how to do that. My experience is mainly with PHP and there things like this is so easy. You can just wrap your code in with if(class_exists("foo")) or if(function_exists("bar")) and everything's fine. But in C# I'm not sure if this is even possible. And I'm not even sure if the answers I find when googling apply to my situation.

    Another problem is that it seems I can't even write an if() statement outside my methods. So how can I conditionally define my "myLine" variable?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,536
    Because it's a compile time check, you can't really do a runtime check for it.

    There's several ways to do it. Here's some ideas.

    1) compile two versions of the library, one that support Vectrosity, another that doesn't. This means delivering the project as dll's.



    2) have a compile time conditional flag for if Vectrosity exists, and then use conditionals for it:

    Code (csharp):
    1.  
    2. #if VECTROSITY_EXISTS
    3.    ...code for vectrosity
    4. #else
    5.    ...code without vectrosity
    6. #endif
    7.  
    Think like if you use the "#UNITY_EDITOR" conditional. This would allow for a source code distribution. But the onus is on the user to make sure the conditional flag exists in the csproj file.



    3) This is the method I'd probably go with personally, as I like 'interfaces'. I would define some interfaces for the classes that can use Vectrosity. Say I had to have some geometry represented either by Vectrosity, or by some other system.

    Code (csharp):
    1.  
    2. interface IGeom
    3. {
    4.     ...methods for what makes up a geometry class
    5. }
    6.  
    Then I'd implement two versions.

    VectrosityGeom
    DefaultGeom

    VectrosityGeom wraps the Vectrosity code and forwards calls into it.

    Then you can have a factory class that checks if Vectrosity.VectorLine exists, and if it does, it returns the Vectrosity version of IGeom, if not it returns the DefaultGeom.
     
  3. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Thanks for the comprehensive reply.

    It sounds like 3) is my only option. Never actually used interfaces before so I'll see what I end up with. :p

    This is just a small game so I won't spend much time on it, I just thought it would be a neat feature. I guess if I can't make interfaces work I will just leave the Vectrosity part of the code commented out and people can comment it back in if they have Vectrosity.
     
  4. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Ahh, I can just define the conditional flag myself, comment it out and let people uncomment it if they have Vectrosity. That's easier than them having to uncomment a bunch of code in my project. Neat. :p

    But I will still check out interfaces. See if I can learn something new while making this game. :p

    Thanks again.