Unity3d GUI.Button hierarchyWindowItemOnGUI 内的文本更改

Unity3d GUI.Button text change within hierarchyWindowItemOnGUI

这是完整的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 () {

    }
}

我想更改在层次结构项目上绘制的 GUI.Button 文本。 GameObjects 的激活和停用仅适用于 Button 文本不会改变,它保持为 "On".

编辑 1// 添加了完整的 class 并重新表述了我的问题

看看 HierarchyItemCB 函数中的 string btnStr = "On";

每次调用 HierarchyItemCB 函数时,btnStr 变量都会重新初始化为 "On"

HierarchyItemCB 函数外声明并初始化 btnStr 变量。也标记为static,这样就可以在HierarchyItemCB函数中访问了,也是一个static函数。


编辑:

这很棘手,因为 HierarchyPlus 必须 是静态函数。一种解决方案是使用字典。使用 instanceID 作为键,使用按钮 string 作为值。

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()
    {

    }
}