- Home /
Detecting another author's lib/package/classes
This has to do with packages/assets being released for the Asset Store.
I've been discussing this with a friend today as we want to collaborate in a way. The new tool I am working on should have the ability to, when it detects the presence of his package, allow additional features while making calls to his code to do the actual work. I was hoping it would be as simple as doing #if SOME_CLASS_NAME (C#) but I see it can't be used like that. So now I am not quite sure how one can write code that will not throw errors if the other package's classes are not present.
So far I can only think of having some unitypackage that can be imported which would add the script(s) that provide the working link between the two.
I would create a script with the needed class and function(s) that my code can call into and these functions would make calls to my friend's scripts. I distribute my tool with an "empty" version of this class, its functions will be be empty/ do nothing and thus have no code that can throw errors cause of the other package's classes not being present.
If a user then installs my friend's package and want my tool to support it he can import/extract the version of my script which has the actual working code.
Any thoughts on this? Is there a better way this could be done? I do not want my customers to really worry about what I described above. Things should just work.
Answer by Leslie-Young · Jun 13, 2013 at 09:41 AM
Since I am only targeting Unity 4 with the tool I am making I could make use of the player settings' scripting define symbols field. My friend will simply add a define to it and then I can use #if HIS_DEFINE_SYMBOL.
Here's a sample of how to add such a symbol via code in an editor script. This code will be executed whenever the code is recompiled by unity, so when the package is imported it will create the needed symbol(s).
#if !(UNITY_2_6 || UNITY_2_6_1 || UNITY_3_0 || UNITY_3_0_0 || UNITY_3_1 || UNITY_3_2 || UNITY_3_3 || UNITY_3_4 || UNITY_3_5)
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
[InitializeOnLoad]
public class SetupMyDefines
{
static SetupMyDefines()
{
// run through all the build targets and add the define symbol if needed
foreach (BuildTargetGroup btg in System.Enum.GetValues(typeof(BuildTargetGroup)))
{
// extract existing defines to check if the ones to be added
// does not allready exist before adding the new defines
string defines_field = PlayerSettings.GetScriptingDefineSymbolsForGroup(btg);
List<string> defines = new List<string>(defines_field.Split(';'));
if (!defines.Contains("SOME_DEFINE_FOR_MY_PACKAGE"))
{
defines.Add("SOME_DEFINE_FOR_MY_PACKAGE");
PlayerSettings.SetScriptingDefineSymbolsForGroup(btg, string.Join(";", defines.ToArray()));
}
}
}
}
#endif
I think it would be nice if more asset store publishers can do this kind of thing for their code related packages so that other who wish to support these packages in whatever way have an easy way of detecting the presence of the package.