-2

I have a line of script which gets a class refrence from third party script(NGUI package):

UIEventListener.Get(this.gameObject).onClick += expiryDateSettingsUIController.ActiveDeactiveUICaller;

The UIEventListener class belongs to NGUI package. My problem is that I don't want to show any error if that particular class doesn't exists. How can I do that?. If the class is not available then it is throwing compiler error and won't allowing me to build the exe.

Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • Can you please add the error you get? If the class is non existent you will always get a compiler error. If it exists however in a certain environmental context maybe some pre-processors could help? (e.g. `#if !UNITY_EDITOR`) – derHugo Nov 06 '18 at 06:15
  • I'm voting to close this question as off-topic because it is about a commercial software package and can not be reproduced by other users. You should rather try to ask directly on the providers page or in the Asset store. – derHugo Nov 06 '18 at 06:43
  • You can only hope that NGUI somehow adds a pre-processor define to Unity (I did this: https://stackoverflow.com/questions/52514658/can-i-define-a-constant-solutionwide-within-c-sharp-code-without-project-settin/53002526#53002526) maybe you can suggest they do it so you could check if it exists – derHugo Nov 06 '18 at 06:57
  • This is only possible with reflection. – Programmer Nov 06 '18 at 07:22

2 Answers2

-1

You can use GetType() to reflect the class you are looking for:

Type mType = Type.GetType("UIEventListener");

if(mType  != null)
{
     UIEventListener.Get(this.gameObject).onClick += expiryDateSettingsUIController.ActiveDeactiveUICaller;
}
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
  • at runtime ... at compile time this won't help much – derHugo Nov 06 '18 at 06:53
  • @derHugo But he asked about runtime isn't? he didn't say directly but he doesn't want it crash in the editor. I believe if you are not sure if you have the class or not... better don't use it, but I think this answers what he is asking. – Ignacio Alorre Nov 06 '18 at 06:56
  • 1
    `If the class is not available then my code crashes in the editor.` since OP says `in the editor` I would think he means it isn't even compiling (ofcourse not since `UIEventListener` will simply not exist) => Your code here will not even reach runtime since Unity doesn't Play if there are compiler errors – derHugo Nov 06 '18 at 06:58
  • @derHugo Ok I understand. Thanks for the clarification. – Ignacio Alorre Nov 06 '18 at 07:03
-1

C# is a compiled language, so before you can run any code, it needs to be error-free. An absent class is a very serious compilation error. There is no way (in Unity or otherwise) that you can achieve the behavior you're asking for.

However, you can use conditional compilation. Conditional compilation, as the name suggests, is when you strip pieces of code out during compilation, so those pieces are never compiled and no compilation errors will be encountered for those. Wrap your code in #if/#endif like below:

#if WITH_NGUI
UIEventListener.Get(this.gameObject).onClick += expiryDateSettingsUIController.ActiveDeactiveUICaller;
#endif

Then, to let this code compile, you will need to define the WITH_NGUI conditional compilation symbol. In Unity, you can go to Player Settings, and there will be a box named Scripting Define Symbols where you can type in as many symbols as you like. Type WITH_NGUI into that box, and the code will compile. When you don't have NGUI available, simply remove the symbol and the code won't compile any more.

This isn't a nice thing to do though. You've been warned.

EDIT: You could also use reflection to access classes which may be absent from your build, but that's way more trouble than it's worth.

Arshia001
  • 1,854
  • 14
  • 19