Is there a way to add logic to Attributes in C# / Unity based on platform?

Viewed 113

I'm working on a Unity project that has integration to google firebase, namely the firestore. For iOS and android I'm using the sdk's from google which allows me to use some Firebase attributes on my fields to make reading and writing easier.

Attributes looks like, [FirestoreData] & [FirestoreProperty]

However, the SDK is not compatible with the webgl builds. I'd like to use the same classes for simplicity but the attributes mean it won't compile for webgl.

One solution, that I'm currently using, it just to duplicate the class sans attributes, e.g.

#if UNITY_WEBGL
public class SomeData
{
    public float height { get; set; }
    public float width { get; set; }
    public float depth { get; set; }
}
#else
[FirestoreData]
public class SomeData
{
    [FirestoreProperty] public float height { get; set; }
    [FirestoreProperty] public float width { get; set; }
    [FirestoreProperty] public float depth { get; set; }
}
#endif

but I wanted to know if there was a way to avoid this duplication. Can you tell c# to ignore certain attributes with the directives? e.g.

[FirestoreProperty && !UNITY_WEBGL] public float depth { get; set; }
1 Answers

Resolved by adding my own custom attributes just for webgl that do nothing. Fixes the compile error without having to add in hundreds of #if UNITY_WEBGL and #endif. Two files, FirestoreDataAttribute.cs & FirestorePropertyAttribute.cs

#if UNITY_WEBGL
using System;

public class FirestoreDataAttribute : Attribute
{
}
#endif

#if UNITY_WEBGL
using System;
public class FirestorePropertyAttribute : Attribute
{
}
#endif

Thanks and credit to @madreflection for the idea. :)

Related