Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Donovan Rucastle · Feb 23, 2013 at 08:09 AM · guiarraysstore

StoreGUI problems

Hi guys

I was wondering if anyone could help me ? I found this code the other day and I have been messing around with it but as I don't know C# well I don't really know what to do ?

I would like to add the functionality of being able to instantiate a prefab when I have selected the item and bought it ? I guess I would have to add a third array referencing to the other two arrays in that category and then add some code in Additemtoinventory function ( it's already there) but how would I do this ?

And one last thing I'm sorry I know this is a large request but can I make the prefab spawn as the child of my empty game object called spawn point ?

Please could someone help ? It would mean a lot !!

This is the script ! If anyone could format it for me that would be great as I am typing on an ipad and cannot do that!

/* Esse script cria uma loja em GUI para Itens

  • 1- Adicione este script a um GameObject

  • 2- Mude sua Tag para NPC

  • 3- Crie um prefab e coloque por toda parte!

  • 4- Chegue perto do Prefab e clique nele para mostrar a loja

  • 5- Afaste-se e ela fecha sozinha

*

  • Autor: Vinicius Rezendrix - Brasil

  • Data: 07/08/2012

*

  • This Script generates a store on GUI for Itens

  • 1- Add this script to a GameObject

  • 2- Set it´s tag for NPC

  • 3- Create a prefab of it and spawn it all around!

  • 4- Get close of the prefab and click him to display the store

  • 5- Get away and the store will close

*

  • Autor: Vinicius Rezendrix - Brazil

  • Data: 07/08/2012

*

*/

using UnityEngine;

using System.Collections;

public class StoreGUI : MonoBehaviour {

region DECLARED VARIABLES

 private Transform myTransform;              // NPC Transform

 private Transform playerTransform;          // Player Transform

 private float playerDistance;               // distance between Player and NPC

 

 public bool displayStore = true;            // show the store

 

 // define and create our GUI delegate

 private delegate void GUIMethod(); 

 private GUIMethod currentGUIMethod; 

 private Vector2 scrollPosition = Vector2.zero;

 

 // Array with the Itens

 public ArrayList itensArray = new ArrayList();

 public ArrayList itensPriceArray = new ArrayList();

 

 // Array with the Armor

 public ArrayList armorsArray = new ArrayList();

 public ArrayList armorsPriceArray = new ArrayList();

 

 // Array with the Sword

 public ArrayList swordsArray = new ArrayList();

 public ArrayList swordsPriceArray = new ArrayList();

 

 private int amount = 1;                     //amount of itens to buy

 private int totalPrice;                     //total price of itens to buy

 private string selectedItem = "";           // selected item from the store

 private int selectedItemPrice;              // price of the selected item from the store

 

 private Color lerpedColor;                  //just a test

endregion DECLARED VARIABLES

 // Use this for initialization

 void Start () { 

         

     GameObject go = GameObject.FindGameObjectWithTag("Player"); //find the GameObject w/ "Player" TAG

     playerTransform = go.transform;                             //and stores its Transform

 

     myTransform = transform;                // NPC transform;

             

     this.currentGUIMethod = itensMenu;      // start with the main menu GUI

 

     selectedItem = "";                      //sets the selected item to null

     

     addStuffToArrays();                     // fills the arrays with the Items

 }

 

 //Update is called Once per frame

 void Update(){

     

     //this is just a test

     lerpedColor = Color.Lerp(Color.green, Color.red, Time.time / 10);

     

     // stores the Distancie between the NPC and the player

     playerDistance = Vector3.Distance(myTransform.position, playerTransform.position);

     

     // if the playerDistance is bigger than 4, close the store

     if(playerDistance > 4){         

         displayStore = false;

     }

 }

 

 //Displays the store

 public void OnMouseUpAsButton() {       // When the NPC is clicked...

     if(playerDistance<4){               // and if playerDistance is lesser than 4...

         displayStore = true;            // then Displays the StoreGUI. 

     }

 }

 

region OnGUI

 // Update is called once per frame 

 void OnGUI () {

 

     //Displays the store

     if(displayStore){

     

         //Text Item

         GUI.Label (new Rect (20, 40 , 230, 30), "Item");

         //Text Price

         GUI.Label (new Rect (250, 40, 40, 30), "Price");

     

         //button Exit

         if(GUI.Button (new Rect(20 + 100+100+100, 5, 100, 30), "Exit Store")){

             displayStore = false;

         }

 

         this.currentGUIMethod(); 

         

         // Area with selected item, amount, total price...

         GUILayout.BeginArea(new Rect( 20, 270, 400, 100));

         

         GUILayout.BeginVertical("box");

         

         GUILayout.BeginHorizontal("Label");

         //Text Item

         GUILayout.Box ("Selected Item", GUILayout.Width(110));

         //Text Amount

         GUILayout.Box ("Amount");

         //Text Total

         GUILayout.Box ("Total");

         GUILayout.EndHorizontal();

         

         

         GUILayout.BeginHorizontal("box");

 

         // Box with Item Name

         GUILayout.Box  (selectedItem.ToString(), GUILayout.Width(110));

 

         // Box with Amount

         GUILayout.Box  (" X " + amount.ToString(), GUILayout.Width(50));

         

         //Button Amount plus

         if(GUILayout.RepeatButton (" + ")){

             amount++;

         }

         //button Amount minus

         if(GUILayout.RepeatButton (" - ")){

             amount--;

         }

         //box Total Price

         GUI.color = lerpedColor;

         GUILayout.Box (totalPrice.ToString(), GUILayout.Width(80));

         

         //button Buy

         if(GUILayout.Button ("Buy")){

             addItemToInventory();

         }

         

         GUILayout.EndHorizontal();

         GUILayout.EndVertical();

         GUILayout.EndArea();

         

     }   //ends the displayStore IF

 }

endregion OnGUI

 /*#---------------------- REGION MENUS----------------------------#*/

 

region ITENS MENU - called inside OnGUI

 public void itensMenu() { 

     

     GUI.Box (new Rect (20, 5, 100, 30), "Itens Menu");  // Box with the Menu Title

     

     //Scroll Area with the Itens displayed Inside                                           // The scroll is dinamic generated by the size of the array

     scrollPosition = GUI.BeginScrollView(new Rect(20, 60, 300, 200), scrollPosition, new Rect(0, 0, 120, itensArray.Count * 30));

         displayItens();

     GUI.EndScrollView();

     

     if (GUI.Button (new Rect (20 + 100, 5, 100, 30), "Armors Menu")) {

         switchMenuForArmorsButton();                //Switch for Armors Menu

     } 

     

     if (GUI.Button (new Rect (20 + 100 + 100, 5, 100, 30), "Swords Menu")) {

         switchMenuForSwordsButton();                //Switch for Swords Menu

     } 

     

 } 

endregion ITENS MENU

region ARMORS MENU - called inside OnGUI

 private void armorsMenu(){

     

     GUI.Box (new Rect (20, 5, 100, 30), "Armors Menu");             // Box with the MenuTitle

     

     //Scroll Area with the Itens displayed Inside                                           // The scroll is dinamic generated by the size of the array

     scrollPosition = GUI.BeginScrollView(new Rect(20, 60, 300, 200), scrollPosition, new Rect(0, 0, 120, armorsArray.Count * 30));

         displayArmor();

     GUI.EndScrollView();

     

     if (GUI.Button (new Rect (20 + 100, 5, 100, 30), "Itens Menu")) {

         switchMenuForItensButton();                 // Switch for Itens Menu

     } 

     

     if (GUI.Button (new Rect (20 + 100 + 100, 5, 100, 30), "Sword Menu")) { 

         switchMenuForSwordsButton();                //Switch for Swords Menu

     } 

 }  

endregion ARMOR MENU

region SWORDS MENU - called inside OnGUI

 private void swordsMenu(){

 

     GUI.Box (new Rect (20, 5, 100, 30), "Swords Menu");             // Box with the MenuTitle

     

     //Scroll Area with the Itens displayed Inside                                           // The scroll is dinamic generated by the size of the array

     scrollPosition = GUI.BeginScrollView(new Rect(20, 60, 300, 200), scrollPosition, new Rect(0, 0, 120, swordsArray.Count * 30));

         displaySword();

     GUI.EndScrollView();

     

     if (GUI.Button (new Rect (20 + 100, 5, 100, 30), "Itens Menu")) {       

         switchMenuForItensButton();                 // Switch for Itens Menu

     } 

     

 

     if (GUI.Button (new Rect (20 + 100 + 100, 5, 100, 30), "Armors Menu")) { 

         switchMenuForArmorsButton();                //Switch for Armors Menu

     } 

 }    

endregion SWORD MENU

region SWITCH MENU BUTTOMs - called inside ITENS, ARMORS AND SWORDS MENUS()

 void switchMenuForItensButton(){

     this.currentGUIMethod = itensMenu;              // switch to Itens Menu

     amount = 1;                                     // resets the amount of itens selected

 }

 

 void switchMenuForArmorsButton(){

     this.currentGUIMethod = armorsMenu;             // switch to Armors Menu

     amount = 1;                                     // resets the amount of itens selected

 }

 

 void switchMenuForSwordsButton(){

     this.currentGUIMethod = swordsMenu;             // switch to Swords Menu

     amount = 1;                                     // resets the amount of itens selected

 }

endregion SWITCH MENU BUTTOMs

 /*#---------------------- END REGION MENUS-------------------------#*/

 

 /*#---------------------- REGION DISPLAY ITENS LOOPS----------------------------#*/

region DISPLAY ITENS LOOP - called inside ItensMenu()

     public void displayItens() {

                         

         // Loop to display all the itensArray of the itensArray

         for(int cnt = 0; cnt < itensArray.Count; cnt++){

         

             //Buttom with Item Name

             if(GUI.Button (new Rect (0, 30 * (cnt), 230, 30), itensArray[cnt].ToString(), "box")){

                 

                 // Selects the item and it´s price

                 selectedItem = itensArray[cnt].ToString();

                 selectedItemPrice = (int)(itensPriceArray[cnt]);

                 

                 // resets the amount of itens selected

                 amount = 1;

             }

         

             // Displays the Item Price

             GUI.Box (new Rect (230, 30 * (cnt), 50, 30), itensPriceArray[cnt].ToString());

             

             // calculates the total price

             totalPrice = amount * (int)(selectedItemPrice);

         } 

     }

endregion DISPLAY ITENS LOOP

region DISPLAY ARMORS LOOP - called inside ArmorsMenu()

     public void displayArmor() {

         

         // Loop to display all the itensArray of the array

         for(int cnt = 0; cnt < armorsArray.Count; cnt++){

 

             //Buttom with Armor Name

             if(GUI.Button (new Rect (0, 30 * (cnt), 230, 30), armorsArray[cnt].ToString(), "box")){

                 

                 // Selects the armor and it´s price

                 selectedItem = armorsArray[cnt].ToString();

                 selectedItemPrice = (int)(armorsPriceArray[cnt]);

                 

                 // resets the amount of itens selected

                 amount = 1;

             }

             

             // Displays the Item Price

             GUI.Box (new Rect (230, 30 * (cnt), 50, 30), armorsPriceArray[cnt].ToString());

             

             // calculates the total price

             totalPrice = amount * (int)(selectedItemPrice);

         } 

     }

endregion DISPLAY ARMOR LOOP

region DISPLAY SWORDS LOOP - called inside SwordsMenu()

     public void displaySword() {

         

         // Loop to display all the itensArray of the array

         for(int cnt = 0; cnt < swordsArray.Count; cnt++){

 

             //Buttom with Armor Name

             if(GUI.Button (new Rect (0, 30 * (cnt), 230, 30), swordsArray[cnt].ToString(), "box")){

                 

                 // Selects the armor and it´s price

                 selectedItem = swordsArray[cnt].ToString();

                 selectedItemPrice = (int)(swordsPriceArray[cnt]);

                 

                 // resets the amount of itens selected

                 amount = 1;

             }

             

             // Displays the Item Price

             GUI.Box (new Rect (230, 30 * (cnt), 50, 30), swordsPriceArray[cnt].ToString());

             

             // calculates the total price

             totalPrice = amount * (int)(selectedItemPrice);

         } 

     }

endregion DISPLAY SWORD LOOP

 /*#---------------------- END REGION DISPLAY ITENS LOOPS------------------------#*/

 

 

 

region ADD ITEM TO INVENTORY - called inside OnGUI - "buy" button

 public void addItemToInventory(){           // Method for the buy buttom

     

     if(selectedItem != ""){                 //se selectedItem for diferente de null

         print("Bought Item!");

         // To Add Item X amount; - Use a loop   

         /* 

         for(int cnt = 0; cnt < amount; cnt++){

             addItemToInventory(selectedItem);

         }

         

         curGold - totalPrice;

         */

     }

 }

endregion ADD ITEM TO INVENTORY

region ADD STUFF TO ARRAYS - called inside Start()

 // Fills itensArrays, armorsArray, swordsArray, and PricesArray with the itens

 private void addStuffToArrays(){

 

 // add itens to the itensArray

     itensArray.Add("Potion");

     itensArray.Add("Hi-Potion");

     itensArray.Add("Ether");

     itensArray.Add("Phoenix Down");

     itensArray.Add("Mega-Potion");

     itensArray.Add("Mega-Ether");

     itensArray.Add("Elixir");

     itensArray.Add("Speed-Tab");

     itensArray.Add("Defense-Tab");

     itensArray.Add("Power-Tab");

     itensArray.Add("Mana-Tab");

     itensArray.Add("Magic-Tab");

         

 // add itens to the itensPriceArray     

     itensPriceArray.Add(25);

     itensPriceArray.Add(50);

     itensPriceArray.Add(70);

     itensPriceArray.Add(50);

     itensPriceArray.Add(500);

     itensPriceArray.Add(700);

     itensPriceArray.Add(700);

     itensPriceArray.Add(1000);

     itensPriceArray.Add(1000);

     itensPriceArray.Add(1000);

     itensPriceArray.Add(1000);

     itensPriceArray.Add(1000);

     

 // add itens to the armorsArray

     armorsArray.Add("Leather Coat");

     armorsArray.Add("Gold Shield");

     armorsArray.Add("Platinum Armor");

     

 // add prices to armorsPriceArray

     armorsPriceArray.Add(200);

     armorsPriceArray.Add(1000);

     armorsPriceArray.Add(2000);

     

     

 // add itens to the swordsArray

     swordsArray.Add("Wood Sword");

     swordsArray.Add("Steel Sword");

     swordsArray.Add("Buster Sword");

     swordsArray.Add("Rainbow");

     swordsArray.Add("Onimusha Sword");

     swordsArray.Add("Sword Of Sparda");

     swordsArray.Add("Blade Of Chaos");  

     swordsArray.Add("Masamune");    

     

 // add prices to swordsPriceArray

     swordsPriceArray.Add(100);

     swordsPriceArray.Add(500);

     swordsPriceArray.Add(1000);

     swordsPriceArray.Add(25000);        

     swordsPriceArray.Add(30000);

     swordsPriceArray.Add(40000);

     swordsPriceArray.Add(50000);

     swordsPriceArray.Add(80000);

 }

endregion ADD STUFF TO ARRAYS

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nventimiglia · Feb 24, 2013 at 01:44 AM 0
Share

Yeah, that re$$anonymous$$ds me why I dont use the built in gui.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by theundergod · Feb 24, 2013 at 02:04 AM

If you simply want to instantiate the item then you need to have prefabs that match the selectedItem text.

  1. Create a prefab called Blade Of Chaos

  2. Select the "Blade Of Chaos" in the store

  3. Click "Buy"

in the addItemToInventory() function (assuming your SpawnPoint object exists)

 GameObject newItem = (GameObject)Instantiate((GameObject)Resources.Load(selectedItem), SpawnPoint.transform.position, SpawnPoint.transform.rotation);

Then to set the spawn point as the parent

 newItem.transform.parent = SpawnPoint.transform;

Let me know if I missed something.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image theundergod · Feb 24, 2013 at 02:07 AM 0
Share

To make it more specific use

 (GameObject)Instantiate((GameObject)Resources.Load(selectedItem, typeof(GameObject), SpawnPoint.transform.position, SpawnPoint.transform.rotation);

To ensure it only tries to load GameObjects and not a Texture of the same name.

avatar image theundergod · Feb 24, 2013 at 02:09 AM 0
Share

Btw Oi Brasileiro! $$anonymous$$inha esposa e do Brasil tambem! :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

RPG turns system - arranging turns freezes up 1 Answer

Using arrays with OnTriggerEnter 0 Answers

need to shorten my code but unsure of how 3 Answers

Remove Items and Item Tooltips 0 Answers

Setting GUI textures 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges