C# 如何在Unity3d自定义编辑器窗口中为垂直GUILayouts添加背景图像

C# 如何在Unity3d自定义编辑器窗口中为垂直GUILayouts添加背景图像,c#,unity3d,C#,Unity3d,我已经在unity3d开发工具上工作了4周。我从工具中获得了我想要的功能。但是,我不知道如何在Unity3d自定义编辑器窗口中将背景图像添加到GUILayOuts。 这是我的密码: using UnityEngine; using UnityEditor; using System.Collections; using System.Collections.Generic; public class InventoryItemEditor : EditorWindow { public In

我已经在unity3d开发工具上工作了4周。我从工具中获得了我想要的功能。但是,我不知道如何在Unity3d自定义编辑器窗口中将背景图像添加到GUILayOuts。

这是我的密码:

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class InventoryItemEditor : EditorWindow {

public InventoryItemList inventoryItemList;

private int viewIndex = 1;

[MenuItem ("Window/Inventory Item Editor %#e")]
static void  Init () 
{
    EditorWindow.GetWindow (typeof (InventoryItemEditor));
}

void  OnEnable () {

    if(EditorPrefs.HasKey("ObjectPath")) 
    {
        string objectPath = EditorPrefs.GetString("ObjectPath");
        inventoryItemList = AssetDatabase.LoadAssetAtPath (objectPath, 
     typeof(InventoryItemList)) as InventoryItemList;
    }

}


void  OnGUI () {


  GUILayout.BeginHorizontal();
  GUILayout.BeginVertical("box", GUILayout.MaxWidth(150), GUILayout.MaxHeight(350));
    GUILayout.BeginHorizontal();
                GUILayout.Label ("Side NavBar", EditorStyles.boldLabel);

    GUILayout.EndHorizontal();


   showcard() ; 
   GUILayout.EndVertical();
   GUILayout.BeginVertical("box");

    GUILayout.BeginHorizontal ();
    GUILayout.Label ("Scene Editor", EditorStyles.boldLabel);
    if (inventoryItemList != null) {
        if (GUILayout.Button("Show Item List")) 
        {
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = inventoryItemList;
        }
    }
    if (GUILayout.Button("Open Item List")) 
    {
            OpenItemList();
    }
    if (GUILayout.Button("New Item List")) 
    {
        EditorUtility.FocusProjectWindow();
        Selection.activeObject = inventoryItemList;
    }
    GUILayout.EndHorizontal ();
    if (inventoryItemList == null) 
    {
        GUILayout.BeginHorizontal ();
        GUILayout.Space(10);
        if (GUILayout.Button("Create New Item List", GUILayout.ExpandWidth(false))) 
        {
            CreateNewItemList();
        }
        if (GUILayout.Button("Open Existing Item List", GUILayout.ExpandWidth(false))) 
        {
            OpenItemList();
        }
        GUILayout.EndHorizontal ();
    }

        GUILayout.Space(20);

    if (inventoryItemList != null) 
    {
        GUILayout.BeginHorizontal ();

        GUILayout.Space(10);

        if (GUILayout.Button("Prev", GUILayout.ExpandWidth(false))) 
        {
            if (viewIndex > 1)
                viewIndex --;
        }

        GUILayout.Space(5);
        if (GUILayout.Button("Next", GUILayout.ExpandWidth(false))) 
        {
            if (viewIndex < inventoryItemList.itemList.Count) 
            {
                viewIndex ++;
            }
        }

        GUILayout.Space(60);

        if (GUILayout.Button("Add Item", GUILayout.ExpandWidth(false))) 
        {
            AddItem();
        }
        if (GUILayout.Button("Delete Item", GUILayout.ExpandWidth(false))) 
        {
            DeleteItem(viewIndex - 1);
        }

        GUILayout.EndHorizontal ();
        if (inventoryItemList.itemList == null)
            Debug.Log("Inventory is empty");
        if (inventoryItemList.itemList.Count > 0) 
        {
            GUILayout.BeginHorizontal ();
            viewIndex = Mathf.Clamp (EditorGUILayout.IntField ("Current Item", viewIndex, 
            GUILayout.ExpandWidth(false)), 1, inventoryItemList.itemList.Count);
            //Mathf.Clamp (viewIndex, 1, inventoryItemList.itemList.Count);
            EditorGUILayout.LabelField ("of   " +  
            inventoryItemList.itemList.Count.ToString() + "  items", "", 
            GUILayout.ExpandWidth(false));
            GUILayout.EndHorizontal ();

             inventoryItemList.itemList[viewIndex-1].itemIcon = EditorGUILayout.ObjectField ("Item Icon", inventoryItemList.itemList[viewIndex-1].itemIcon, typeof (Texture2D), false) as Texture2D;
             inventoryItemList.itemList[viewIndex-1].Actor1 = EditorGUILayout.ObjectField ("Actor1", inventoryItemList.itemList[viewIndex-1].Actor1, typeof (GameObject), false) as GameObject;
             inventoryItemList.itemList[viewIndex-1].Actor2 = EditorGUILayout.ObjectField ("Actor2", inventoryItemList.itemList[viewIndex-1].Actor2, typeof (GameObject), false) as GameObject;
             inventoryItemList.itemList[viewIndex-1].Actor3 = EditorGUILayout.ObjectField ("Actor3", inventoryItemList.itemList[viewIndex-1].Actor3, typeof (GameObject), false) as GameObject;

             inventoryItemList.itemList[viewIndex-1].itemName = EditorGUILayout.TextField ("Item Name", inventoryItemList.itemList[viewIndex-1].itemName as string);
             inventoryItemList.itemList[viewIndex-1].itemScene = EditorGUILayout.TextField ("New Scene", inventoryItemList.itemList[viewIndex-1].itemScene as string);
             inventoryItemList.itemList[viewIndex-1].itemWBook = EditorGUILayout.TextField ("What WorldBook Section?", inventoryItemList.itemList[viewIndex-1].itemWBook as string);
             inventoryItemList.itemList[viewIndex-1].itemACT = EditorGUILayout.TextField ("What Act?", inventoryItemList.itemList[viewIndex-1].itemACT as string);
             inventoryItemList.itemList[viewIndex-1].itemBEAT = EditorGUILayout.TextField ("xxxxx?", inventoryItemList.itemList[viewIndex-1].itemBEAT as string);
             inventoryItemList.itemList[viewIndex-1].itemCard = EditorGUILayout.FloatField ("What is the Card id?", inventoryItemList.itemList[viewIndex-1].itemCard,  GUILayout.ExpandWidth(false));
             inventoryItemList.itemList[viewIndex-1].itemWCount = EditorGUILayout.FloatField ("How many Words per card", inventoryItemList.itemList[viewIndex-1].itemWCount,  GUILayout.ExpandWidth(false));
             inventoryItemList.itemList[viewIndex-1].itemVerson = EditorGUILayout.FloatField ("What is your version number", inventoryItemList.itemList[viewIndex-1].itemVerson,  GUILayout.ExpandWidth(false));
             inventoryItemList.itemList[viewIndex-1].itemStatus = EditorGUILayout.TextField ("Project Status", inventoryItemList.itemList[viewIndex-1].itemStatus as string);
             inventoryItemList.itemList[viewIndex-1].Description = EditorGUILayout.TextField ("Description", inventoryItemList.itemList[viewIndex-1].Description as string);


            GUILayout.Space(10);

            GUILayout.BeginHorizontal ();
            inventoryItemList.itemList[viewIndex-1].isUnique = (bool)EditorGUILayout.Toggle("Unique", inventoryItemList.itemList[viewIndex-1].isUnique, GUILayout.ExpandWidth(false));
            inventoryItemList.itemList[viewIndex-1].isIndestructible = (bool)EditorGUILayout.Toggle("Indestructable", inventoryItemList.itemList[viewIndex-1].isIndestructible,  GUILayout.ExpandWidth(false));
            inventoryItemList.itemList[viewIndex-1].isQuestItem = (bool)EditorGUILayout.Toggle("QuestItem", inventoryItemList.itemList[viewIndex-1].isQuestItem,  GUILayout.ExpandWidth(false));
            GUILayout.EndHorizontal ();

            GUILayout.Space(10);

            GUILayout.BeginHorizontal ();
            inventoryItemList.itemList[viewIndex-1].isStackable = (bool)EditorGUILayout.Toggle("Stackable ", inventoryItemList.itemList[viewIndex-1].isStackable , GUILayout.ExpandWidth(false));
            inventoryItemList.itemList[viewIndex-1].destroyOnUse = (bool)EditorGUILayout.Toggle("Destroy On Use", inventoryItemList.itemList[viewIndex-1].destroyOnUse,  GUILayout.ExpandWidth(false));
            inventoryItemList.itemList[viewIndex-1].encumbranceValue = EditorGUILayout.FloatField("Encumberance", inventoryItemList.itemList[viewIndex-1].encumbranceValue,  GUILayout.ExpandWidth(false));
            GUILayout.EndHorizontal ();

            GUILayout.Space(10);

            GUILayout.BeginHorizontal ();

            string json = JsonUtility.ToJson(inventoryItemList.itemList[viewIndex-1]);
            EditorStyles.textField.wordWrap = true;

            EditorGUILayout.TextArea("Story Logs: "+ json);
            GUILayout.EndHorizontal ();
        } 
        else 
        {
            GUILayout.Label ("This Inventory List is Empty.");
        }
    }

   GUILayout.EndVertical();
   GUILayout.EndHorizontal();

    if (GUI.changed) 
    {
        EditorUtility.SetDirty(inventoryItemList);
    }
}

void showcard()
{
Stack<int> theStack = new Stack<int>();

                //  Add items to the stack.
                for (int number = 15; number >= 1; number--)
                {
                    theStack.Push(number);
                }

                foreach (int number in theStack)
               {

                if (GUILayout.Button("Card "+ number))
                    {

                             {

                                        viewIndex =number;

                                }
                    }             
                }

}
void CreateNewItemList () 
{
    // There is no overwrite protection here!
    // There is No "Are you sure you want to overwrite your existing object?" if it exists.
    // This should probably get a string from the user to create a new name and pass it ...
    viewIndex = 1;
    inventoryItemList = CreateInventoryItemList.Create();
    if (inventoryItemList) 
    {
        inventoryItemList.itemList = new List<InventoryItem>();
        string relPath = AssetDatabase.GetAssetPath(inventoryItemList);
        EditorPrefs.SetString("ObjectPath", relPath);
    }
}

void OpenItemList () 
{
    string absPath = EditorUtility.OpenFilePanel ("Select Inventory Item List", "", "");
    if (absPath.StartsWith(Application.dataPath)) 
    {
        string relPath = absPath.Substring(Application.dataPath.Length - "Assets".Length);
        inventoryItemList = AssetDatabase.LoadAssetAtPath (relPath, 
    typeof(InventoryItemList)) as InventoryItemList;
        if (inventoryItemList.itemList == null)
            inventoryItemList.itemList = new List<InventoryItem>();
        if (inventoryItemList) {
           EditorPrefs.SetString("ObjectPath", relPath);
        }
    }

 }

void AddItem () 
{
    InventoryItem newItem = new InventoryItem();
    newItem.itemName = "New Item";
    inventoryItemList.itemList.Add (newItem);
    viewIndex = inventoryItemList.itemList.Count;
}

void DeleteItem (int index) 
{
    inventoryItemList.itemList.RemoveAt (index);
}
}
使用UnityEngine;
使用UnityEditor;
使用系统集合;
使用System.Collections.Generic;
公共类InventoryItemEditor:EditorWindow{
公共清单项目清单清单清单项目清单;
私有int viewIndex=1;
[菜单项(“窗口/库存项目编辑器%#e”)]
静态void Init()
{
EditorWindow.GetWindow(typeof(InventoryItemEditor));
}
void-OnEnable(){
if(EditorPrefs.HasKey(“ObjectPath”))
{
string objectPath=EditorPrefs.GetString(“objectPath”);
inventoryItemList=AssetDatabase.LoadAssetPath(objectPath,
(InventoryItemList)作为InventoryItemList的类型;
}
}
void OnGUI(){
GUILayout.BeginHorizontal();
GUILayout.beginternal(“框”,GUILayout.MaxWidth(150),GUILayout.MaxHeight(350));
GUILayout.BeginHorizontal();
GUILayout.Label(“侧导航栏”,editorstyle.boldLabel);
GUILayout.EndHorizontal();
showcard();
GUILayout.EndVertical();
GUILayout.Beginterial(“盒子”);
GUILayout.BeginHorizontal();
Label(“场景编辑器”,editorstyle.boldLabel);
如果(inventoryItemList!=null){
if(GUILayout.按钮(“显示项目列表”))
{
EditorUtility.FocusProjectWindow();
Selection.activeObject=inventoryItemList;
}
}
if(GUILayout.按钮(“未清项列表”))
{
OpenItemList();
}
if(GUILayout.按钮(“新项目列表”))
{
EditorUtility.FocusProjectWindow();
Selection.activeObject=inventoryItemList;
}
GUILayout.EndHorizontal();
如果(inventoryItemList==null)
{
GUILayout.BeginHorizontal();
空间(10);
if(GUILayout.Button(“创建新项目列表”,GUILayout.ExpandWidth(false)))
{
CreateNewItemList();
}
if(GUILayout.Button(“打开现有项目列表”,GUILayout.ExpandWidth(false)))
{
OpenItemList();
}
GUILayout.EndHorizontal();
}
Guillayout.空间(20);
如果(inventoryItemList!=null)
{
GUILayout.BeginHorizontal();
空间(10);
if(GUILayout.Button(“Prev”,GUILayout.ExpandWidth(false)))
{
如果(视图索引>1)
视图索引--;
}
空间(5);
if(GUILayout.Button(“下一步”,GUILayout.ExpandWidth(false)))
{
if(viewIndex0)
{
GUILayout.BeginHorizontal();
viewIndex=Mathf.Clamp(EditorGUILayout.IntField(“当前项”),viewIndex,
ExpandWidth(false)),1,inventoryItemList.itemList.Count);
//Mathf.Clamp(viewIndex,1,inventoryItemList.itemList.Count);
EditorGUILayout.LabelField(“of”+
inventoryItemList.itemList.Count.ToString(),
GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
inventoryItemList.itemList[viewIndex-1]。itemIcon=EditorGUILayout.ObjectField(“项目图标”,inventoryItemList.itemList[viewIndex-1]。itemIcon,typeof(Texture2D),false)作为Texture2D;
inventoryItemList.itemList[viewIndex-1]。Actor1=EditorGUILayout.ObjectField(“Actor1”,inventoryItemList.itemList[viewIndex-1]。Actor1,typeof(GameObject),false)作为游戏对象;
inventoryItemList.itemList[viewIndex-1]。Actor2=EditorGUILayout.ObjectField(“Actor2”,inventoryItemList.itemList[viewIndex-1]。Actor2,typeof(GameObject),false)作为游戏对象;
inventoryItemList.itemList[viewIndex-1]。Actor3=EditorGUILayout.ObjectField(“Actor3”,inventoryItemList.itemList[viewIndex-1]。Actor3,typeof(GameObject),false)作为游戏对象;
inventoryItemList.itemList[viewIndex-1].itemName=EditorGUILayout.TextField(“项目名称”,inventoryItemList.itemList[viewIndex-1].itemName为字符串);
inventoryItemList.itemList[viewIndex-1].itemScene=EditorGUILayout.TextField(“新场景”,inventoryItemList.itemList[viewIndex-1].itemScene作为字符串);
inventoryItemList.itemList[viewIndex-1].itemWBook=EditorGUILayout.TextField(“什么世界书章节?”,inventoryItemList.itemList[viewIndex-1].itemWBook作为字符串);
inventoryItemList.itemList[viewIndex-1]。itemACT=EditorGUILayout.TextField(“什么动作?”,inventoryItemList.itemList[viewIndex-1]。itemACT作为字符串);
inventoryItemList.itemList[viewIndex-1]。itemBEAT=EditorGUILayout.TextField(“xxxxx?”,inventoryItemList.itemList[viewIndex-1]。itemBEAT为字符串);
inventoryItemList.itemList[viewIndex-1]。itemCard=EditorGUILayout.FloatField(“卡id是什么?”,inventoryItemList.itemList[viewIndex-1]。itemCard,GUILayout.ExpandWidth(false));
inventoryItemList.itemList[viewIndex-1].itemWCount=EditorGUILayout.FloatField(“每张卡的字数”,inventoryItemList.itemList[viewIndex-1]。itemWCount,GUILayout.ExpandWidth(false));
inventoryItemList.itemList[viewIndex-1].itemVerson=EditorGUILayout.FloatField(“您的版本号是什么”,inventoryItemList.itemList[viewIndex-1]。它
Texture2D bImage;
...
GUIStyle bImageStyle = new GUIStyle(EditorStyles.label);
...
GUILayout.Button(bImage,bImageStyle);