- Home /
Custom editor makes inspector view act weirdly.
Hello,
I've created a script that ended up making my inspector view looking from the right way to a really weird way (pics bellow) :
Before
After
Needless to say I don't want this behavior. Where did I screw up so badly ?
Here is my custom Script code
[CustomEditor(typeof(GameObject))]
public class AutoSnapObject : Editor {
void OnSceneGUI()
{
if (Event.current.type == EventType.KeyDown && Event.current.character=='p')
{
RaycastHit hit;
GameObject go = ((SnapTarget)GameObject.FindObjectOfType(typeof(SnapTarget))).gameObject;
if (go==null)
{
Debug.Log("no snap target found");
return;
}
if (go==target)
{
Debug.Log("currently selected object is the snap target");
return;
}
Collider coll = go.GetComponent<Collider>();
if (coll==null)
{
Debug.Log("no collider detected at the snap target");
return;
}
GameObject targetObject = (GameObject) target;
if (targetObject==null)
{
Debug.Log("invalid cast");
return;
}
Vector3 normalizedDir = coll.transform.position-targetObject.transform.position;
if (normalizedDir.magnitude <0.04f)
return;
normalizedDir=Vector3.Normalize(normalizedDir);
coll.Raycast(new Ray(targetObject.transform.position,normalizedDir),out hit,Mathf.Infinity);
targetObject.transform.position=hit.point + hit.normal*0.03f;
targetObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Event.current.Use();
}
}
}
Answer by Michael CMS · Nov 06, 2012 at 09:23 PM
Ok Solved it by changing
[CustomEditor(typeof(GameObject))]
to
[CustomEditor(typeof(Transform))]
Anyone has any clues as to why using GameObject is causing this behavior to happen ?
I believe on Unity's back end they have written a custom inspectors for the GameObject class and the Transform class. If you make a CustomEditor for those classes it overrides Unity's implementation of their custom inspector. By drawing their default inspectors you are telling Unity to not use their CustomEditor so ins$$anonymous$$d you get what would be serialized if Unity was using no custom inspectors
Diet Chugg,
Do you know where one could find the gameObject inspector class? I found the transform inspector class but can't seem to hunt down the GameObject class.
The "GameObjectInspector" class as well as the "TransformInspector" class are internal classes inside the UnityEngine.dll which you can't access without using massive reflection.
You really should avoid replacing the inspector of any built-in object, especially if you plan to release your editor extension for others. It was never intended and isn't recommended to replace the GameObject or Transform inspector.
If you just want to add generic SceneView code, use the SceneView class and the "onSceneGUIDelegate" to register a callback.
Your answer
