Hi there, I am at a point, where I don't understand, what Unity is doing with my script. I have written an own GUI class, there are also classes for menus and buttons. In buildGUI() I want to copy all buttons of all menus into one big texture. SO far, so good. But I have to take care of the status of the buttons. If they are active, they have another texture. So every button has its own getCombinedTexture()-methode, which should deliver the correct active or inactive texture of the button. Befor calling getCombinedTexture() in buildGUI(), I print the isActive-member for controlling the procedure, because the button looks the same, wether it is active, or not. In addition I wantet to control via print(), what the getCombinedTexture()-methode is doing, but Unity won't show me anything in the console. You can try it with the attached package. Here is the script for a first view: Code (csharp): #pragma downcast var GUIStyle : GUIStyle; private var GUITexture : Texture2D; var btnWidth : int = 64; // Breite eines Buttons var btnHeight : int = 64; // Höhe eines Buttons var btnDistance : int = 3; // Abstand zwischen Buttons private var screenWidth : int; private var screenHeight : int; var btnNormal : Texture2D; var btnActive : Texture2D; var settingsBtnNormal : Texture2D; var settingsBtnActive : Texture2D; function Awake() { screenWidth = Screen.width; screenHeight = Screen.height; // Definition von Menüs und Buttons ///////////////////////////// // Hauptmenü var mainMenu : GUIMenu = new GUIMenu(); mainMenu.id = "mainMenu"; mainMenu.setPosition(0, 0, screenWidth, btnWidth+2*btnDistance); addMenu(mainMenu); // Button für Einstellungen var settingsBtn : GUIButton = new GUIButton(); settingsBtn.id = "settingsBtn"; settingsBtn.setPosition(3, 3, btnWidth, btnHeight); settingsBtn.setTextures(btnNormal, btnActive, settingsBtnNormal, settingsBtnActive); mainMenu.addButton(settingsBtn); // GUI zusammenbauen GUITexture = buildGUI(); } function Update() { if(Input.GetMouseButtonDown(0)) { var btnPressed = getPressedButton(Input.mousePosition); if(btnPressed) { if(btnPressed.isActive) { btnPressed.isActive = false; } else { btnPressed.isActive = true; } buildGUI(); } } } function OnGUI() { GUI.Box(Rect(0, 0, screenWidth, screenHeight), GUITexture, GUIStyle); } private var listOfMenus = new Array(); // die Liste mit allen Menüs des GUI // Methode, um Menüs zum GUI hinzuzufügen function addMenu(menu : GUIMenu) { listOfMenus.push(menu); } // Methode, um die ID eines Buttons zu erhalten, der eventuell gedrückt wurde function getPressedButton(mousePointer : Vector2) { mousePointer = new Vector2(mousePointer.x, screenHeight-mousePointer.y); // von ScreenSpace zu PixelSpace umrechnen var menuArray : GUIMenu[]; menuArray = listOfMenus.ToBuiltin(GUIMenu); for(var menu : GUIMenu in menuArray) { if(menu.isMouseOverObject(mousePointer)) { var buttonArray = menu.getListOfButtons(); for(var btn : GUIButton in buttonArray) { if(btn.isMouseOverObject(mousePointer)) { return btn; } } } } return false; } // Methode. die das manuell definierte GUI in eine einzige Textur zusammenbaut function buildGUI() { var texture = new Texture2D(screenWidth, screenHeight, TextureFormat.ARGB32, false); var menuArray : GUIMenu[]; menuArray = listOfMenus.ToBuiltin(GUIMenu); // für alle Menüs ... for(var menu : GUIMenu in menuArray) { var buttonArray = menu.getListOfButtons(); // für alle Buttons im jeweiligen Menü ... for(var btn : GUIButton in buttonArray) { // hole kombinierte Textur des Buttons und kopiere sie in die eine GUITextur print("isActive: "+btn.isActive); print("rufe getCombinedTexture() auf"); var btnTexture = btn.getCombinedTexture(); for(var x=0; x<btn.size.x; x++) { for(var y=0; y<btn.size.y; y++) { texture.SetPixel(x+btn.position.x, screenHeight-btn.size.y-btn.position.y+y, btnTexture.GetPixel(x,y)); } } } } texture.Apply(); //texture.Compress(true); return texture; } ///////////////////////////////////////////////////////////////////////////////////// // Klassen der einzelnen GUI Elemente ///////////////////////////////////////////////////////////////////////////////////// // allgemeine Klasse für GUI Elemente class GUIElement { var position : Vector2; var size : Vector2; var id : String; // Methode, um die Ausmaße des Elements festzulegen function setPosition(posX : int, posY : int, sizeX : int, sizeY : int) { position = new Vector2(posX, posY); size = new Vector2(sizeX, sizeY); } // Methode, die prüft, ob die Maus über dem Element liegt function isMouseOverObject(pointer : Vector2) { if(pointer.x>position.x pointer.x<position.x+size.x pointer.y>position.y pointer.y<position.y+size.y) { return true; } return false; } } // Klasse für Menüs, die Buttons und so enthalten class GUIMenu extends GUIElement { private var listOfButtons = new Array(); // Liste mit allen Buttons eines Menüs private var background : Texture2D; // Aussehen des Menüs // Methode, um dem Menü einen Button hinzuzufügen function addButton(btn : GUIButton) { listOfButtons.push(btn); } // Methode, um die Texturen fürs Aussehen festzulegen function setTextures(bg : Texture2D) { background = bg; } // Methode, die die Liste mit zugehörigen Buttons liefert function getListOfButtons() { return listOfButtons; } } // Klasse für Buttons, die innerhalb von Menüs sind class GUIButton extends GUIElement { private var normalLook : Texture2D; // Aussehen des Buttons, wenn er nicht gedrückt ist private var activeLook : Texture2D; // Aussehen des Buttons, wenn er gedrückt ist private var normalIcon : Texture2D; // Aussehen des Icons, wenn es nicht gedrückt ist private var activeIcon : Texture2D; // Aussehen des Icons, wenn es gedrückt ist var isActive : boolean = false; // Flag, ob Button gerade aktiv ist // Methode, um die Texturen fürs Aussehen festzulegen function setTextures(nL : Texture2D, aL : Texture2D, nI : Texture2D, aI : Texture2D) { normalLook = nL; activeLook = aL; normalIcon = nI; activeIcon = aI; } // Methode, die eine kombinierte Textur aus ButtonTextur und IconTextur erstellt function getCombinedTexture() { var btnTexture = new Texture2D(size.x, size.y); var icon : Texture2D; if(!isActive) { btnTexture.SetPixels(normalLook.GetPixels()); icon = normalIcon; }else { btnTexture.SetPixels(activeLook.GetPixels()); icon = activeIcon; } for(var x=0; x<size.x; x++) { for(var y=0; y<size.y; y++) { var btnColor : Color = btnTexture.GetPixel(x, y); var iconColor : Color = icon.GetPixel(x, y); btnColor = new Color(iconColor.a*iconColor.r+(1-iconColor.a)*btnColor.r, iconColor.a*iconColor.g+(1-iconColor.a)*btnColor.g, iconColor.a*iconColor.b+(1-iconColor.a)*btnColor.b, iconColor.a+(1-iconColor.a)*btnColor.a ); btnTexture.SetPixel(x, y, btnColor); } } btnTexture.Apply(); return btnTexture; } }
Thanks for your answer. Could you please insert a print(isActive) somewhere in the function named getCombinedTexture() ? Because there I don't get any output and this is the function which does not do the things I expect.