Snr Naldo
Snr Naldo

Reputation: 113

Unity3d GUI.Button text change within hierarchyWindowItemOnGUI

Here is the entire class:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class HierarchyPlus{

    static HierarchyPlus()
    {
        EditorApplication.hierarchyWindowItemOnGUI += HierarchyItemCB;
    }

    private static void HierarchyItemCB(int instanceID, Rect selectionRect)
    {
        GameObject go = (GameObject)EditorUtility.InstanceIDToObject(instanceID);
        Rect rect = new Rect(selectionRect);
        rect.x = rect.width - 30;
        string btnStr = "On";
        if (go != null)
        {
            GUI.skin.button.fixedWidth = 30;

            if (GUI.Button(rect, btnStr))
            {
                Debug.Log(go.name);
                if (go.activeSelf == true)
                {
                    go.SetActive(false);
                    btnStr = "Off";
                }
                else
                {
                    go.SetActive(true);
                    btnStr = "On";
                }
            }
        }               
    }    

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
}

I want to change the GUI.Button text which i draw onto the hierarchy item. The activation and deactivation of the GameObjects works only the Button text doesnt change, it stays as "On".

Edit 1// added the full class and reformulated my issue

Upvotes: 0

Views: 242

Answers (1)

Programmer
Programmer

Reputation: 125305

Take a look at string btnStr = "On"; in the HierarchyItemCB function.

The btnStr variable is being re-initialized to "On" each time the HierarchyItemCB function is called.

Declare and initialize the btnStr variable outside the HierarchyItemCB function. Also mark it as static so that you can access it in the HierarchyItemCB function is also a static function.


EDIT:

This is tricky since HierarchyPlus must be a static function. A solution is to use a Dictionary. Use the instanceID as the key and the button string as the value.

using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class HierarchyPlus
{

    static HierarchyPlus()
    {
        EditorApplication.hierarchyWindowItemOnGUI += HierarchyItemCB;
    }

    static Dictionary<int, string> instanceToBtnStr = new Dictionary<int, string>();

    private static void HierarchyItemCB(int instanceID, Rect selectionRect)
    {
        Debug.Log("Over");


        GameObject go = (GameObject)EditorUtility.InstanceIDToObject(instanceID);
        Rect rect = new Rect(selectionRect);
        rect.x = rect.width - 30;


        if (go != null)
        {
            //Add key if it is not in the Dictionary yet
            if (!instanceToBtnStr.ContainsKey(instanceID))
            {
                //Add with Default On Key
                instanceToBtnStr.Add(instanceID, go.activeSelf ? "On" : "Off");
            }

            GUI.skin.button.fixedWidth = 30;

            //Initialize btnStr from Dictionary
            string btnStr = instanceToBtnStr[instanceID];

            if (GUI.Button(rect, btnStr))
            {
                Debug.Log(go.name);
                if (go.activeSelf == true)
                {
                    go.SetActive(false);
                    instanceToBtnStr[instanceID] = "Off";
                }
                else
                {
                    go.SetActive(true);
                    instanceToBtnStr[instanceID] = "On";
                }
            }
        }
    }

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
}

Upvotes: 1

Related Questions