Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# EditorGUILayout.ObjectField selection referencing its own object variables

Discussion in 'Scripting' started by yumupdate, Jul 19, 2013.

  1. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    30
    I don't know if I'm describing what I want correctly in the title, but let me give a simple example of what I would like to happen.

    I have a structure like this in the Inspector:

    --> List<Operations> (custom class)
    --->Operation (contains MonoScript variable and an OperationScript variable)

    1.) The Operations list contains individual Operation objects.
    2.) Operation objects are setup to accept any MonoScript item in the editor, because I can't figure out how to make it only allow script derived from the Operation class (which would make this so much easier.)

    I can extend the list, add Operation items and select a MonoScript for the Operation.

    I want to take that MonoScript that I select, determine the class (GetClass?, I presume) and assign that class to a variable in the same Operation the editor has generated that will handle executing code in-game that's specific to that MonoScript. For example: I select my ChangeTransform script and after selecting it, a ChangeTransform object is instantiated and assigned to the OperationScript variable in that same Operation object.

    If there's an easier way to do this, I would love to know! Thanks!
     
    Last edited: Jul 19, 2013
  2. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    30
    Ouch. A case of RTFM on my part. You can use "target" anywhere in the Editor derived class to get the object you're inspecting. <facepalm>
     
  3. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    30
    I'm still having issues getting the CustomEditor to actually work. I have a custom class (not inheriting from anything) called EDExtraParams. It's got a string in it and that's it.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class EDExtraParams
    6. {
    7.     public string testString= "";
    8. }
    9.  
    I then use the following for the Inspector's code:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System;
    4. using System.Collections;
    5.  
    6.  
    7. [CustomEditor(typeof(EDExtraParams))]
    8. class EDOperation2Editor : Editor
    9. {
    10.     public override void OnInspectorGUI()
    11.     {
    12.         EditorGUIUtility.LookLikeInspector();
    13.  
    14.         Color greenish = EditorGUILayout.ColorField(Color.green, new GUILayoutOption[0]);
    15.     }
    16. }
    But nothing happens in the inspector. It's still showing just the public properties of ExtraParams. Any advice on what I did wrong there?
     
  4. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,892
    Your script code looks fine to me. Is your custom editor script in a folder called Editor as it should be? Is your Inspector perhaps accidentally in Debug mode?

    Rune
     
  5. yumupdate

    yumupdate

    Joined:
    Nov 20, 2012
    Posts:
    30
    The Editor-derived script is in a folder named "Editor" in the root "Assets" folder of my project. I double-checked my Inspector settings and it appears to be set to "Normal".

    It works if EDExtraParams inherits MonoBehavior and drop it directly on an object, but when it's a property of another custom class dropped on an object, then it doesn't show up correctly.

    Thanks for the help in troubleshooting this with me.

    Edit: I believe what I may be looking for is a "PropertyDrawer" instead of what I have. It just took finding and using the proper terminology in Google. I'll update if this works for me.

    Edit2: "CustomPropertyDrawer" was exactly what I needed. It's working like a champ now :) Thanks to anyone who read through all my junk and I hope this helps clear up confusion for someone else as to how to achieve similar results.
     
    Last edited: Jul 19, 2013