繁体   English   中英

如何使用按钮索引为特定的单击按钮着色?

[英]How can I color the specific clicked button using the button index?

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using UnityEditor;
using UnityEngine;
using System.IO;

public class Manager : EditorWindow
{
    private static bool red;

    [MenuItem("Tools/Manager")]
    static void Managers()
    {
        EditorWindow.GetWindow<Manager>();
        Init();
    }

    private static void Init()
    {
        red = true;
    }

    private void OnGUI()
    {
        GUIContent c = new GUIContent();

        var style = new GUIStyle(GUI.skin.button);

        if (red)
        {
            style.normal.textColor = Color.red;
        }
        else
        {
            style.normal.textColor = Color.green;
        }
        style.fontSize = 18;

        string[] Paths = new string[2];
        string[0] = "testing1";
        string[1] = "testing2";

        for (int i = 0; i < Paths.Length; i++)
        {
            if (Paths[i].Contains("test")) // or .js if you want
            {
                var x = assetPaths[i].LastIndexOf("/");
                var t = assetPaths[i].Substring(x + 1);

                GUILayout.Button(t, style, GUILayout.Width(1000), GUILayout.Height(50));
                if (Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains(Event.current.mousePosition))
                {
                    OnMouseOver(i);
                }
            }
        }
    }

    private void OnMouseOver(int buttinIndex)
    {
        UnityEngine.Debug.LogFormat("OnMouseOver {0}", buttinIndex);
        red = !red;
    }
}

问题是,当我将鼠标光标移到其中一个按钮上时,它会将所有按钮涂成绿色,然后又变回红色。

我希望当我单击特定按钮或将鼠标光标移到特定按钮颜色上时,仅此按钮为绿色,而将按钮保留为绿色。

您应该记录每个按钮使用列表或其他样式的样式。

目前,您的代码导致所有按钮使用一种相同的样式,因此它们会一起更改。

我提供小费。 希望对您有所帮助。

private void OnMouseOver(int buttinIndex)
{
    UnityEngine.Debug.LogFormat("OnMouseOver {0}", buttinIndex);
    //red = !red; 
    //try to record style or color flag for each button use index.
}

当你画按钮

GUILayout.Button(t, GetStyleFromList(i), GUILayout.Width(1000), GUILayout.Height(50));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM