在自定义编辑器检查器中的多个GUILayout按钮上加载不同的纹理

时间:2017-03-05 15:36:09

标签: c# unity3d unity5

我正在制作一个自定义编辑器,我希望在3个单独的GUILayout按钮上加载3个单独的纹理(图标图像),但很难这样做。让我解释一下:我正在从Resources / Prefabs文件夹加载3个预制件,然后我为每个预制件分配一个GUILayout按钮,就像这样:

                 Object[] obj = Resources.LoadAll("Prefabs", typeof(GameObject));
                 objects = new GameObject[obj.Length];

                 for (int i = 0; i < obj.Length; i++)
                 {
                     objects[i] = (GameObject)obj[i];
                 }

                 if (objects != null)
                 {
                     for (int j = 0; j < obj.Length; j++)
                     {
                         if (GUILayout.Button("", GUILayout.Width(70), GUILayout.Height(70)))
                         {
                            selectedObject = objects[j];
                         }
                     }
                 }

如何为每个按钮加载单独的纹理?纹理位于Resources / Textures文件夹中。

提前致谢!

1 个答案:

答案 0 :(得分:0)

为什么不使用GUILayout.Button的适当重载?您应该查看GUILayout.Button的文档。

例如,

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(ScriptName))]
public class ExampleOnInspector : Editor
{
    public Texture[] icons;
    void Awake()
    {
        //Load textures into icons by `AssetDatabase.LoadAssetAtPath<T>(assetPath)`
    }

    public override void OnInspectorGUI()
    {
        if (GUILayout.Button(icons[0]))
            Debug.Log("Clicked the icon 0");
        if (GUILayout.Button(icons[1]))
            Debug.Log("Clicked the icon 1");
        if (GUILayout.Button(icons[2]))
            Debug.Log("Clicked the icon 2");
    }
}

PS:
如果您的纹理仅用于编辑器脚本,我建议您put them in Assets/Editor Default Resources。否则它们将包含在您的运行时资源中。