How to add Background Images for Vertical GUILayouts in Unity3d Custom Editor Window
I have been working 4 weeks on a unity3d Dev Tool. I have the functionality I want from the tool. However, I dont understand how to add a background image to GUILayOuts in Unity3d Custom Editor Window.
Here is my code:
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);
}
}
My Research
I have Been looking in to GUIStyle, GUISkins and Texture2ds. For all I am a little confused on how to add them to the code to enable the ablity to dynamically add background images.
I can use GUIStyles to Change the color of Text but dont understand how to add an image behind the text. I also dont need this to by Dynamic. if i can add a static Image to each "GUILayout.BeginVertical("box")" I would have a working solution.
Finally, I have learned that i can get code hints by adding a "." after GUILayout and/or a code hint from "," after "GUILayout.BeginVertical("box"<,> )".
Any help getting to the solution would be must appreciated.
Your answer
Follow this Question
Related Questions
Camera.Render calls expensive UIEvents.WillRenderCanvases 2 Answers
Horizontal scrollview with images constant height in Unity3D UI - How? 0 Answers
Best way to add file chooser dialog 0 Answers
Receiving strange error for first time 1 Answer
How to make Grid Layout Buttons in Custom Editor Window? 2 Answers