Home > Mobile >  Change function within unity inspector
Change function within unity inspector

Time:09-05

I have a gameObject that needs to call lots of other functions. I was hoping to do something like thisenter image description here

Where I can select the gameobject and script for it to run. I can't figure out how to do this.

I'm calling a function on button click for a gui.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class AvatarSystem : MonoBehaviour
{
    public Button general;
    public Button face;
    public Button hair;
    public Button eyebrows;
    public Button clothing;


    // Start is called before the first frame update
    void Start()
    {
        var root = GetComponent<UIDocument>().rootVisualElement;

        general = root.Q<Button>("general");
        face = root.Q<Button>("face");
        hair = root.Q<Button>("hair");
        eyebrows = root.Q<Button>("eyebrows");
        clothing = root.Q<Button>("clothing");

        general.clicked  = generalClicked;
        face.clicked  = faceClicked;
        hair.clicked  = hairClicked;
        eyebrows.clicked  = eyebrowsClicked;
        clothing.clicked  = clothingClicked;


    }

    void generalClicked()
    {

    }
    void faceClicked()
    {

    }
    void hairClicked()
    {

    }
    void eyebrowsClicked()
    {

    }
    void clothingClicked()
    {

    }


}

The 5 bottom functions should do something like the top image. Thanks

CodePudding user response:

You can this script which creates buttons in the inspector.

This should be outside of your AvatarSystem class.

using Unity.Editor;

[CustomEditor(typeof(AvatarSystem))]
public class AvatarSystemGUI : Editor
{
     public override void OnInspectorGUI()
     {
          AvatarSystem t = AvatarSystem(target);
          if (GUILayout.Button("Test"))
          {
               t.ExampleMethod();
          }
     }
 }

Let me know of any problems! :)

CodePudding user response:

You want to use a Unity Event in Editor Unity Event script

CodePudding user response:

You can use AddListener of UnityEvent; you can write the code like this;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class AvatarSystem : MonoBehaviour
{
    public Button general;
    public Button face;
    public Button hair;
    public Button eyebrows;
    public Button clothing;


    // Start is called before the first frame update
    void Start()
    {
        var root = GetComponent<UIDocument>().rootVisualElement;

        general = root.Q<Button>("general");
        face = root.Q<Button>("face");
        hair = root.Q<Button>("hair");
        eyebrows = root.Q<Button>("eyebrows");
        clothing = root.Q<Button>("clothing");

        general.onClick.AddListener(() => { 
          this.generalClicked();
          this.faceClicked();
          this.hairClicked();
          this.eyebrowsClicked();
          this.clothingClicked();
        }) ; 


    }

    void generalClicked()
    {

    }
    void faceClicked()
    {

    }
    void hairClicked()
    {

    }
    void eyebrowsClicked()
    {

    }
    void clothingClicked()
    {

    }


}
  • Related