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.