Author Topic: How to do code for ngui  (Read 2648 times)

wendy3112

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
How to do code for ngui
« on: July 05, 2014, 11:16:09 PM »
I'm not sure how to put this but does anyone know how convert this script to ngui its a memory game. I am still learning but if you could maybe give me a tutorial on how to do this it would be great thanks.

  1. var cols:int = 4; // the number of columns in the card grid
  2. var rows:int = 4; // the number of rows in the card grid
  3. var totalCards:int = 16;
  4. var matchesNeededToWin:int = totalCards * 0.5; // If there are 16 cards, the player needs to find 8 matches to clear the board
  5. var matchesMade:int = 0; // At the outset, the player has not made any matches
  6. var cardW:int = 100; // Each card's width and height is 100 pixels
  7. var cardH:int = 100;
  8. var aCards:Array; // We'll store all the cards we create in this Array
  9. var aGrid:Array; // This array will keep track of the shuffled, dealt cards
  10. var aCardsFlipped:ArrayList; // This array will store the two cards that the player flips over
  11. var playerCanClick:boolean; // We'll use this flag to prevent the player from clicking buttons when we don't want him to
  12. var playerHasWon:boolean = false; // Store whether or not the player has won. This should probably start out false :)
  13.  
  14. function Start()
  15. {
  16.   playerCanClick = true; // We should let the player play, don't you think?
  17.  
  18.   // Initialize the arrays as empty lists:
  19.   aCards = new Array();
  20.   aGrid = new Array();
  21.   aCardsFlipped = new ArrayList();
  22.  
  23.   BuildDeck();
  24.  
  25.   for(i=0; i<rows; i++)
  26.   {
  27.     aGrid[i] = new Array(); // Create a new, empty array at index i
  28.      
  29.    for(j=0; j<cols; j++)
  30.     {
  31.                 var someNum:int = Random.Range(0,aCards.length);
  32.                 aGrid[i][j] = aCards[someNum];
  33.                 aCards.RemoveAt(someNum);
  34.     }
  35.   }
  36. }
  37.  
  38. function BuildDeck()
  39. {
  40.    var totalRobots:int = 4;  // we've got four robots to work with
  41.    var card:Object; // this stores a reference to a card
  42.    var id:int = 0;
  43.  
  44.    for(i=0; i<totalRobots; i++)
  45.    {
  46.                 var aRobotParts:Array = ["Head", "Arm", "Leg"];
  47.                 for(j=0; j<2; j++)
  48.                 {
  49.                         var someNum:int = Random.Range(0, aRobotParts.length);
  50.                         var theMissingPart:String = aRobotParts[someNum];
  51.  
  52.                         aRobotParts.RemoveAt(someNum);
  53.  
  54.                         card = new Card("robot" + (i+1) + "Missing" + theMissingPart, id);
  55.                         aCards.Add(card);
  56.  
  57.                         card= new Card("robot" + (i+1) + theMissingPart, id);
  58.                         aCards.Add(card);
  59.                         id++;
  60.            }           
  61.    }
  62. }
  63.  
  64.  
  65. function BuildWinPrompt()
  66. {
  67.         var winPromptW:int = 100;
  68.         var winPromptH:int = 90;
  69.  
  70.         var halfScreenW:float = Screen.width/2;
  71.         var halfScreenH:float = Screen.height/2;
  72.  
  73.         var halfPromptW:int = winPromptW/2;
  74.         var halfPromptH:int = winPromptH/2;
  75.  
  76.         GUI.BeginGroup(Rect(halfScreenW-halfPromptW,
  77.         halfScreenH-halfPromptH, winPromptW, winPromptH));
  78.         GUI.Box (Rect (0,0,winPromptW,winPromptH), "A Winner is You!!");
  79.        
  80.         if(GUI.Button(Rect(10,40,80,20),"Play Again"))
  81.         {
  82.                 Application.LoadLevel("Title");
  83.         }
  84.         GUI.EndGroup();
  85. }
  86.  
  87.  
  88.  
  89. function OnGUI () {
  90.         GUILayout.BeginArea (Rect (0,0,Screen.width,Screen.height));    
  91.         BuildGrid();
  92.         if(playerHasWon) BuildWinPrompt();
  93.         GUILayout.EndArea();
  94.         print("building grid!");
  95. }
  96.  
  97. function BuildGrid()
  98. {
  99.         GUILayout.BeginVertical();
  100.         GUILayout.FlexibleSpace();
  101.         for(i=0; i<rows; i++)
  102.         {
  103.                 GUILayout.BeginHorizontal();
  104.                 GUILayout.FlexibleSpace();
  105.                 for(j=0; j<cols; j++)
  106.                 {
  107.                         var card:Object = aGrid[i][j];  
  108.                         var img:String;
  109.  
  110.                         if(card.isMatched)
  111.                         {
  112.                         img = "blank";
  113.                         }
  114.                         else
  115.                         {
  116.                                 if(card.isFaceUp)
  117.                                 {
  118.                                         img = card.img;
  119.                                 }
  120.                                 else
  121.                                 {
  122.                                         img = "wrench";
  123.                                 }
  124.                         }
  125.  
  126.                         GUI.enabled = !card.isMatched;
  127.  
  128.                         if(GUILayout.Button(Resources.Load(img),
  129.                         GUILayout.Width(cardW)))
  130.                         {
  131.                                 if(playerCanClick)
  132.                                 {
  133.                                         FlipCardFaceUp(card);
  134.                                 }
  135.                                 Debug.Log(card.img);
  136.                         }
  137.                         GUI.enabled = true;
  138.                 }
  139.                 GUILayout.FlexibleSpace();
  140.                 GUILayout.EndHorizontal();  
  141.         }
  142.         GUILayout.FlexibleSpace();
  143.         GUILayout.EndVertical();
  144. }
  145.  
  146. function FlipCardFaceUp(card:Card)
  147. {
  148.         card.isFaceUp = true;
  149.         if(aCardsFlipped.IndexOf(card) < 0)
  150.         {
  151.                 aCardsFlipped.Add(card);
  152.  
  153.                 if(aCardsFlipped.Count == 2)
  154.                 {
  155.                         playerCanClick = false;
  156.  
  157.                         yield WaitForSeconds(1);
  158.                        
  159.                         if(aCardsFlipped[0].id == aCardsFlipped[1].id)
  160.                         {
  161.                                 // Match!
  162.                                 aCardsFlipped[0].isMatched = true;
  163.                                 aCardsFlipped[1].isMatched = true;
  164.                                
  165.                                 matchesMade ++;
  166.            
  167.                                 if(matchesMade >= matchesNeededToWin)
  168.                                 {
  169.                                         playerHasWon = true;
  170.                                 }
  171.                                
  172.                         }
  173.                         else
  174.                         {
  175.                                 aCardsFlipped[0].isFaceUp = false;
  176.                                 aCardsFlipped[1].isFaceUp = false;
  177.                         }
  178.                        
  179.                         aCardsFlipped = new ArrayList();
  180.  
  181.                         playerCanClick = true;
  182.                 }
  183.         }
  184. }
  185.  
  186.  
  187. class Card extends System.Object
  188. {
  189.   var isFaceUp:boolean = false;
  190.   var isMatched:boolean = false;
  191.   var img:String;
  192.   var id:int;
  193.    
  194.   function Card(img:String, id:int)
  195.   {
  196.     this.img = img;
  197.         this.id = id;
  198.   }
  199. }

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to do code for ngui
« Reply #1 on: July 06, 2014, 11:56:32 AM »
I know how, but I'm not here to do your homework for you.

