Search Unity

no print ouput

Discussion in 'Scripting' started by vollnull, Dec 3, 2010.

  1. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    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):
    1.  
    2. #pragma downcast
    3.  
    4. var GUIStyle : GUIStyle;
    5. private var GUITexture : Texture2D;
    6. var btnWidth : int = 64; // Breite eines Buttons
    7. var btnHeight : int = 64; // Höhe eines Buttons
    8. var btnDistance : int = 3; // Abstand zwischen Buttons
    9.  
    10. private var screenWidth : int;
    11. private var screenHeight : int;
    12.  
    13. var btnNormal : Texture2D;
    14. var btnActive : Texture2D;
    15. var settingsBtnNormal : Texture2D;
    16. var settingsBtnActive : Texture2D;
    17.  
    18. function Awake() {
    19.     screenWidth = Screen.width;
    20.     screenHeight = Screen.height;
    21.     // Definition von Menüs und Buttons
    22.     /////////////////////////////
    23.     // Hauptmenü
    24.     var mainMenu : GUIMenu = new GUIMenu();
    25.     mainMenu.id = "mainMenu";
    26.     mainMenu.setPosition(0, 0, screenWidth, btnWidth+2*btnDistance);
    27.     addMenu(mainMenu);
    28.     // Button für Einstellungen
    29.     var settingsBtn : GUIButton = new GUIButton();
    30.     settingsBtn.id = "settingsBtn";
    31.     settingsBtn.setPosition(3, 3, btnWidth, btnHeight);
    32.     settingsBtn.setTextures(btnNormal, btnActive, settingsBtnNormal, settingsBtnActive);
    33.     mainMenu.addButton(settingsBtn);
    34.     // GUI zusammenbauen
    35.     GUITexture = buildGUI();
    36. }
    37.  
    38. function Update() {
    39.     if(Input.GetMouseButtonDown(0)) {
    40.         var btnPressed = getPressedButton(Input.mousePosition);
    41.         if(btnPressed) {
    42.             if(btnPressed.isActive) {
    43.                 btnPressed.isActive = false;
    44.             } else {
    45.                 btnPressed.isActive = true;
    46.             }
    47.             buildGUI();
    48.         }
    49.     }
    50. }
    51.  
    52. function OnGUI() {
    53.     GUI.Box(Rect(0, 0, screenWidth, screenHeight), GUITexture, GUIStyle);
    54. }
    55.  
    56. private var listOfMenus = new Array(); // die Liste mit allen Menüs des GUI
    57. // Methode, um Menüs zum GUI hinzuzufügen
    58. function addMenu(menu : GUIMenu) {
    59.     listOfMenus.push(menu);
    60. }
    61.  
    62. // Methode, um die ID eines Buttons zu erhalten, der eventuell gedrückt wurde
    63. function getPressedButton(mousePointer : Vector2) {
    64.     mousePointer = new Vector2(mousePointer.x, screenHeight-mousePointer.y); // von ScreenSpace zu PixelSpace umrechnen
    65.     var menuArray : GUIMenu[];
    66.     menuArray = listOfMenus.ToBuiltin(GUIMenu);
    67.     for(var menu : GUIMenu in menuArray) {
    68.         if(menu.isMouseOverObject(mousePointer)) {
    69.             var buttonArray = menu.getListOfButtons();
    70.             for(var btn : GUIButton in buttonArray) {
    71.                 if(btn.isMouseOverObject(mousePointer)) {
    72.                     return btn;
    73.                 }
    74.             }
    75.         }
    76.     }
    77.     return false;
    78. }
    79.  
    80. // Methode. die das manuell definierte GUI in eine einzige Textur zusammenbaut
    81. function buildGUI() {
    82.     var texture = new Texture2D(screenWidth, screenHeight, TextureFormat.ARGB32, false);
    83.     var menuArray : GUIMenu[];
    84.     menuArray = listOfMenus.ToBuiltin(GUIMenu);
    85.     // für alle Menüs ...
    86.     for(var menu : GUIMenu in menuArray) {
    87.         var buttonArray = menu.getListOfButtons();
    88.         // für alle Buttons im jeweiligen Menü ...
    89.         for(var btn : GUIButton in buttonArray) {
    90.             // hole kombinierte Textur des Buttons und kopiere sie in die eine GUITextur
    91.             print("isActive: "+btn.isActive);
    92.             print("rufe getCombinedTexture() auf");
    93.             var btnTexture = btn.getCombinedTexture();
    94.             for(var x=0; x<btn.size.x; x++) {
    95.                 for(var y=0; y<btn.size.y; y++) {
    96.                     texture.SetPixel(x+btn.position.x, screenHeight-btn.size.y-btn.position.y+y, btnTexture.GetPixel(x,y));
    97.                 }
    98.             }
    99.         }
    100.     }
    101.     texture.Apply();
    102.     //texture.Compress(true);
    103.     return texture;
    104. }
    105.  
    106. /////////////////////////////////////////////////////////////////////////////////////
    107. // Klassen der einzelnen GUI Elemente
    108. /////////////////////////////////////////////////////////////////////////////////////
    109.  
    110. // allgemeine Klasse für GUI Elemente
    111. class GUIElement {
    112.     var position : Vector2;
    113.     var size : Vector2;
    114.     var id : String;
    115.     // Methode, um die Ausmaße des Elements festzulegen
    116.     function setPosition(posX : int, posY : int, sizeX : int, sizeY : int) {
    117.         position = new Vector2(posX, posY);
    118.         size = new Vector2(sizeX, sizeY);
    119.     }
    120.     // Methode, die prüft, ob die Maus über dem Element liegt
    121.     function isMouseOverObject(pointer : Vector2) {
    122.         if(pointer.x>position.x  pointer.x<position.x+size.x  pointer.y>position.y  pointer.y<position.y+size.y) {
    123.             return true;
    124.         }
    125.         return false;
    126.     }
    127. }
    128.  
    129.  
    130. // Klasse für Menüs, die Buttons und so enthalten
    131. class GUIMenu extends GUIElement {
    132.     private var listOfButtons = new Array(); // Liste mit allen Buttons eines Menüs
    133.     private var background : Texture2D; // Aussehen des Menüs
    134.     // Methode, um dem Menü einen Button hinzuzufügen
    135.     function addButton(btn : GUIButton) {
    136.         listOfButtons.push(btn);
    137.     }
    138.     // Methode, um die Texturen fürs Aussehen festzulegen
    139.     function setTextures(bg : Texture2D) {
    140.         background = bg;
    141.     }
    142.     // Methode, die die Liste mit zugehörigen Buttons liefert
    143.     function getListOfButtons() {
    144.         return listOfButtons;
    145.     }
    146. }
    147.  
    148.  
    149. // Klasse für Buttons, die innerhalb von Menüs sind
    150. class GUIButton extends GUIElement {
    151.     private var normalLook : Texture2D; // Aussehen des Buttons, wenn er nicht gedrückt ist
    152.     private var activeLook : Texture2D; // Aussehen des Buttons, wenn er gedrückt ist
    153.     private var normalIcon : Texture2D; // Aussehen des Icons, wenn es nicht gedrückt ist
    154.     private var activeIcon : Texture2D; // Aussehen des Icons, wenn es gedrückt ist
    155.     var isActive : boolean = false; // Flag, ob Button gerade aktiv ist
    156.     // Methode, um die Texturen fürs Aussehen festzulegen
    157.     function setTextures(nL : Texture2D, aL : Texture2D, nI : Texture2D, aI : Texture2D) {
    158.         normalLook = nL;
    159.         activeLook = aL;
    160.         normalIcon = nI;
    161.         activeIcon = aI;
    162.     }
    163.     // Methode, die eine kombinierte Textur aus ButtonTextur und IconTextur erstellt
    164.     function getCombinedTexture() {
    165.         var btnTexture = new Texture2D(size.x, size.y);
    166.         var icon : Texture2D;
    167.         if(!isActive) {
    168.             btnTexture.SetPixels(normalLook.GetPixels());
    169.             icon = normalIcon;
    170.         }else {
    171.             btnTexture.SetPixels(activeLook.GetPixels());
    172.             icon = activeIcon;
    173.         }
    174.         for(var x=0; x<size.x; x++) {
    175.             for(var y=0; y<size.y; y++) {
    176.                 var btnColor : Color = btnTexture.GetPixel(x, y);
    177.                 var iconColor : Color = icon.GetPixel(x, y);
    178.                 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 );
    179.                 btnTexture.SetPixel(x, y, btnColor);
    180.             }
    181.         }
    182.         btnTexture.Apply();
    183.         return btnTexture;
    184.     }
    185. }
    186.  
     

    Attached Files:

    Last edited: Dec 6, 2010
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I copypasted that script, and it does print to the console.

    --Eric
     
  3. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use Debug.Log instead; I think print only works in Monobehaviour.

    --Eric
     
  5. vollnull

    vollnull

    Joined:
    Aug 26, 2010
    Posts:
    70
    Oh, thanks. This was the problem.