Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Select from a component's public variables in editor

Discussion in 'Scripting' started by Ishkur, Oct 6, 2015.

  1. Ishkur

    Ishkur

    Joined:
    Nov 18, 2011
    Posts:
    26
    I know making variables public will display them in editor as a component's properties.

    I'd like to be able to set a script as a property of a component, and then be able to view and select from that script's public variables.

    For example, I have Script A attached to a GameObject in scene with a public GameObject field which makes a field visible in editor. Using that field, I can reference any other script, say Script B. Upon referencing Script B, all of Script B's public variables would be visible in editor and selectable via drop down menu.

    Is there any sort of Serializable call I can make in Script B or Script A to make those variables viewable in editor?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This involves a fair bit of editor magic and reflection. But its certainly doable.

    You can use the RemoteFloatField and its property drawer from trend.Me as a place to start. Its only set up to accept numerical data types, but it could easily be adapted for your use case.

    https://bitbucket.org/BoredMormon/trend.me
     
  3. Ishkur

    Ishkur

    Joined:
    Nov 18, 2011
    Posts:
    26
    A working example of what I'd like to happen is seen in the new UI Button component OnClick field. The user select a Game Object the button will act on OnClick, and from there a drop down list of executable methods associated with that Game Object is then shown.

    I'd just like to select any game object and then select variables associated with the scripts on that object.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    That's pretty much what the remote field does in TrendMe.
     
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    In script you can define an event and expose it in Unity Inspector as so..

    Code (CSharp):
    1. using UnityEngine.Events;
    2.  
    3. [System.Serializable]
    4. public class OnEvent : UnityEvent { }
    5.  
    6.  
    7. // Expose in your script.
    8. [SerializeField] OnEvent myEvent;