- Home /
How to instantiate Component from Context Menu in Editor
How would I test if the selected object is a component?
Selection.activeGameObject != null
&& PrefabUtility.GetPrefabType(Selection.activeGameObject) == PrefabType.Prefab;
I tested this way if the selected Object is a prefab and instantiated the prefab when this returns true. That works so far. How would I test if the selected object is a Component (MonoBehaviour) such that I can use AddComponent for all selected GameObjects to attach it via AddComponent(selectedObject.GetType())?
I have found
var s = Selection.activeObject as MonoScript;
and
MonoScript MonoScript.FromMonoBehaviour(MonoBehaviour m);
However I would require the other way, something like:
AddComponent(MonoBehaviour.FromMonoScript(MonoScript m).getType());
$$anonymous$$eep in $$anonymous$$ind that a GameObject is not a component itself, but it contains components. Use GameObject.GetComponent to access them:
if ( Selection.activeGameObject.GetComponent <YourComponent>() != null ) {
// ...
}
I know, but this is not the question. This question is about how to attach a new instance of a $$anonymous$$onoBehaviour to a gameobject from a right click context menu on the script file in the project view. Script files are no game objects, they are $$anonymous$$onoScripts, which are text assets. Therefore it won't work this way.
$$anonymous$$y bad, I guess I got this wrong. I'm not sure if that's possible at all. What I know is that you can simply drag any $$anonymous$$onoBehaviour script onto the GameObject or you could add an custom entry to the "AddComponent" menu in the inspector like this:
[AddComponent$$anonymous$$enu("$$anonymous$$yComponents/$$anonymous$$yScript")]
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
}
The latter is the convention I'm pretty sure. The right click menu in the project view is always the same for each file and is meant to handle filesystem changes rather than changes in your scene.
$$anonymous$$aybe that helps? :)
EDIT if you want to be able to add multiple components at once I think drag and drop is your only option
Answer by Bunny83 · Nov 23, 2017 at 03:38 PM
The MonoScript class has a method called GetClass (actually it's the only instance method it introduces). It returns the System.Type object of the class that is represented by this MonoScript object. This can be the MonoBehaviour or ScriptableObject with the same name as the script file.
To add this class as component you can simply pass the System.Type object to AddComponent.
var s = Selection.activeObject as MonoScript;
if (s != null)
{
var type = s.GetClass();
// check if type is actually a Component
if (type != null && typeof(Component).IsAssignableFrom(type))
{
yourGameObject.AddComponent(type);
}
}
Oh Bunny83, I love you. I really hoped you would see this question. You are always a great help. Thank you!
Answer by jeffreyrampineda · Nov 23, 2017 at 10:47 AM
Try if(something.GetType() == typeof(MonoBehaviour)){ // do something }
$$anonymous$$onoScript is not a $$anonymous$$onoBehaviour, but a text asset containing the link to the script in the ProjectView. I am pretty sure therefore there is no way to do it this way. A TextAsset directly inherits from Object as Component - the base class for $$anonymous$$onoBehaviour does. They share an ancestor but are not in the same line of inheritance.
Also this check doesn't reall work for any component as you can't have a "$$anonymous$$onoBehaviour" instance, only a $$anonymous$$onoBehaviour derived type. However you check if "something" is of type $$anonymous$$onoBehaviour which will always fail.
If you want to know if something is derived from $$anonymous$$onoBehaviour you usually use the "is" operator:
if (something is $$anonymous$$onoBehaviour)
If for some reason you only have the System.Type object for a type you need to use IsAssignableFrom method like this:
if (typeof($$anonymous$$onoBehaviour).IsAssignableFrom(yourType))
The method works a bit "backwards". It checks if a variable of the first type can accept an instance of the second type. So it checks if a $$anonymous$$onoBehaviour variable can accept an instance of "yourType". Of course this is all part of the reflection system. The normal way if you have an instance is to use the "is" or "as" operator.
Though this also doesn't really address the problem in question ^^.
Your answer
Follow this Question
Related Questions
Editor update whilst displaying dialogue? 1 Answer
Avoid using MonoBehaviour just for StartCoroutine or WWW load 0 Answers
namespaces and script names 0 Answers
Get Monobehaviour play mode instance from editor instance 0 Answers
Selection box not working 0 Answers