- Home /
GUI Style Issue-- Texture not changing
Hello,
My code is meant to iterate through a list and set all of the components to "blank."
Blank is a blank inventory space and has its' own GUIStyle associated with it.
I know that my code DOES set everything to blank, but I do not see any changes.
My code is as follows:
IEnumerator SetItemList_Ienum(GUIComponent arrayParent)
{
yield return new WaitForEndOfFrame();
for (int i = 0; i < arrayParent.numberOfChildElements; i++)
{
if (_currentItems.Count > 0)
{
arrayParent.childrenComponents[i] = _currentItems[i].GetComponent<GUIComponent>();
}
else
{
arrayParent.childrenComponents[i] = Item_Special.instance.blankItemGUIComponent;
print(arrayParent.childrenComponents[i].style.normal.background);
}
}
}
The current count of the _currentItems list is 0, so it's all happening in the else fragment.
Also, the print shows that the texture has indeed changed, but there is no visible difference.
The only thing I can think of is that I may have botched up calling apply. I was using this code:
void ApplyTextures()
{
if (_style.normal.background) // Apply the background Texture2D
{
_style.normal.background.Apply(); // Lag.
}
if (_style.hover.background) // Apply the hover Texture2D
{
_style.hover.background.Apply(); // Lag.
}
}
Anyone have any suggestions on how to fix this?
Answer by Jamora · Jul 12, 2013 at 06:18 PM
Calling Apply() on a texture will apply all previous SetPixel and SetPixels changes. What you need to do in order to change the background picture is just _style.normal.background = newBackgroundTexture. You can use them on any GUI or GUILayout components, by passing the name of the style as a string parameter. Refer to the API for more info. The changes take effect instantly.
Instead of changing a GUIStyle from code, I would instead create a new GUIStyle, and apply that when appropriate. In case you have a lot of GUIStyles, I hope you have read the documentation on GUISkins.
Thanks, it works!
I really thought by setting one script (named GUIComponent) = to another script of the same type, it would apply all of the values.. Every other value is applied but for some reason you need to implicitly set the Style variable.
Answer by tw1st3d · Jul 12, 2013 at 05:19 PM
I did something like this:
using System.Collections;
using UnityEngine;
class MyInventory
{
int maxInventorySpace = 0; // Set
public GUISkin InvSkin;
ItemType[] inv = new ItemType[maxInventorySpace];
void OnGUI()
{
GUI.skin = InvSkin;
int x = y = 0;
int maxX; //Set
int boxHeight; //Set
int width, height; //Set
foreach(ItemType i in inv)
{
if(x >= maxX)
{
x = 0;
y = y + boxHeight;
}
GUI.Box(new Rect(x, y, width, height), i, "InvBox");
}
}
}
Pretty much just re-create your box over and over with a specific texture. Then you can add in conditionals and set the box style to another name.
Thanks for posting the code, but this isn't really an answer to my question.
How do you make it so that the changes applied to a style variable take effect? Do you have to use some sort of texture.Apply() derivative?