Search Unity

OnGUI hover button in built mode

Discussion in 'Scripting' started by Gogin, Sep 1, 2018.

  1. Gogin

    Gogin

    Joined:
    Nov 9, 2013
    Posts:
    60
    Hey, have anyone faced similar issue with function OnGUI, please see below:

    I know that it's obsolete function but still I am not getting why its happening.

    for example, OnGUI I use following simple code:

    Code (CSharp):
    1.     private void InitStyles()
    2.     {
    3.         if (currentStyle == null)
    4.         {
    5.             currentStyle = new GUIStyle(GUI.skin.box);
    6.             currentStyle.font = font;
    7.             currentStyle.fontSize = 25;
    8.             currentStyle.alignment = TextAnchor.MiddleCenter;
    9.             currentStyle.hover.textColor = Color.red; //this line does not work in built mode
    10.  
    11.             currentStyle.normal.background = MakeTex(1, 1, defaultColor); //this line does not work in built mode
    12.             currentStyle.hover.background = MakeTex(1, 1, hoverColor); //this line does not work in built mode
    13.  
    14.         }
    15.     }
    Code (CSharp):
    1.     void OnGUI()
    2.     {
    3.         //If its not belong to actual player, return
    4.         if (!isLocalPlayer)
    5.         {
    6.             return;
    7.         }
    8.         //end
    9.  
    10.         GUI.Label(new Rect(30, Screen.height - 380, 30, 30), Time.time.ToString(), blackText);
    11.  
    12.         InitStyles();
    13.  
    14.         //Show properties!
    15.         if (GetSelectedObjects()[randomlyGeneratedRaceId] != null) //lookObj != null
    16.         {
    17.  
    18.             // Mob mob = lookObj.GetComponent<Mob>();
    19.             Mob mob = GetSelectedObjects()[randomlyGeneratedRaceId].GetComponent<Mob>();
    20.  
    21.             _mobProperties = "Name:  " + mob.mobName + " [" + convertorType(mob) + "]\n" +
    22.                          "Cost: <b>" + mob.cost + "</b> silver \n" +
    23.                          "Population: " + mob.population + "\n" +
    24.                          "Attack type: " + convertorRange(mob) + " [" + mob.attackRange + " m] \n" +
    25.                          "Hitpoints: " + mob.hp + " [Regen: " + mob.hitPointsRegen + " hp/s]\n" +
    26.                          "Attack speed: " + mob.attackSpeed + " hit/s \n" +
    27.                          "Damage: " + mob.damage + "[" + mob.damageType + "]\n" +
    28.                          "Armor: " + mob.armor + "[" + mob.armorType + "]\n" +
    29.                          "Movement speed: " + mob.movementSpeed + " m/s";
    30.         }
    31.  
    32.         Rect buildingBtnRect1 = new Rect(0, Screen.height - 70, 70, 70);
    33.         Rect buildingBtnRect2 = new Rect(71, Screen.height - 70, 70, 70);
    34.         Rect buildingBtnRect3 = new Rect(142, Screen.height - 70, 70, 70);
    35.         Rect buildingBtnRect4 = new Rect(213, Screen.height - 70, 70, 70);
    36.  
    37.         //Click on button (show lookobj)
    38.         if (GUI.Button(buildingBtnRect1, new GUIContent("1", _mobProperties), currentStyle))
    39.             SetId(0, true);
    40.         else if (GUI.Button(buildingBtnRect2, new GUIContent("2", _mobProperties), currentStyle))
    41.             SetId(1, true);
    42.         else if (GUI.Button(buildingBtnRect3, new GUIContent("3", _mobProperties), currentStyle))
    43.             SetId(2, true);
    44.         else if (GUI.Button(buildingBtnRect4, new GUIContent("4", _mobProperties), currentStyle))
    45.             SetId(3, true);
    46.  
    47.         //[Hover]
    48.         if (buildingBtnRect1.Contains(Event.current.mousePosition)) //Event.current.mousePosition //Input.mousePosition
    49.             SetId(0, false);
    50.         else if (buildingBtnRect2.Contains(Event.current.mousePosition))
    51.             SetId(1, false);
    52.         else if (buildingBtnRect3.Contains(Event.current.mousePosition))
    53.             SetId(2, false);
    54.         else if (buildingBtnRect4.Contains(Event.current.mousePosition))
    55.             SetId(3, false);
    56.  
    57.         //[Hover tooltip]
    58.         if (lookObj == null) //Structure is already showen, hide tooltip
    59.             GUI.Label(new Rect(30, Screen.height - 280, 30, 30), GUI.tooltip, blackText);
    60.     }
    In unity editor everything works fine (in first video) and in built edition its not working properly (seems like these functions are called just when I move camera).

    Working in unity editor:


    Not working in built version:
     
unityunity