RequireComponent from EditorWindow in Custom Editor?

Hello there.

I’m in the middle of scripting a custom window for my game project.

I have 2 scritps.

CustomWindow.cs ← This being a custom window
&&
CustomEditor.cs ← This being a custom editor.

In the editor script using void OnSceneGUI; in order to cast rays to the suface, as well as instantiate objects on the surface.

Now I would like the object to be defined in the custom Window. I have the ObjectField working correctly.

Is there a way I could possibly require a component of “GameObject” from CustomWindow and using this object in CustomEditor?

Help would be gladly appreciated.

~Patryk.

Your wording is a little confusing, you want the object field to only accept gameobjects that have a certain component on them?

2 ways that work:

customComponent theComponent;
GameObject theGO;
...
theComponent = (customComponent) GUI.objectField(...typeof (customComponent) );
//only accept instance of the component
if(theComponent!= null) theGO = myComponent.gameObject
//then fetch the GO from it


//OR

GameObject theGO;
...
theGO = (GameObject) GUI.objectField(... typeof(GameObject) );
//accept any GO
if(theGO.getComponent<theComponent>() == null) theGO = null;
//check for component, clear the obj field if null

I apologize for the confusion, I was in rush while writing this.

Let me be more clear as I have time to actually write some code examples for help.

So here is my custom EditorWindow.

using UnityEngine;
using UnityEditor;
using System.Collections;
public class ClickPositionDrop : EditorWindow {

    public Object source;
    public Transform obj;

    //Adding menu button called "ClickPositionDrop" to the "Lisewski" tab.
    [MenuItem ("Window/Lisewski/ClickPositionDrop")]
    static void Init () {
        ClickPositionDrop window = (ClickPositionDrop)EditorWindow.GetWindow (typeof(ClickPositionDrop));
        window.Show ();
    }

    void OnInspectorUpdate(){
        Repaint ();
    }
   

    void OnGUI () {
        GUILayout.Label ("Chose object to place on surface.", EditorStyles.boldLabel);
            source = EditorGUILayout.ObjectField(source, typeof(Object), true);
        if (GUILayout.Button ("Clear")) {
            if (source == null) {
                ShowNotification(new GUIContent("No object selected"));
            } else {
                source = null;
            }
        }

        Event e = Event.current;
        if (e.button == 0 && e.isMouse)
            Debug.Log("Left Click");

    }
   

}

This code has the

void OnGUI () {
        GUILayout.Label ("Chose object to place on surface.", EditorStyles.boldLabel);
            source = EditorGUILayout.ObjectField(source, typeof(Object), true);

script, which takes a GameObject from the Editor by simply dragging it and dropping it in the specific field and assigned this GameObject to be of a value “source”.

Now here is my custom Editor script.

using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof(ClickPositionDrop))]
public class ClickPositionDropEditor : Editor {

    void OnSceneGUI(){

        Event e = Event.current;
        if (e.button == 0 && e.type == EventType.MouseDown) {
            Debug.Log ("Left Click on Screen");
            Ray ray = HandleUtility.GUIPointToWorldRay (Event.current.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast (ray, out hit)) {
                Debug.Log ("Ray Hit: " + hit.point + ":" + hit.normal);
                Instantiate ([B]source[/B], hit.point, Quaternion.LookRotation(Vector3.Cross(Vector3.forward, hit.normal), hit.normal));
            }
        }
    }
}

See, what I want is the source to be taken from my custom EditorWindow GameObject input.

Can this be done?

Thank you

There’s three things you need to do:

  1. Change line 6 / line 23 of your EditorWindow to use GameObject rather than Object. (You will need to cast the value returned by ObjectField).

  2. Change line 17 of your Editor script to use PrefabUtility.InstantiatePrefab instead of Instantiate.

  3. Use a function like FindObjectOfType() in your Editor script to find your EditorWindow, then access its source variable.