Search Unity

Obsessive compulsive over a Custom Editor component unable to expose himself...

Discussion in 'Scripting' started by JFR, Oct 25, 2014.

  1. JFR

    JFR

    Joined:
    Feb 21, 2014
    Posts:
    65
    For the life of me, I cannot get this thing to return anything but null. My intent is to get a class which derives from the Editor class (who therefore should be a ScriptableObject class) to expose itself as a MonoScript, so I can just click it and get it highlighted in the project window.

    To alleviate the fact that I cannot get an object from a target like I would from MonoScript.FromMonoBehaviour, I've tried using MonoScript.FromScriptableObject (this) in the custom editor component. But this returns null. So I tried to create an instance of the class, like in: ScriptableObject.CreateInstance(this.GetType()) but this does not work either.

    Anyone know of a quick and dirty way to get a Custom Editor script to be displayed as a clickable property in the inspector?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Editorscripts are not components, so cannot be attatched to gameobjects - so you'll never see them in the inspector.

    As for pinging the script in the project window, these scripts works for me:
    Code (CSharp):
    1. //Editor window:
    2. using UnityEngine;
    3. using UnityEditor;
    4. using System.Collections;
    5.  
    6. public class PingTest : EditorWindow {
    7.     [MenuItem( "EDITORS/Ping Window" )]
    8.     static void Init () {
    9.         PingTest window = (PingTest) EditorWindow.GetWindow( typeof( PingTest ) );
    10.         window.Show();
    11.         window.position = new Rect( 20, 80, 100, 80 );
    12.     }
    13.  
    14.     void OnGUI () {
    15.         if ( GUILayout.Button( "Ping my script" ) ) {
    16.             EditorGUIUtility.PingObject( MonoScript.FromScriptableObject( this ) );
    17.         }
    18.     }
    19. }
    20.  
    21.  
    22. //----OR----
    23.  
    24.  
    25. //Customised inspector for "theScript" class
    26. using UnityEngine;
    27. using UnityEditor;
    28. using System.Collections;
    29.  
    30. [CustomEditor( typeof( theScript ) )]
    31. public class PingTest : Editor {
    32.  
    33.    public override void OnInspectorGUI () {
    34.      if ( GUILayout.Button( "Ping my script" ) ) {
    35.        EditorGUIUtility.PingObject( MonoScript.FromScriptableObject( this ) );
    36.      }
    37.    }
    38. }
    39.  
    40.  
     
  3. JFR

    JFR

    Joined:
    Feb 21, 2014
    Posts:
    65
    Thanks! That did the trick. Thanks for taking the time to post your code.