Some tips:

* Use c# instead of Unityscript (javascript), it just plain works better.
* Don't use ArrayList, just use List<>. I assume whoever wrote the code has a java background, where ArrayList is the hot rave, but List is basically the same thing, but inside the Unity namespace with easier usage.
* There's a fundamental difference between OnGUI and NGUI in that OnGUI is re-drawn every frame, such that if the code returns, it just doesn't draw what's below - NGUI is persistent, so if you set something up and leave it alone, it will stay until you do something with it.

wendy3112

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: How to do code for ngui
« Reply #2 on: July 06, 2014, 12:09:14 PM »
I was learning that code through a unity book so i haven't had the experience but you know what with ngui it doesn't give much tutorials with scripting the ui. but i was also asking for a tutorial so i could learn how to was not asking for people to do it for me oh and i don't know c# because they tell me be better off learning java first
« Last Edit: July 06, 2014, 12:20:03 PM by wendy3112 »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to do code for ngui
« Reply #3 on: July 06, 2014, 12:33:49 PM »
True enough, there aren't much in the way of tutorials as such, but you can look at the examples which are in the package you got from the asset store - they show many if not most of the different controls that NGUI provides by default.

There are tutorials here: https://www.youtube.com/user/ArenMookR5/videos

And you can get more information here http://www.tasharen.com/?page_id=140 as well.


I would really recommend you to not work with javascript. C# is closer to real java and is much better to work with in the long run, while not being particularly more confusing than Unityscript. If they tell you to use javascript first, their advice is questionable. :)

wendy3112

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 9
    • View Profile
Re: How to do code for ngui
« Reply #4 on: July 06, 2014, 10:44:41 PM »
When i get more experience i will come back to that project i am going through the ngui for unity book but it has been hard when the person used different version of ngui but it teaches you how to code with ngui