Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Prime31 UIToolkit Multi Resolution GUI Solution Ready for Use...and it's free

Discussion in 'iOS and tvOS' started by prime31, May 3, 2011.

  1. guto-thomas

    guto-thomas

    Joined:
    Mar 12, 2012
    Posts:
    25
    Hey guys!
    What's the best way to scale a UIJoystick element? Because every time I try to do this, my hitArea get silly.
    Any idea/solution?

    Thanks!
     
  2. toto2003

    toto2003

    Joined:
    Sep 22, 2010
    Posts:
    528
    wow that s awesome, will have a look.
    thanks for this
     
  3. Casio

    Casio

    Joined:
    Jul 16, 2012
    Posts:
    28
    @Trooper.

    OMG you know what I am talking about! Thanks. I tried to implement your code into my version of toolkit but I couldn't seem to get it to work due to missing things for OrderByDescending. I am not a tri guy so I wouldnt have done this myself. Thanks and if I can get this working I can finally get this project released - this is my last major bug. Definitely this should be added into the toolkit as it really is essential for zIndex to actually be true.

    @Prime
    Any thoughts on smashing this bug and implementing this sort so zIndex is true on all platforms?
     
  4. Casio

    Casio

    Joined:
    Jul 16, 2012
    Posts:
    28
    hoping that they implement something in the uitoolkit to sort the order of rendered sprites so their zindex truly is reflective in their draw order thus correcting this and other similar problems.
     
  5. Casio

    Casio

    Joined:
    Jul 16, 2012
    Posts:
    28
    OMG you know what I am talking about! Thanks. I tried to implement your code into my version of toolkit but I couldn't seem to get it to work due to missing things for OrderByDescending. I am not a tri guy so I wouldnt have done this myself. Thanks and if I can get this working I can finally get this project released - this is my last major bug. Definitely this should be added into the toolkit as it really is essential for zIndex to actually be true.
     
  6. Ben-BearFish

    Ben-BearFish

    Joined:
    Sep 6, 2011
    Posts:
    1,204
    I'm assuming trianglesChanged is equivalent to vertsChanged in this code?
     
  7. shrey-malhotra

    shrey-malhotra

    Joined:
    Jan 27, 2013
    Posts:
    11
    I am making a GUI for my android game project on Unity using the UIToolkit.
    The Sample Scenes (eg Kitchen Sink) works fine on the Unity Editor.
    But when I run the exact same sample scenes on the android device , it doesnt work.
    The Button Texture is slightly shifted from the actual Touch Region.
    I have tried different Android Devices, but the result is the same.

    I tried accessing the Sprite Locations using a function like

    But it the code returns an no Value!

    I even tried using the "IPositionablePositioningExtensions" and returning the Sprite location. But no Success. Although I am not sure how to use the 'IPositionablePositioningExtensions', I have just started a beginner in this field.

    My Button Creation Looks something like this :

    Here is a screenshot of how the sample scene looks on an android device.
    The Bounding box is the region where the Touch Works, while the Button texture is being rendered somewhere else.

    $Screenshot_2013-03-10-04-52-56.png

    http://i.stack.imgur.com/D6Wfy.png


    SOLUTION :

    Thanks to @ncst
    Found the solution.

    In UI.cs
    and

    Call the above method once the scene has been loaded

     
    Last edited: Mar 10, 2013
  8. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    Yes, i just added it in to match the logic with vertsChanged, so in the updateMeshProperties there is a bit that changes the triangles on the mesh and sets trianglesChanged to false just like vertsChanged.
     
  9. mastermax001

    mastermax001

    Joined:
    Aug 9, 2012
    Posts:
    9
    Hello everyone, im experiencing a weird bug with the Vertical Scroller (mainly in iOS, but even in the Editor): when a vertical scroller is still "moving" (so when it'snt totally stop) it's impossible hide this scroller.

    I dont have checked the code itself, but i want to know if anyone have experienced this problem and (maybe) understand how this people have resolved this :)

    Another question that i'd like to ask is if im right when i hide/show elements for navigate through different page of the menu (so, basically, when im in the "home/first page" and i want to go in the "shop" page, i hide all the element of the "home" and show the element of the "shop"). This is the right way to work or someone have a better suggestion (except the scroller this solution seems works pretty fine for me and for the profiler :) ).

    Thanks
     
  10. Pauls

    Pauls

    Joined:
    Nov 12, 2012
    Posts:
    46
    Hi,

    I added a button above a rectangle, the button is a plain circle with a thin circle around, so there is an empty space that let us see the rectangle behind.

    It works fine in Unity, but then on mobile, there is an "alpha" rectangle around the button that hide my rectangle behind, such as if there wasn't anything behind the button. (It has the color of the background) So if i move the button right or left, we won't see my rectangle behind, because there is this weird alpha rectangle that appears right around the bounds of the button.

    Would you know what could cause this?

    Thanks a lot
     
  11. laserlars

    laserlars

    Joined:
    Nov 17, 2011
    Posts:
    255
    Having some issues with UIButton. I´m placing them outside of cameraview on startup, then reposition them to screen center when round is over.
    Some of the buttons seem to lose their "touch area" after doing this, while others work like normal. Almost feels like a lottery :/

    Any ideas on how to fix this ?
     
  12. Nels83

    Nels83

    Joined:
    Oct 27, 2012
    Posts:
    20
    I have run into the same issue....just started happening one day.
    I noticed that it mainly happens when I have more than one 3d View showing.
    Also if you have the show touch fields set to true in the view port, it tends to work.
     
  13. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    Hello,
    And sorry if this error has been fixed but I can't seem to find it.
    I've just started using UIToolkit today and followed some basic setup steps from the intro video and on github document. But when I tried to run it there was an error thrown in the console:
    Code (csharp):
    1. InvalidCastException: Cannot cast from source type to destination type.
    2. UISpriteManager.loadTexturesFromTexturePackerJSON (System.String filename, Vector2 textureSize) (at Assets/Plugins/UIToolkit/BaseElements/UISpriteManager.cs:122)
    3. UISpriteManager.loadTextureAndPrepareForUse () (at Assets/Plugins/UIToolkit/BaseElements/UISpriteManager.cs:108)
    4. UI.Awake () (at Assets/Plugins/UIToolkit/UI.cs:117)
    .
    Has anyone had the same problem? If you've solved this please show me how :( Thanks in advance!
     
  14. laserlars

    laserlars

    Joined:
    Nov 17, 2011
    Posts:
    255
    @Brastik: At first glance, it looks like uitoolkit can't find your textureatlas. Remember to put your files in the Resources folder.
     
  15. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    Yes, I think I've created new folder named Resources and put all of my texture files and .txt files there.
    As it stated "InvalidCastException" at "UISpriteManager.cs:122" I would suspect this line
    Code (csharp):
    1. var frames = (IDictionary)decodedHash["frames"];
    but since its not listed as a common bug then I have no idea how'd this happen :(
     
  16. amzg-maki

    amzg-maki

    Joined:
    Mar 12, 2013
    Posts:
    12
    Ok I found out what caused the error now. My bad > < When export the json file from TexturePacker I choose JSON(Array) not JSON(Hash), that's why...
     
  17. chubz

    chubz

    Joined:
    Mar 14, 2013
    Posts:
    1
    Hello, I'm having an issue with setting up an About screen for my mini game.
    I've set up a scrollable vertical layout with 1 element and I have a button as a header, I've positioned the layout below my header button.
    I'm using multiple atlases and I've put a wrappedText instance on the screen.
    When I try to say scrollable.addChild(myText) this yields an error:
    No appropriate version of 'UIAbstractTouchableContainer.addChild' for the argument list '(UITextInstance)' was found.

    Don't know if this feature is missing, or maybe I'm doing something wrong.

    When I read the setup text and features text on git it said that layouts can add any children.
     
  18. JDUnity

    JDUnity

    Joined:
    Oct 1, 2012
    Posts:
    13
    I can't believe I was in such a trouble with this. UIToolkit is simpler than it seems. I'm sorry for the delay but I was fixing some other things I needed first.

    The images on a scroll list are changed (or at least I solved it) like this:

    The fillScroll() goes inside the scroll on Start()

    Thanks again Prime;

    Code (csharp):
    1. void fillScroll()
    2.     {
    3.         UIButton button;
    4.         for(int i=0; i<10; i++)
    5.             {
    6.                     button = UIButton.create( "marioPanel.png", "emptyDown.png", 0, 0);
    7.                     scrollable.addChild(button);
    8.                     itemButtons.Add(button);
    9.                     button.onTouchUpInside += sender => checkInventory(sender.index);
    10.                     button.setSize(Screen.width/3, Screen.height/3);
    11.             }          
    12.     }
    13.    
    14.    
    15.     public void updateInventoryImages()
    16.     {
    17.         UIButton button;
    18.         for(int i=0; i<10; i++)
    19.         {
    20.                 scrollable.removeChild(itemButtons[i], true);
    21.                 Debug.Log(Main.Inventory[i].name);
    22.         }
    23.         itemButtons.Clear();
    24.         for(int i=0; i<10; i++)
    25.         {
    26.             if(Main.Inventory[i].name=="Empty")
    27.             {
    28.                 button = UIButton.create( "marioPanel.png", "emptyDown.png", 0, 0);
    29.                 scrollable.addChild(button);
    30.                 itemButtons.Add(button);
    31.                 button.onTouchUpInside += sender => checkInventory(sender.index);
    32.                 button.setSize(Screen.width/3, Screen.height/3);
    33.             }
    34.             else
    35.             {
    36.                     button = UIButton.create( "emptyDown.png", "marioPanel.png", 0, 0);
    37.                     scrollable.addChild(button);
    38.                     itemButtons.Add(button);
    39.                     button.onTouchUpInside += sender => checkInventory(sender.index);
    40.                     button.setSize(Screen.width/3, Screen.height/3);
    41.             }
    42.         }  
    43.     }
     
  19. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    UIText UTF-16/32 Support

    Use this if you want to support more than the standard 255 characters (useful for iPhone and the Euro symbol).

    I have added some extra code for character spacing so this code might not 100% work with the original code base, you may need to make very minor alterations.

    Code (csharp):
    1. using UnityEngine;
    2. using System.IO;
    3. using System.Collections.Generic;
    4. using UIEaseType = System.Func<float, float>;
    5. using System.Collections;
    6.  
    7.  
    8.  
    9.  
    10. public enum UITextLineWrapMode
    11. {
    12.     None = 0,
    13.     AlwaysHyphenate,
    14.     MinimumLength
    15. };
    16.  
    17.  
    18. public enum UITextAlignMode
    19. {
    20.     Left = 0,
    21.     Center,
    22.     Right
    23. };
    24.  
    25.  
    26. public enum UITextVerticalAlignMode
    27. {
    28.     Top = 0,
    29.     Middle,
    30.     Bottom
    31. }
    32.  
    33.  
    34.  
    35.  
    36.  
    37.  
    38.  
    39.  
    40. public class UIText : System.Object
    41. {
    42.     public static int ASCII_NEWLINE = 10;
    43.     public static int ASCII_SPACE = 32;
    44.     public static int ASCII_HYPHEN_MINUS = 45;
    45.     // Break out the magic line height number into a static var.
    46.     public static int ASCII_LINEHEIGHT_REFERENCE = 124;
    47.    
    48.     private struct UIFontCharInfo
    49.     {    
    50.         public int charID;
    51.         public int posX;
    52.         public int posY;
    53.         public int w;
    54.         public int h;
    55.         public int offsetx;
    56.         public int offsety;
    57.         public int xadvance;
    58.         public bool initialised;
    59.     }
    60.    
    61.     /// <summary>
    62.     ///  Forces known High-ASCII values (common when pasting text from word) down to low ASCII. Incurs a performance penalty, but might be helpful.
    63.     /// </summary>
    64.     public bool forceLowAscii = false;
    65.     private bool hasLowAsciiQuotes = false;
    66.    
    67.     public float lineSpacing = 1.2f;
    68.    
    69.      private UIFontCharInfo[] _fontDetails;
    70.     private Vector2 _textureOffset;
    71.     private UIToolkit _manager;
    72.     public UIToolkit manager { get { return _manager; } }
    73.    
    74.     public UITextAlignMode alignMode = UITextAlignMode.Left;
    75.     public UITextVerticalAlignMode verticalAlignMode = UITextVerticalAlignMode.Top;
    76.     public UITextLineWrapMode wrapMode = UITextLineWrapMode.None;
    77.     public float lineWrapWidth = 500.0f;
    78.    
    79.  
    80.  
    81.     /// <summary>
    82.     /// Creates a UIText instance which can then be used to create actual text sprites
    83.     /// </summary>
    84.     public UIText( string fontFilename, string textureFilename ) : this( UI.firstToolkit, fontFilename, textureFilename )
    85.     {    
    86.     }
    87.    
    88.    
    89.     public UIText( UIToolkit manager, string fontFilename, string textureFilename )
    90.     {
    91.         _manager = manager;
    92.         _fontDetails = new UIFontCharInfo[maxConfilefileChar(fontFilename)];
    93.        
    94.         loadConfigfile( fontFilename );
    95.        
    96.         // grab the texture offset from the UI
    97.         var rect = _manager.frameForFilename( textureFilename );
    98.        
    99.         // Since the font config data adjusts for padding, but TexturePacker trimming removes it,
    100.         // We need to sub out the trimmed amount coming back from the manager.
    101.         var info = _manager.textureInfoForFilename( textureFilename );
    102.        
    103.         this._textureOffset = new Vector2( rect.x - info.spriteSourceSize.x, rect.y - info.spriteSourceSize.y );
    104.     }
    105.    
    106.     private int maxConfilefileChar(string filename)
    107.     {
    108.        
    109.         // should we load a double resolution font?
    110.         if( UI.isHD )
    111.             filename = filename + UI.instance.hdExtension;
    112.    
    113.         var asset = Resources.Load( filename, typeof( TextAsset ) ) as TextAsset;
    114.        
    115.         if( asset == null )
    116.             Debug.LogError( "Could not find font config file in Resources folder: " + filename );
    117.    
    118.         int idNum = 0;
    119.         int maxId = 0;
    120.        
    121.         foreach( var input in asset.text.Split( new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries ) )
    122.         {
    123.             //first split line into "space" chars
    124.                string[] words = input.Split(' ');
    125.             foreach( string word in words )
    126.             {
    127.                 //then split line into "=" sign to get the values for each component
    128.                 string[] wordsSplit = word.Split( '=' );
    129.                 foreach( string word1 in wordsSplit )
    130.                     {
    131.                     if( string.Equals( word1, "id" ) )
    132.                     {    
    133.                        
    134.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    135.                         forceLowAsciiChar( ref tmp );
    136.                         idNum = System.Int32.Parse( tmp );
    137.                        
    138.                         if (idNum > maxId)
    139.                         {
    140.                             maxId = idNum+1;
    141.                            
    142.                         }
    143.                        
    144.                     }
    145.                    
    146.                 }
    147.                
    148.             }
    149.            
    150.         }
    151.        
    152.         return maxId;
    153.        
    154.     }
    155.  
    156.  
    157.    
    158.     /// <summary>
    159.     /// Parse the fnt file with the font definition.  Font files should be in the Resources folder and have a .txt extension.
    160.     /// Do not inluclude the file extension in the filename!
    161.     /// </summary>
    162.     private void loadConfigfile( string filename )
    163.     {
    164.         // should we load a double resolution font?
    165.         if( UI.isHD )
    166.             filename = filename + UI.instance.hdExtension;
    167.    
    168.         var asset = Resources.Load( filename, typeof( TextAsset ) ) as TextAsset;
    169.         if( asset == null )
    170.             Debug.LogError( "Could not find font config file in Resources folder: " + filename );
    171.    
    172.         int idNum = 0;
    173.        
    174.         Debug.Log (filename    );
    175.        
    176.         foreach( var input in asset.text.Split( new string[] { "\r\n" }, System.StringSplitOptions.RemoveEmptyEntries ) )
    177.         {
    178.             //first split line into "space" chars
    179.                string[] words = input.Split(' ');
    180.             foreach( string word in words )
    181.             {
    182.                 //then split line into "=" sign to get the values for each component
    183.                 string[] wordsSplit = word.Split( '=' );
    184.                 foreach( string word1 in wordsSplit )
    185.                     {
    186.                     if( string.Equals( word1, "id" ) )
    187.                     {    
    188.                        
    189.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    190.                         forceLowAsciiChar( ref tmp );
    191.                         idNum = System.Int32.Parse( tmp );
    192.                        
    193.                         if (idNum == 145 || idNum == 146 || idNum == 147 || idNum == 148)
    194.                             hasLowAsciiQuotes = true;
    195.                        
    196.                         _fontDetails[idNum] = new UIFontCharInfo();
    197.                         _fontDetails[idNum].initialised = true;
    198.                        
    199.                         _fontDetails[idNum].charID = new int();
    200.                         _fontDetails[idNum].charID = idNum;
    201.                        
    202.                        
    203.                     }
    204.                     else if( string.Equals( word1, "x" ) )
    205.                     {
    206.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    207.                         forceLowAsciiChar( ref tmp );
    208.                         _fontDetails[idNum].posX = new int();
    209.                         _fontDetails[idNum].posX = System.Int32.Parse( tmp );
    210.                     }
    211.                     else if( string.Equals( word1, "y" ) )
    212.                     {
    213.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    214.                         forceLowAsciiChar( ref tmp );
    215.                         _fontDetails[idNum].posY = new int();
    216.                         _fontDetails[idNum].posY = System.Int32.Parse( tmp );
    217.                     }
    218.                     else if( string.Equals( word1, "width" ) )
    219.                     {
    220.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    221.                         forceLowAsciiChar( ref tmp );
    222.                         _fontDetails[idNum].w = new int();
    223.                         _fontDetails[idNum].w = System.Int32.Parse( tmp );
    224.                     }
    225.                     else if( string.Equals( word1, "height" ) )
    226.                     {
    227.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    228.                         forceLowAsciiChar( ref tmp );
    229.                         _fontDetails[idNum].h = new int();
    230.                         _fontDetails[idNum].h = System.Int32.Parse( tmp );
    231.                     }
    232.                     else if( string.Equals( word1, "xoffset" ) )
    233.                     {
    234.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    235.                         forceLowAsciiChar( ref tmp );
    236.                         _fontDetails[idNum].offsetx = new int();
    237.                         _fontDetails[idNum].offsetx = System.Int32.Parse(tmp);
    238.                     }
    239.                     else if( string.Equals( word1, "yoffset" ) )
    240.                     {
    241.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    242.                         forceLowAsciiChar( ref tmp );
    243.                         _fontDetails[idNum].offsety = new int();
    244.                         _fontDetails[idNum].offsety = System.Int32.Parse( tmp );
    245.                     }
    246.                     else if( string.Equals( word1, "xadvance" ) )
    247.                     {
    248.                         string tmp = wordsSplit[1].Substring( 0, wordsSplit[1].Length );
    249.                         forceLowAsciiChar( ref tmp );
    250.                         _fontDetails[idNum].xadvance = new int();
    251.                         _fontDetails[idNum].xadvance = System.Int32.Parse( tmp );
    252.                     }
    253.                 }  // end foreach
    254.             }  // end foreach
    255.         }  // end while
    256.     }
    257.    
    258.     private void drawText( UITextInstance textInstance, float xPos, float yPos, float scale, int depth, Color[] color, UITextAlignMode instanceAlignMode, UITextVerticalAlignMode instanceVerticalAlignMode )
    259.     {
    260.        
    261.         drawText(textInstance,xPos,yPos,scale,depth,0,color,instanceAlignMode,instanceVerticalAlignMode);
    262.        
    263.     }
    264.    
    265.     /// <summary>
    266.     /// Draw text on screen, create each quad and send it to the manager
    267.     /// </summary>
    268.     private void drawText( UITextInstance textInstance, float xPos, float yPos, float scale, int depth, int characterSpacing, Color[] color, UITextAlignMode instanceAlignMode, UITextVerticalAlignMode instanceVerticalAlignMode )
    269.     {
    270.         // Start by resetting textInstance position
    271.         textInstance.position = Vector3.zero;
    272.         bool hidden = textInstance.hidden;
    273.         float dx = 0;
    274.         float dy = 0;
    275.         float offsetY;
    276.         int fontLineSkip = 0;
    277.         int charId = 0;
    278.        
    279.  
    280.  
    281.         // Perform word wrapping ahead of sprite allocation!
    282.         var text = textInstance.text;
    283.         text = wrapText( text, scale );
    284.        
    285.         int lineStartChar = 0;
    286.         int lineEndChar = 0;
    287.        
    288.         float totalHeight = ( _fontDetails[ASCII_LINEHEIGHT_REFERENCE].h * scale * lineSpacing );
    289.        
    290.         for( var i = 0; i < text.Length; i++ )
    291.         {
    292.        
    293.             charId = System.Convert.ToInt32( text[i] );
    294.        
    295.             if (_fontDetails[charId].initialised)
    296.             {
    297.                                
    298.                 if( charId == ASCII_NEWLINE )
    299.                 {
    300.                     // calculate the size to center text on Y axis, based on its scale
    301.                     // 77 is the "M" char usually big enough to get a proper spaced
    302.                     // lineskip, use any other char if you want
    303.                     fontLineSkip += (int)( _fontDetails[ASCII_LINEHEIGHT_REFERENCE].h * scale * lineSpacing );
    304.                     totalHeight += (int)( _fontDetails[ASCII_LINEHEIGHT_REFERENCE].h * scale * lineSpacing );
    305.                    
    306.                     alignLine( textInstance.textSprites, lineStartChar, lineEndChar, dx, instanceAlignMode );
    307.                    
    308.                     lineStartChar = i + 1;
    309.                    
    310.                     dx = 0;
    311.                 }
    312.                 else
    313.                 {
    314.                     // calculate the size to center text on Y axis, based on its scale
    315.                     offsetY = _fontDetails[charId].offsety * scale;
    316.                     dy = offsetY + fontLineSkip;
    317.                 }
    318.                
    319.                 // Extend end of line
    320.                 lineEndChar = i;
    321.    
    322.                 // add quads for each char
    323.                 // Use curpos instead of i to compensate for line wrapping hyphenation
    324.                 // reuse a UISprite if we have one. if we don't, we need to set it's parent and add it to the textInstance's list
    325.                 var currentTextSprite = textInstance.textSpriteAtIndex( i );
    326.                 var addingNewTextSprite = currentTextSprite == null;
    327.                
    328.                 currentTextSprite = configureSpriteForCharId( currentTextSprite, charId, dx, dy, scale, 0 );
    329.                
    330.                 if( addingNewTextSprite )
    331.                 {
    332.                     currentTextSprite.color = color.Length == 1 ? color[0] : color[i];
    333.                     currentTextSprite.parentUIObject = textInstance;
    334.                     textInstance.textSprites.Add( currentTextSprite );
    335.                 }
    336.    
    337.                 // Ensure the sprite is hidden if the textInstance is
    338.                 currentTextSprite.hidden = hidden;
    339.                
    340.                 // See below @NOTE re: offsetx vs. xadvance bugfix.
    341.                 // advance the position to draw the next letter
    342.                 dx += _fontDetails[charId].xadvance * scale + characterSpacing;
    343.                
    344.             }
    345.    
    346.         }
    347.        
    348.         alignLine( textInstance.textSprites, lineStartChar, lineEndChar, dx, instanceAlignMode );
    349.         verticalAlignText( textInstance.textSprites, totalHeight, _fontDetails[ASCII_LINEHEIGHT_REFERENCE].offsety * scale * lineSpacing, instanceVerticalAlignMode );
    350.  
    351.  
    352.         // Re-position textInstance
    353.         textInstance.position = new Vector3(xPos, yPos, depth);
    354.     }
    355.    
    356.    
    357.     /// <summary>
    358.     /// Performs horizontal alignment of each line independently.
    359.     /// </summary>
    360.     private void alignLine( List<UISprite> sprites, int lineStartChar, int lineEndChar, float lineWidth, UITextAlignMode instanceAlignMode )
    361.     {
    362.         if( instanceAlignMode == UITextAlignMode.Left )
    363.             return;
    364.        
    365.        
    366.         if( instanceAlignMode == UITextAlignMode.Center )
    367.         {
    368.             // Go from start character to end character, INCLUSIVE.
    369.             for ( var i = lineStartChar; i <= lineEndChar; i++ )
    370.             {
    371.                 if( sprites[i] != null )
    372.                     sprites[i].position = new Vector3( sprites[i].position.x - lineWidth / 2.0f, sprites[i].position.y, sprites[i].position.z );
    373.             }
    374.         }  
    375.         else if( instanceAlignMode == UITextAlignMode.Right )
    376.         {
    377.             // Go from start character to end character, INCLUSIVE.
    378.             for ( var i = lineStartChar; i <= lineEndChar; i++ )
    379.             {
    380.                 if ( i < sprites.Count  sprites[i] != null )
    381.                     sprites[i].position = new Vector3( sprites[i].position.x - lineWidth, sprites[i].position.y, sprites[i].position.z );
    382.             }
    383.            
    384.         }
    385.     }
    386.    
    387.    
    388.     /// <summary>
    389.     /// Performs vertical alignment of entire paragraph to the positioning originally provided.
    390.     /// </summary>
    391.     private void verticalAlignText( List<UISprite> sprites, float totalHeight, float charOffset, UITextVerticalAlignMode instanceVerticalAlignMode )
    392.     {
    393.         if ( instanceVerticalAlignMode == UITextVerticalAlignMode.Top )
    394.             return;
    395.        
    396.        
    397.         var numSprites = sprites.Count;
    398.         for( int i = 0; i < numSprites; i++ )
    399.         {
    400.             if( instanceVerticalAlignMode == UITextVerticalAlignMode.Middle )
    401.                 sprites[i].position = new Vector3( sprites[i].position.x, sprites[i].position.y + totalHeight/2 + charOffset, sprites[i].position.z );
    402.             else if( instanceVerticalAlignMode == UITextVerticalAlignMode.Bottom )
    403.                 sprites[i].position = new Vector3( sprites[i].position.x, sprites[i].position.y + totalHeight + charOffset, sprites[i].position.z );
    404.         }
    405.     }
    406.    
    407.    
    408.     /// <summary>
    409.     /// Text-wrapping function performs function according to UIText wrapMode setting.
    410.     /// Beware, there are (minor) allocation and performance penalties to word wrapping!
    411.     /// </summary>
    412.     private string wrapText( string text, float scale )
    413.     {
    414.         var newText = string.Empty;
    415.         float dx = 0;
    416.         //float dy = 0;
    417.         int length = 0;
    418.        
    419.         // Use Double-size wrap length in HD mode.
    420.         var scaledWrapWidth = lineWrapWidth * UI.scaleFactor;
    421.        
    422.         switch( wrapMode )
    423.         {
    424.             case UITextLineWrapMode.None:
    425.             {
    426.                 // No-op
    427.                 newText = text;
    428.                 break;
    429.             }
    430.             case UITextLineWrapMode.AlwaysHyphenate:
    431.             {
    432.                 length = text.Length;
    433.                 for( var i = 0; i < length; i++ )
    434.                 {
    435.                     var charId = System.Convert.ToInt32( text[i] );    
    436.                     var charWidth = _fontDetails[charId].xadvance;
    437.                
    438.                     if( charId == ASCII_NEWLINE )
    439.                     {
    440.                         newText += "\n";
    441.                         dx = 0;    
    442.                     }
    443.                     else if( dx > scaledWrapWidth )
    444.                     {
    445.                         int prevCharId = ASCII_SPACE;
    446.                         if( i > 1 )
    447.                         {
    448.                             prevCharId = text[i-1];
    449.                         }
    450.                    
    451.                         // Wrap here, unless this character or previous character is a space.
    452.                         if( charId == ASCII_SPACE )
    453.                         {
    454.                             // If this is a space, do a simple line break and skip the space.
    455.                             newText += "\n";
    456.                         }
    457.                         else if( prevCharId == ASCII_SPACE )
    458.                         {
    459.                             // Add the character, but do not hyphenate line.
    460.                             newText += "\n" + text[i];
    461.                         }
    462.                         else
    463.                         {
    464.                             // use ASCII hyphen-minus to wrap.
    465.                             newText += "-\n" + text[i];
    466.                         }
    467.                    
    468.                         // New line, break.
    469.                         dx = 0;
    470.                     }  
    471.                     else
    472.                     {
    473.                         newText += text[i];    
    474.                     }
    475.                     dx += charWidth;
    476.                 }
    477.                 break;
    478.             }
    479.             case UITextLineWrapMode.MinimumLength:
    480.             {
    481.                 // Break text into words
    482.                 var words = text.Split( new char[]{ ' ' } );
    483.                 length = words.Length;
    484.                 float spaceWidth = wordWidth( " ", scale );
    485.                 float spaceLeft = scaledWrapWidth;
    486.            
    487.                 for( var i = 0; i < length; i++ )
    488.                 {
    489.                     var size = wordWidth( words[i], scale );
    490.                     if( size + spaceWidth > spaceLeft )
    491.                     {
    492.                         // Insert line break before word.
    493.                         newText +=  "\n" + words[i] + " ";
    494.                    
    495.                         // Reset space left on line
    496.                         spaceLeft = scaledWrapWidth - size;
    497.                     }  
    498.                     else
    499.                     {
    500.                         // Insert word
    501.                         newText += words[i] + " ";
    502.                         spaceLeft = spaceLeft - ( size + spaceWidth );
    503.                     }
    504.                    
    505.                 }
    506.                 break;
    507.             }
    508.         }  // end case
    509.        
    510.         return newText;
    511.     }
    512.  
    513.  
    514.  
    515.  
    516.     /// <summary>
    517.     /// Convenience method to calculate width of a word.
    518.     /// </summary>
    519.     private float wordWidth( string word, float scale )
    520.     {
    521.         // Convert the word into char array.
    522.         var width = 0f;
    523.         foreach( var c in word )
    524.         {
    525.             var charId = System.Convert.ToInt32( c );
    526.            
    527.             width += _fontDetails[charId].xadvance * scale;
    528.         }
    529.         return width;
    530.     }
    531.  
    532.  
    533.    
    534.     /// <summary>
    535.     /// Convenience method to configure and optionally instantiate a new UISprite for a font character.
    536.     /// </summary>
    537.     private UISprite configureSpriteForCharId( UISprite sprite, int charId, float xPos, float yPos, float scale, int depth )
    538.     {
    539.         var uvRect = new UIUVRect( (int)_textureOffset.x + _fontDetails[charId].posX, (int)_textureOffset.y + _fontDetails[charId].posY, _fontDetails[charId].w, _fontDetails[charId].h, _manager.textureSize );
    540.        
    541.         // NOTE: This contains a bugfix from the previous version where offsetx was being used
    542.         // in the wrong spot according to the angelcode spec. xadvance is the complete character width
    543.         // and offsetx is supposed to be used only during character rendering, not during cursor advance.
    544.         // Please note that yPos already has offsety built in.
    545.         var rect = new Rect( xPos + _fontDetails[charId].offsetx* scale,
    546.                              yPos,
    547.                              _fontDetails[charId].w,
    548.                              _fontDetails[charId].h);
    549.        
    550.         if( sprite == null )
    551.         {
    552.             sprite = new UISprite( rect, depth, uvRect, false );
    553.             _manager.addSprite( sprite );
    554.         }
    555.         else
    556.         {
    557.             sprite.uvFrame = uvRect;
    558.             sprite.position = new Vector3( rect.x, -rect.y, depth );
    559.             sprite.setSize( rect.width, rect.height );
    560.         }
    561.  
    562.  
    563.         // We scale the sprite this way so it will work with the container clipping
    564.         sprite.autoRefreshPositionOnScaling = false;
    565.         sprite.scale = new Vector3( scale, scale, 1 );
    566.        
    567.         return sprite;
    568.     }
    569.    
    570.  
    571.  
    572.     /// <summary>
    573.     /// Returns the actual size that will be required to display the text
    574.     /// </summary>
    575.     public Vector2 sizeForText( string text )
    576.     {
    577.         return sizeForText( text, 1f );
    578.     }
    579.    
    580.    
    581.     public Vector2 sizeForText( string text, float scale )
    582.     {
    583.         float dx = 0;
    584.         float dxMax = 0;
    585.         float dy = 0;
    586.        
    587.         float offsetY;
    588.         int fontLineSkip = 0;
    589.         int charId = 0;
    590.        
    591.         // Simulate text wrapping
    592.         text = wrapText( text, scale );
    593.        
    594.         // Simulated origin of 0, 0
    595.        
    596.         //float xPos = 0;
    597.         //float yPos = 0;
    598.        
    599.         for( var i = 0; i < text.Length; i++ )
    600.         {
    601.             charId = System.Convert.ToInt32( text[i] );
    602.            
    603.            
    604.             if( charId == ASCII_NEWLINE )
    605.             {
    606.                 // calculate the size to center text on Y axis, based on its scale
    607.                 // 77 is the "M" char usually big enough to get a proper spaced
    608.                 // lineskip, use any other char if you want
    609.                 // this looked like a duplicate code bug, so I commented it out.
    610.                 //fontLineSkip += (int)( _fontDetails[77].h * scale );
    611.                
    612.                 // add a small bit of spacing
    613.                 fontLineSkip += (int)( _fontDetails[77].h * scale * lineSpacing );
    614.                 dx = 0;
    615.             }
    616.             else
    617.             {
    618.                 // calculate the size to center text on Y axis, based on its scale
    619.                 offsetY = _fontDetails[charId].offsety * scale;
    620.                 dy =  0 + offsetY + fontLineSkip;
    621.             }
    622.  
    623.  
    624.             // calculate the size to advance, based on its scale
    625.            
    626.        
    627.             // advance the position to draw the next letter
    628.             dx += _fontDetails[charId].xadvance * scale;
    629.            
    630.             // we want the longest line
    631.             if( dxMax < dx )
    632.                 dxMax = dx;
    633.         }
    634.        
    635.         return new Vector2( dxMax > 0 ? dxMax : dx, dy + ( _fontDetails[77].h * scale ) );
    636.     }
    637.  
    638.  
    639.  
    640.  
    641.     /// <summary>
    642.     /// Creates a new UITextInstance and draws the text at the given position.  The UITextInstance is mutable and can
    643.     /// be changed at any time
    644.     /// </summary>
    645.     public UITextInstance addTextInstance( string text, float xPos, float yPos )
    646.     {
    647.         return addTextInstance( text, xPos, yPos, 1f, 1, 0 );
    648.     }
    649.    
    650.    
    651.     public UITextInstance addTextInstance( string text, float xPos, float yPos, float scale )
    652.     {
    653.         return addTextInstance( text, xPos, yPos, scale, 1, 0 );
    654.     }
    655.    
    656.     public UITextInstance addTextInstance( string text, float xPos, float yPos, float scale, int depth, int spacing )
    657.     {
    658.         return addTextInstance( text, xPos, yPos, scale, depth, spacing, Color.white );
    659.     }
    660.    
    661.     public UITextInstance addTextInstance( string text, float xPos, float yPos, float scale, int depth, int spacing, Color color )
    662.     {
    663.         return addTextInstance( text, xPos, yPos, scale, depth, spacing, color, this.alignMode, this.verticalAlignMode );
    664.     }
    665.    
    666.    
    667.     public UITextInstance addTextInstance( string text, float xPos, float yPos, float scale, int depth, int spacing,  Color color, UITextAlignMode alignMode, UITextVerticalAlignMode verticalAlignMode )
    668.     {
    669.         return addTextInstance( text, xPos, yPos, scale, depth, spacing, new Color[] { color }, alignMode, verticalAlignMode );
    670.     }
    671.    
    672.    
    673.     public UITextInstance addTextInstance( string text, float xPos, float yPos, float scale, int depth, int spacing, Color[] colors, UITextAlignMode alignMode, UITextVerticalAlignMode verticalAlignMode )
    674.     {
    675.         if( forceLowAscii )
    676.             forceLowAsciiString( ref text );
    677.        
    678.         var textInstance = new UITextInstance( this, text, xPos, yPos, scale, depth, colors, alignMode, verticalAlignMode );
    679.         textInstance.parent = _manager.transform;
    680.        
    681.         drawText(textInstance, textInstance.xPos, textInstance.yPos, textInstance.textScale, textInstance.depth, spacing, colors, textInstance.alignMode, textInstance.verticalAlignMode);
    682.        
    683.         return textInstance;
    684.     }
    685.  
    686.  
    687.    
    688.     public void updateText( UITextInstance textInstance )
    689.     {
    690.         // kill the current text then draw some new text
    691.         drawText( textInstance, textInstance.xPos, textInstance.yPos, textInstance.textScale, textInstance.depth, textInstance.colors, textInstance.alignMode, textInstance.verticalAlignMode );
    692.     }
    693.    
    694.    
    695.     void forceLowAsciiChar( ref string character )
    696.     {
    697.         // Perform character conversions.
    698.         // NOTE: These will use the low-ASCII addresses for quotes and dashes to be safe.
    699.         if ( character == "8211" ) character = "150";
    700.         else if ( character == "8212" ) character = "151";
    701.         else if ( character == "8216" ) character = "145";
    702.         else if ( character == "8217" ) character = "146";
    703.         else if ( character == "8220" ) character = "147";
    704.         else if ( character == "8221" ) character = "148";
    705.        
    706.     }
    707.    
    708.    
    709.     void forceLowAsciiString( ref string text )
    710.     {
    711.         text = text.Replace( char.ConvertFromUtf32( 8211 ), char.ConvertFromUtf32( 150 ) ); // Hyphen or En-Dash
    712.         text = text.Replace( char.ConvertFromUtf32( 8212 ), char.ConvertFromUtf32( 151 ) ); // Em-Dash
    713.         // NOTE: Convert to ASCII 0x27 for straight single quotes, but might have access to low values.
    714.         if ( hasLowAsciiQuotes )
    715.         {
    716.             text = text.Replace( char.ConvertFromUtf32( 8216 ), char.ConvertFromUtf32( 145 ) ); // Left Single Quotation Mark
    717.             text = text.Replace( char.ConvertFromUtf32( 8217 ), char.ConvertFromUtf32( 146 ) ); // Right Single Quotation Mark
    718.         }
    719.         else
    720.         {
    721.             text = text.Replace( char.ConvertFromUtf32( 8216 ), char.ConvertFromUtf32( 39 ) ); // Move to Straight Quotation Mark
    722.             text = text.Replace( char.ConvertFromUtf32( 8217 ), char.ConvertFromUtf32( 39 ) ); // Move to Straight Quotation Mark
    723.         }
    724.         // NOTE: Convert to ASCII 0x22 for straight double quotes
    725.         if ( hasLowAsciiQuotes )
    726.         {
    727.             text = text.Replace( char.ConvertFromUtf32( 8220 ), char.ConvertFromUtf32( 147 ) ); // Left Double Quotation Mark
    728.             text = text.Replace( char.ConvertFromUtf32( 8221 ), char.ConvertFromUtf32( 148 ) ); // Right Double Quotation Mark
    729.         }
    730.         else
    731.         {
    732.             text = text.Replace( char.ConvertFromUtf32( 8220 ), char.ConvertFromUtf32( 34 ) ); // Left Double Quotation Mark
    733.             text = text.Replace( char.ConvertFromUtf32( 8221 ), char.ConvertFromUtf32( 34 ) ); // Right Double Quotation Mark
    734.         }
    735.     }
    736.  
    737.  
    738. }
    739.  
     
    Last edited: Apr 3, 2013
  20. Zelek

    Zelek

    Joined:
    Jun 12, 2010
    Posts:
    87
    Has anyone gotten seamless sprites to work with UIToolkit+TexturePacker? I've tried fiddling with the "Border padding", "Shape Padding", and "Inner Padding" values in Texture Packer, but there's always a visible seam between adjacent sprites.
     
  21. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    Anyone been able to get the scaleTo to scale from the center of the object rather than the top left?
     
  22. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    746
    Ok no problem scaling sprites from the center point but I'm having trouble scaling AbsoluteLayouts and having the children's positions shift with the new scale of it's parent.
     
  23. pinx

    pinx

    Joined:
    Apr 18, 2013
    Posts:
    1
    Dear all,
    as I cannot find a solution by searching, this is a beginner question: I have two "panel" UIObjects with two child objects each, one of the children is a UIProgressBar. I manage to position them by using ".positionFrom..", but how do I rotate them (ideally the panels)?
    Thanks for a beginner-friendly explanation...

    Update: OK, I found the way to rotate using extremely short animations in Start(), but I'm still wondering whether there is a "cleaner" way to setup the elements?
     
    Last edited: Apr 25, 2013
  24. Zelek

    Zelek

    Joined:
    Jun 12, 2010
    Posts:
    87
    Has anyone been able to get around this bug? I'm having the exact same problem, and Trooper's code works if I call ZSort() right after creating the sprites, but it doesn't seem to help the problem when I'm dynamically changing the sprite's Z position in the Update() loop. As Casio mentioned, this is only a problem on mobile devices, not on the Mac or PC standalone.

    Here's a super short example program I'm using to test on my phone, if anyone sees something foolish in there:
    Code (csharp):
    1. public class App : MonoBehaviour {
    2.     void Start () {
    3.             sprite1 = UI.firstToolkit.addSprite("sprite.png", 0, 0);
    4.             sprite2 = UI.firstToolkit.addSprite("sprite.png", 0, 0);
    5.  
    6.             sprite1.position = new Vector3(20, -20, 2);
    7.             sprite2.position = new Vector3(40, -30, 3);
    8.  
    9.             UI.firstToolkit.ZSort();
    10.     }
    11.    
    12.     void Update () {
    13.             // If we get a touch, change the Z position of the sprite
    14.             foreach (Touch touch in Input.touches) {
    15.                 sprite2.position = new Vector3(
    16.                         sprite2.position .x,
    17.                         sprite2.position .y,
    18.                         1
    19.                 );
    20.                 UI.firstToolkit.ZSort();
    21.             }
    22.     }
    23.  
    24.     protected UISprite sprite1;
    25.     protected UISprite sprite2;
    26. }
     
  25. TokyoDan

    TokyoDan

    Joined:
    Jun 16, 2012
    Posts:
    1,080
    I've been testing UIToolkit. I create buttons and text and everything works in the Game view in the editor. But when I build for iOS Simulator or for an iOS device, the buttons don't appear. The scene is blank. There are no errors anywhere. I tried on an iPad1 (non-retina) and an iPad3 (retina). What could be wrong.
     
  26. auwri

    auwri

    Joined:
    Mar 25, 2013
    Posts:
    5
    Quick, maybe simplistic question.

    I want to change the alpha of UIToolkit button elements in my game's pause menu. (for example, I'm using onTouchDown for the depressed state of a button so I don't need two separate sprite graphics).

    In regular gameplay I use the built-in alphaFrom and alphaTo methods, but I pause my game by setting Time.timeScale = 0 (to preserve momentum and other states). The built in UIObjectAnimationExtensions alpha methods all require time to elapse for their animations to run.

    Is there a way to set the alpha directly? I'd be OK with not animating it if I could just modify the alpha. Thanks!
     
  27. auwri

    auwri

    Joined:
    Mar 25, 2013
    Posts:
    5
    Hmm... what kind of positioning are you using? Have you checked in multiple resolutions within the editor to make sure nothing's rendering offscreen?
     
  28. ron3662

    ron3662

    Joined:
    May 29, 2013
    Posts:
    1
    Hey "MetalGreg", even i got the same issue,
    But after browsing through the forums, i've got some possible solution but again i have not tested them yet.

    Try zaxis sorting between sprites, or check the default shader given in the uitoolkit (May be its not supported by the device)
     
  29. Mister-E2

    Mister-E2

    Joined:
    Jun 6, 2013
    Posts:
    179
    I am trying to create a grid of buttons in a 4x4 format and center them on screen. I did this and parented them all to a UIObject "panel". But when I try and scale each button individually, they all scale down and change to the same position. Anyone care to explain?

    The centerize() function does not work for me. I followed this intro: http://www.youtube.com/watch?v=tOh8ZmiU9RY

    code exactly as it is. My button still scales to the corner point. Help please
     
    Last edited: Jun 7, 2013
  30. gfxguru

    gfxguru

    Joined:
    Jun 5, 2010
    Posts:
    107
    I am new to UIToolkit, thank you prime31 for giving such a wonderful thing free.
    I am able to make gui work, and it is giving very good performance also. But some advanced things are causing roadblocks for me.

    1. My game has a few game screens and i need to slide and fade from one screen to other - something like iphone guis
    2.How to add a static background for all the screens.

    i have seen some topics on this thread but i dont remember where..102 pages of thread :)


    Can somebody throw some light on these things?

    Is there any documentation available for this uitoolkit...?

    thanks in advance
     
  31. AtomicChimp

    AtomicChimp

    Joined:
    Aug 9, 2011
    Posts:
    47
    @Mister E2 - From what I understand, .centerize() changes the pivot of a sprite to the center but does not change the position on the screen. If you want to place a sprite on the very center of the screen, use mySprite.positionCenter(); If you might need to offset it slightly, use mySprite.positionFromCenter( 0.0f, 0.0f ) and change your float values. Hope this helps. :)
     
  32. timothyallan

    timothyallan

    Joined:
    May 22, 2013
    Posts:
    72
    I'm getting much different placement and sizing results depending on the hardware and resolutions used. Here is a shot with 3 UISliders, each being either 300 or 600px wide depending on the 2x flag.

    I've included shots from iPhone4 in the game view, and a real iPhone4... as well as a normal iPhone in game view and an actual iPhone 3Gs. Yes, the game window is sized enough so there are no resizing shenanigans going on :) Any suggestions?

    $iphone.png $iphone4.png $iphone4unity.png $iphoneunity.png
     
  33. jdat2020

    jdat2020

    Joined:
    Jun 27, 2013
    Posts:
    4
    I'm really have a lot of trouble and I really hate to ask but I've spent the last 3 days searching for answers and haven't found anything that makes sense to me. I am trying to set up an options menu that uses the UIToogleButton to turn things like the sounds off and so on. I have it where you can hit the options icon and three options appear, at that point you can click on the options and an X will toggle on and off of it. I have that working fine but I can not for the life of me figure out how to make it recognize which state the button is in. Mind you I have just begun to grasps Unity Script and the change to C# is making this a little harder for me. I am a able to have normal UIButtons send me to a function but I can not figure out what the setup is for the Toggle Buttons. I really wish I could find a good tutorial for this function that shows more than how to make another button disappear. Any direction would be great, this has put my game on hold and I am almost at the point of finding something other than UIToolKit to use.
     
  34. habitoti

    habitoti

    Joined:
    Feb 28, 2013
    Posts:
    141
    UIToolkit is really extremely easy and straightforward. No magic involved. Most of what you need can be looked up right in the implementation code once you understood the structure of everything.
    If you need the state, just read the .selected property of your toggle button. If you want to react on state change, subscribe to the onToggle event.

    Code (csharp):
    1.  
    2.     UIToggleButton mySwitch;
    3.  
    4.     // event handler
    5.     void mySwitchToggled(UIToggleButton tb, bool selected) {
    6.         Debug.Log("State changed: " + selected);
    7.     }
    8.  
    9.     void Start() {
    10.         mySwitch = UIToggleButton.create("offImage.png", "onImage.png", "highlightedImage.png", 100,100); // I usually use the offImage as highlightImage
    11.         mySwitch.selected = false; // initially off
    12.         mySwitch.onToggle += mySwitchToggled; // subscribe event
    13.     }
    14.  
    15.     void someOtherPlaceInTheCode() {
    16.         Debug.Log("Current state of switch: " + mySwitch.selected);
    17.     }
    18.  
    Regards, habitoti
     
    Last edited: Jul 8, 2013
  35. jdat2020

    jdat2020

    Joined:
    Jun 27, 2013
    Posts:
    4
    Thanks habitoti, I have been working on this for a way to long. I was under the misunderstanding that I had to use specific terminology related to UIToolKit after onToggle and apparently my event handler was not correct either. This really helped me understand how to use UIToolKit a lot better. Thanks.
     
  36. jdat2020

    jdat2020

    Joined:
    Jun 27, 2013
    Posts:
    4
    Ok I'm still having issues. I can get it to show that the state has changed in the Debug log but I can't get it to change an int like I want. It only appears to be reading the first if statement. So now it will change the int to 1 but it won't change it back to 0. I am only using the toggleMusic int to make sure that it will change a value. I will be replacing it with a player pref so I can keep track of whether or not the play has turned off certain options. I've tried moving my event around and this seems to be the one that works the best so far, just doesn't work well enough.


    Code (csharp):
    1.  
    2.         UIToggleButton myMusicSwitch;
    3.     public int toggleMusic = 0;
    4.  
    5.     // event handler
    6.     void myMusicSwitchToggled(UIToggleButton tb, bool selected)
    7.     {
    8.        
    9.         if (true)
    10.         {
    11.             toggleMusic = 1;
    12.             Debug.Log("State changed: " + selected);
    13.         }
    14.        
    15.         if (false)
    16.         {
    17.             toggleMusic = 0;
    18.             Debug.Log("Now the State changed: " + selected);
    19.         }  
    20.    
    21.    }
    22.    
    23.    
    24.    
    25.    
    26.     // Use this for initialization
    27.     void Start ()
    28.     {
    29.        
    30.        
    31.         //play button
    32.         var playButton = UIButton.create ( "play.png", "playhighlighted.png", 150, 100);
    33.         playButton.onTouchUpInside += onTouchUpInsidePlayButton;
    34.        
    35.        
    36.         //help button
    37.         var helpButton = UIButton.create ( "help.png", "helphighlighted.png", 150, 225);
    38.         helpButton.onTouchUpInside += onTouchUpInsideHelpButton;
    39.        
    40.         //high scores button
    41.         var highscoreButton = UIButton.create ( "highScore.png", "highScorehighlighted.png", 150, 350);
    42.         highscoreButton.onTouchUpInside += onTouchUpInsideHighScoreButton;
    43.        
    44.         //quit button
    45.         var quitButton = UIButton.create ( "quit.png", "quithighlighted.png", 150, 475);
    46.         quitButton.onTouchUpInside += onTouchUpInsideQuitButton;
    47.        
    48.        
    49.         //this is the option I'm trying to use to test the toggleMusic int
    50.  
    51.         myMusicSwitch = UIToggleButton.create( "musicon.png", "musicoff.png", "musicoff.png", 20, 440 );
    52.         myMusicSwitch.selected = false;
    53.         myMusicSwitch.hidden = true;
    54.         myMusicSwitch.onToggle += myMusicSwitchToggled;  // subscribe event
    55.        
    56.  
    57.  
    58.         var soundButton = UIToggleButton.create( "soundon.png", "soundoff.png", "soundoff.png", 20, 380 );
    59.         soundButton.selected = false;
    60.         soundButton.hidden = true;
    61.         var isSoundOn = soundButton.selected;
    62.        
    63.        
    64.         var vibrateButton = UIToggleButton.create( "vibrateon.png", "vibrateoff.png", "vibrateoff.png", 20, 320 );
    65.         vibrateButton.selected = false;
    66.         vibrateButton.hidden = true;
    67.        
    68.        
    69.         var toggleButton = UIToggleButton.create( "optionsrotate.png", "optionsrotate.png", "options.png", 20, 500 );
    70.         toggleButton.onToggle += ( sender, newValue1) => myMusicSwitch.hidden = newValue1;
    71.         toggleButton.onToggle += ( sender, newValue2 ) => soundButton.hidden = newValue2;
    72.         toggleButton.onToggle += ( sender, newValue3 ) => vibrateButton.hidden = newValue3;
    73.         toggleButton.selected = true;
    74.        
    75.        
    76.        
    77.     }
    Apparently it's not just the first if statement. It only reads the if (true) statement. It never reads the if (false) one. I test to see if the state is in fact changing and it is, it just won't trigger the if false. I tried putting myMusicSwitch.selected = false and myMusicSwitch.selected = true but it causes it to stay true, which seems weird. Hopefully someone can see what simple thing I'm not doing.
     
    Last edited: Jul 9, 2013
  37. jdat2020

    jdat2020

    Joined:
    Jun 27, 2013
    Posts:
    4
    Ok I got it to work. I replaced the if (true) and if (false) with if (!selected) and if (selected) and it seems to be working.
     
  38. DarkzideDemigod

    DarkzideDemigod

    Joined:
    Jul 10, 2013
    Posts:
    1
    Hi Prime31!

    First thanks for sharing this Awesome UIToolkit.

    I am new to unity and I found UIToolkit to be very helpful. I just wondering if you have some sample or tutorials on using the UIKnob like limiting the rotation angle. Thanks in advance!
     
  39. Krummelz

    Krummelz

    Joined:
    Feb 20, 2013
    Posts:
    3
    Thanks Prime31! This toolkit is a lifesaver!

    Has anyone had success in using a UISlice9 element inside of a UIScrollableVerticalLayout container?
     
  40. Krummelz

    Krummelz

    Joined:
    Feb 20, 2013
    Posts:
    3
    Ok, so to answer my own cry for help; I got this to work by making the spriceSlices array in the UISlice9 class, public. Then, after creating the UISlice9 instance, I do the following:

    Code (csharp):
    1. foreach(UISprite slice in mySlice9Instance.spriceSlices)
    2. {
    3.     slice.parentUIObject = mySlice9Instance;
    4. }
     
  41. KrishnaMV

    KrishnaMV

    Joined:
    Feb 15, 2013
    Posts:
    2
    im using prime 31 for FB integration... i have some description..but escape seqences are not working...my situation is i have got a description screen....i want to make the text align.like new line...break..not working
     
  42. Dothackking

    Dothackking

    Joined:
    Sep 10, 2012
    Posts:
    52
    hey guys, when I imported it, and pressed play after doing the "setup" steps, it says that the array in UI.cs is out of range. Any help would be appreciated
     
  43. JuiceReis

    JuiceReis

    Joined:
    May 28, 2013
    Posts:
    29
    How would I go about hiding the UItoolkit before calling a takescreenshot action from Etcetera? I currently have this,

    Any ideas? Thank you.
     
  44. JuiceReis

    JuiceReis

    Joined:
    May 28, 2013
    Posts:
    29
    I've tried using a boolean that links into the StartCoroutine to take the screenshot,
    But this doesn't hide the buttons when taking the photo I'm pretty much new to c sharp and I'm learning as I go but in theory this makes sense? The IEnumerator ScreenShot fires linear? ie one after the other?

    Any guidance in this would be appreciated. Thanks.
     
  45. atsakir

    atsakir

    Joined:
    Sep 28, 2012
    Posts:
    8
    Is there any way I can get a version of UIToolkit that works with Unity 3.5.x? The master branch gives me an error about meta files not being compatible so I tried to see if there was a version in github that is still working with Unity 3.5.x but I'm not really familiar with cloning a specific revision in git (I'm more versed in svn).

    If someone can help I would appreciate it immensely.

    EDIT: Ok, I cloned the ScrollableFix branch from 2 years ago and that works with 3.5.x. Is there any way I can get a newer version on the master branch that still woorks with Unity 3.5.x?
     
    Last edited: Jul 18, 2013
  46. JuiceReis

    JuiceReis

    Joined:
    May 28, 2013
    Posts:
    29
    I've done the screenshot and hidden the buttons through animating the alpha channel in 3 stages, 100% opacity to 0, then 0 to 0 (giving enough time to take the screenshot) then reappear after, is this a proficient enough way of doing this?

    Previous I had ontouchdown and ontouchup to animate the fade out and fade in of the button but it was a little temperamental when clicking fast.

    If I use .hidden = true/false as soon as its classed as hidden it reappears due to the touch would no longer be assigned to that button ontouchdown.
     
  47. lagidigu3ll

    lagidigu3ll

    Joined:
    Jul 20, 2013
    Posts:
    3
    I can't manage to implement the toolkit inside my own project. Do I really have to import the entire library of 30 mb? When I try to follow all the steps shown in the setup guide, it won't let me drag the ui.cs script onto my empty gameobject, because the script is bugged... How much do I actually have to import into my own project, and how?
     
  48. AtomicChimp

    AtomicChimp

    Joined:
    Aug 9, 2011
    Posts:
    47
    Has anyone had any luck in placing a photo from the internet onto a UISprite? I need to do this for a UISprite in a slider and have not yet found a way to do it. Any suggestions for approach are welcome.
     
  49. AtomicChimp

    AtomicChimp

    Joined:
    Aug 9, 2011
    Posts:
    47
    If you are still experiencing trouble, please describe what "setup" steps you did so that we might be able to help determine what is going on in your project.
     
  50. lagidigu3ll

    lagidigu3ll

    Joined:
    Jul 20, 2013
    Posts:
    3
    I basically created the empty game object as said in the instructions (in my current project), but I just added the ui.cs script and the uitoolkit.cs script. The thing is that I want to avoid to import the entire file (30mb) into my project, as it is designed for mobile devices...