- Home /
How to check if other Asset exists?
Hey all. I tried posting this in the Scripting and the Asset Store forum, but wasn't able to find a good answer, so I thought I would try here instead. I have a problem that I would like to resolve but am not sure exactly where to start. Essentially I have an asset that I'm working on that has extra behaviour supported if another one of my assets is included. Essentially I've boiled it down to the following example.
Asset 1:
namespace Package1 {
//The class that I want to extend from in my new asset
public class Class_A {
public int IntField;
}
}
Asset 2:
namespace Package2 {
public interface SomeInterface {
void DoAThing();
}
public class Class_B : SomeInterface {
public void DoAThing() {
Debug.Log("Does A Thing");
}
}
//This will cause compile errors if the user does not also have Package1.
public class SomeClass_A : Package1.Class_A, SomeInterface {
public void DoAThing() {
Debug.Log(IntField);
}
}
}
My question is essentially, how can I resolve the issue of determining if the user has Package1 or not? Essentially, I want to provide bonus functionality for people who have Package1, but it shouldn't cause compile errors if someone only wants Package2. I know that some people have added support for other assets such as PlayMaker and I want to know how to do similar, but for my own packages.
Thanks for any help!
Answer by Dave-Carlile · Jun 29, 2015 at 09:17 PM
You should be able to use Reflection for this.
From this Stackoverflow answer...
var instances = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetInterfaces().Contains(typeof(ISomething))
That will get all of the types in the executing assembly, then get a list of interfaces to see if any of them are a specific interface type. This probably isn't exactly what you're looking for, but it kind of gives you a good place to start exploring.
I can't declare a class and extend another type by way of reflection.
Yeah, that would be difficult, but do you need to extend the class? Your new class can hold a reference to an instance of the other object and call methods in it. Your new class becomes kind of a wrapper around the old one, rather than a descendent.
Because I have specialized editor script for Class_A from the first package and unfortunately because of how awful Unity handles generalized classes, you can't use:
public ExampleGeneric<T> ExampleField;
ins$$anonymous$$d you have to do something like this:
[Serializeable]
public class ConcreteIntVersion : ExampleGeneric<int> { }
public ConcreteIntVersion ExampleIntVersion;
So if I want to create for my 2nd package a class that includes a version of that uses Package2.CustomType as the type for ExampleGeneric, I have to do it like this:
public class ConcreteCustomVersion : ExampleGeneric<Package2.CustomType> { }
But of course I don't want to do this if it's going to cause compile errors when the user of Package2 isn't using Package1 which includes ExampleGeneric.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201531im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unloading Broken Assembly | How to solve this !? 2 Answers
how to keep track of Lean Tween function? 0 Answers
Mirror : How to Sync child objects active status on join? 0 Answers