Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Inventory Looting based on Forum Code

Discussion in 'Immediate Mode GUI (IMGUI)' started by Adam-Buckner, May 17, 2010.

  1. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I've updated code found on the Unity Forums (most notably in this case, code from Der Dude) to make a basic Inventory and Looting system.


    (Some Textures and Icons in this project are not licensed for distribution and are for testing purposes only)

    Please note that this code is platform independent and has NOT been optimized for an Apple Touch Device and is standard Unity code for any project.

    A webplayer example of this project can be found here.

    - Hitting the "I" key toggles the main inventory window..
    - Clicking on any lootable object creates new loot contents from a loot table and opens a loot window for that lootable object displaying the new loot content.
    - If the Inventory window is not open, opening a loot window it will open the inventory window.
    - If a loot window is open, hitting the "I" key to close the main inventory window will also close the currently open loot window
    - Repeatedly clicking on the same lootable object will toggle the loot window open and closed.
    - Clicking on a different lootable object will leave the loot window open, but will change the focus of the loot window to the new object.

    For your information I have put up:
    A more detailed explanation and links to a sample package with scripts and objects here.

    Please see this thread where Der Dude's code was first posted.
     
    Last edited: Aug 23, 2011
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Good one. Community WIN.
    AaronC
     
  3. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    I'm glad you found my code useful. Your version is looking really good. Keep up the good work!
     
  4. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Cool! Thanks for sharing. This will be helpful.
     
  5. Taigo

    Taigo

    Joined:
    Feb 18, 2010
    Posts:
    128
    You are doing a great job on that inventory Little Angel, just need to add the part to drop the objects on the scene and you are gold.
    Let me know if you need some help with art and i would gladly help.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Taigo:

    That code is not there on purpose.

    I was going for more of a "Worlds of Warcraft" approach where there are no physical objects in the game world per se, only item data that is passed between LootableObjects and the Player. I've done a number of improvements since I posted this on Monday, including incorporating the inventory into its own window and adding distance checking to the LootableObject script, so that you can only loot an object if you are close enough. The distance check also has a co-routine to regularly check distance between the player and LootableObject and closes the loot and inventory windows when the player moves too far away from the object.

    I have already assimilated these scripts into my overall game-in-progress, but was planning on compiling a number of changes and additions into a new version of the inventory system when I had enough changes "to make it worth while" (including the possibility of equipping and using objects), but that will take a while.

    Now, in a move to make all things for all people, I could try to reincorporate the "drop objects" code back into the system for a future release as well. It shouldn't be too hard to instantiate a new object containing only one on command. The inventory item class will just have to hold a reference to the prefab to instantiate. The only possible lack of polish would be clicking on the item would create another loot window, rather than something more individual... but hey.

    All this being said, this is a fairly simple system. For anyone who wants to look at a more complicated system with serialization, data files and a whole lotta more, look here: http://forum.unity3d.com/viewtopic.php?t=51475 ...

    ... but this is a bit beyond my ken for now.
     
  7. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    When I try to import the asset, I get error.

     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Wolf Dreamer:

    Try downloading it again, and/or try importing it into a new empty Unity Project. I tested the file and it worked for me. IIRC, I tested the entire loop, downloading the file from the internet and opening it.

    If you continue to have trouble, let me know.
     
  9. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    That is odd.

    Perhaps Windows Vista doesn't clear the cache or something. It worked fine this time.

    Had it in the proper directory when I went to import it, just as I had tried on my second try last time, and this time it worked!

    Minor error, easily dealt with though.
    So I have to download blender to do this? A simple enough shape, I can just use something else.

    Once I figure out what I'm doing wrong with the other code [http://forum.unity3d.com/viewtopic.php?p=322006#322006] I'll see what I can learn from it. Thanks again! You are really helping me make my dream come true.
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yup: Download Blender

    In general, most of the Unity functionality for importing files seems to require the related application to be installed on that machine (Max, C4D, etc..). Blender is free! (http://www.blender.org) And it's a simple download.

    On the other hand pedestal.blend is just part of the eye candy and even though the file chest.blend is a part of the demo, these could be replaced by unity primitives with a box collider for testing purposes. Just thow in some boxes and add the lootableObject script. I think that's all you'll need to do.

    I'll keep that import issue in mind on the next release, and perhaps I'll just have a simple plane, boxes and colliders. No eye candy. I've done some simple updates to the inventory system, including: a window to draw the inventory slots into to match the loot window; the inventory is now in a single flat array rather than a matrix and it's broken in to rows and columns for display purposes using a simple forumula - this also allows inventory sizes that are not column multiples of rows; a distance check that prevents the user from looting if too far away coupled with a co-routine that continues to check distance (unless window is closed manually) and closes the window when the player moves too far from the lootableObject; a few other minor tweaks.

    All that being said, I don't have the time, and I'm not ready to release a new version, so it will be some time before I get to posting that.
     
  11. Wolf Dreamer

    Wolf Dreamer

    Joined:
    Sep 2, 2009
    Posts:
    142
    Anyway to incorporate that into stneas's stuff?http://forum.unity3d.com/viewtopic.php?p=322006#322006 He just listed how to make it so you could easily have more slots to access.

    Being able to drag things around, to decide where to stick them at, is important. Move things from your backpack, to a bank account, to a trade menu, etc.

    I'm trying many things, but keep ending up with a never ending string of errors and confusion.
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I really like a lot of the things the stneas does. And I've spent several days looking over his approach.

    I have two difficulties with it. It's partial integration with an existing game framework, which makes it harder to isolate what the inventory system is doing per se, and my lack of coding (especially C#) skills. His use of data files and serialization is great! But I don't know enough to really bend it to my will and I'm not sure I can afford the time right now to get my head around it.

    I've solved some of the issues by using external tools. I have a FileMaker Pro database that can produce a text file that I can parse and load into an array using ExecuteInEditMode() to create an array of items. This is not as elegant as the in-editor solution stneas has done, but it does work.

    I'll be solving equipping and selling problems in some sort of manner, but I'm not sure how much drag and drop it will be. I have to keep in mind that my ultimate platform right now is the iPhone. As such, I have to be wary of heavy use of OnGUI() and Update(), both of which can kill the performance on the phone.
     
  13. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Had a PM about updates to this system. I don't have a time frame at the moment, as I don't feel I have a new version in a presentable fashion, and there is more to be done before it's really time for an update.

    On the other hand, I've mentioned some proto-code here in this thread where Der Dude's code was first posted.a

    This is the basis of the flat inventory array and code to wrap it into columns based on width.
     
  14. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hey little angel, I had a bit of a test with this tonight, lots of errors out of the box?

    Cheers
    Aaron
     

    Attached Files:

  15. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Very odd. I'm on a job most of this week, but I'll try to get a lunch break or evening to look at it on my laptop.

    Coming to this cold:
    Did you load this into an empty project or an existing project. I tested this in an empty project before posting it and it worked for me.

    Can you give me any more info on how you loaded it and used it?
     
  16. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hiya

    I tried it in a new project and had the same issues. If seen this "slicing" error before though I dont know what it means. Using Unity iPhone Basic 1.7.

    Cheers
    Aaron
     

    Attached Files:

  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ah! That's prolly my bad. I didn't try this explicity in iPhone 1.7, just 2.6. As this was using OnGUI(), I was planning to port this to SpritUI or EZGUI before deploying onto the iPhone for performance reasons. It should work on the iPhone just not as performantly. I'll power up 1.7 and give it a try and see if I can find the problem. Blindly, I can only think it could be a .net issue, as it's just OnGUI. The only big difference related to this I can think of between 2.6 1.7 is .net default. Try .net 2 in the project pref and see shat that does. More later.
     
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    First: I have to apologize for displaying this project in an iPhone graphic.
    This is part of my pipeline, and it can become quite invisible to me. My target platform is the iPhone, so I've got my entire pipeline setup for the iPhone as a target, but I'm developing the project in 2.6 for ease of use with add on packages like Aron Granberg's A* pathing project (http://forum.unity3d.com/viewtopic.php?p=333716) and Above Beyond Software's EZ GameSaver and EZ GUI (http://www.anbsoft.com/middleware/). I was also aware that my development schedule would include the upgrade to 3.0, so I was moving to the 2.6 editor to gain some familiarity with post 1.x Unity, as the 1.x Unity for iPhone would become obsolete.

    This project was not written explicitly for the iPhone. It was written generically for Unity and will require some work to bring it in line with iPhone standards and restrictions.

    Second: These changes (with the exception of the OnMouseDown() code from the wiki) will also work in Unity 2.6, and are a good change if you want to use a flat array. This will allow easy resizing of the inventory array.

    Third: I am working on an update to this package that includes the flat array, resizing the inventory array, proximity detection for the player and a few more things, but I can't say when that will be ready to post.

    -

    Using the package I have provided with the iPhone must require two important considerations:
    - Overall drawcalls. Using OnGUI() means that every button could be a new draw call, and with a 5x5 grid of buttons, that could mean up to 25 drawcalls just to display the inventory array.
    - OnMouseDown(). This package is based on the 3D object in the scene (say the chest) receiving an OnMouseDown() event. The iPhone does not support OnMouseDown(). This is one work-around: (http://www.unifycommunity.com/wiki/index.php?title=OnMouseDown) Otherwise you'll have to write a ray cast detector looking for colliders in your scene. You can use the layer mask to keep them isolated to just your lootable objects layer.

    -

    I had a quick look on my laptop, and you are correct, that 1.7 does not like the 2D array used in drawing the inventory window.

    The following chunk of code from OnGUI in the current version that you have is based on a 2D array. You can tell it's a 2D array when you see items like:
    (You can find out more about them here:
    http://www.unifycommunity.com/wiki/...Of_Array_Or_Collection_Should_I_Use?#2D_Array)

    Code (csharp):
    1.         for (var i : int = 0; i < inventory.length; i ++) {
    2.             for (var k : int = 0; k < inventory[i].length; k ++) {
    3.                 currentInventoryItem = inventory[i][k];
    4.                 if (inventory[i][k] == null) { etc, etc }
    I've changed this from a 2D array to a single flat array with code that displays the flat array in rows and columns:

    Add these to the top of the script.

    Code (csharp):
    1. var inventoryWidth : int;       //  Add this to the top of the script
    2. var inventoryLength : int;      //  Add this to the top of the script
    You can delete or comment out these two variables, as they are no longer applicable:
    Code (csharp):
    1. //var inventorySizeX : int;                                             //  the size of the inventory in x and y dimension
    2. //var inventorySizeY : int;
    Change the initialization of the array in function Start(); to be relevant to a flat array:

    Code (csharp):
    1.     inventory = new Array (inventoryLength);                                //  Create  init the array to hold the inventory
    2.     for (var i : int = 0; i < inventory.length; i++) {
    3.         inventory[i] = null;
    4.     }
    Replace OnGUI with this (This is the entire OnGUI to replace...):

    Code (csharp):
    1. function OnGUI () {
    2.     //  Loot Window
    3.     if (openLootWindow) {                                               //  If the "open loot window" toggle is true
    4.         GUI.Window (0, lootWindow, DrawLootWindow, "Loot");             //  The title of this window could be passed as a String from the LootableItem
    5.     }
    6.    
    7.     //  Inventory Window
    8.     if (openInventory) {                                                //  If the "open inventory window" toggle is true
    9.         var j : int;
    10.         var k : int;
    11.         var currentInventoryItem : InventoryItem;                           //  Establish a variable to hold our data
    12.         var currentRect : Rect;
    13.         for (var i : int = 0; i < inventory.length; i ++) {                 //  Go through each row ...
    14.             j = i / inventoryWidth;                                         //  ... divide by array by width to get rows...
    15.             k = i % inventoryWidth;                                         //  ... find the remainder by width to get columns...
    16.             currentInventoryItem = inventory[i];                            //  ... set this point in the matrix as our current point ...
    17.             currentRect = (new Rect (offSet.x + k * (iconWidthHeight + spacing), offSet.y + j * (iconWidthHeight + spacing), iconWidthHeight, iconWidthHeight));
    18.             if (currentInventoryItem == null) {                             //  ... if there is no item in the j-th row and the k-th column, draw a blank texture
    19.                 GUI.DrawTexture (currentRect, emptySlot);
    20.             } else {
    21.                 GUI.DrawTexture (currentRect, currentInventoryItem.itemIcon);
    22.             }
    23.            
    24.             //  If there is an item at this location and there is a button click...
    25.             if (currentInventoryItem != null  GUI.Button (currentRect, "", GUIStyle ("label"))) {
    26.                 if (Input.GetMouseButtonUp (0)) {                   //  ... if that click is mouse button 0: equip it.
    27.                     //  Equip it
    28.                 } else if (Input.GetMouseButtonUp (1)) { // ... if that click is mouse button 1: delete it.
    29. //                  //  Drop it
    30.                     Debug.Log ("Destroying " + inventory[i][k].itemName);
    31.                     inventory[i] = null;
    32.                 }
    33.             }
    34.         }
    35.     }
    36. }
    You must also change function AddItem to this:

    Code (csharp):
    1. function AddItem (item : InventoryItem) {
    2.     for (var i : int = 0; i < inventory.length; i ++) {
    3.         if (inventory[i] == null) {
    4.             inventory[i] = item;
    5.             return;
    6.         }
    7.     }
    8.     Debug.Log ("Inventory is full");
    9. }
    This will allow you to compile successfully in 1.7. You will have to add the OnMouseDown() fix to get the events to work correctly. Beware of your drawcalls. To keep drawcalls to a minimum on the iPhone, you should look at one of the Sprite based GUI solutions like SpriteUI (Free and Relatively Straightforward: http://forum.unity3d.com/viewtopic.php?t=22371) or EZGUI (Inexpensive and Powerful: http://forum.unity3d.com/viewtopic.php?p=333789). These take advantage of atlases and materials batching to reduced drawcalls. The underlying logic in the code that I have presented here and in the original package will be correct, it's just how you display it that will need changing.
     
  19. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well - ok. I stayed up late and pushed out the update.

    AaronC: This now works out of the box on the iPhone with 1.7. You just need to disable one function Update and unquote one short script.*



    You can see it in action here:
    http://www.theantranch.com/Inventory.html

    There are at least two bits of code that do not show here. First is the "test distance" function that first check to see if the player is close enough to use the object, and then closes the relevant windows if the player moves out of range - this is due to the static camera in the test scene. If you implement this package in a 3d environment, it will work. Second is the function that resizes the inventory array... as this needs to be integrated into your game, it's a stand-alone function that can be called when necessary. Please note it expects the inventory to grow not shrink, and more attention will have to be made by an author who wants to shrink an inventory - as there needs to be code dealing with any objects in the array that are in slots larger then the new inventory length.

    This update includes the flat array, resizing the inventory array, proximity detection for the player, Equipping and Un-equipping, inventory slot recognition - and some other bits and pieces.

    For more information and packages go here:
    http://theantranch.com/Unity/Entries/2010/5/17_Basic_Inventory_&_Looting_System.html

    * for Unity iPhone see the last note on the above page. It is surprisingly more performant that I would have thought, but still, I'd suggest a sprite-base UI system for displaying the inventory on the iPhone.
     
  20. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Hmm ok, I downloaded the package but it appears to be unchanged. I tried pasting in all the above stuff, but its still not compiling. Thanks though.

    Also a couple of notes. Because of the way you archived the unityPackage, it required a special unarchiver to be opened on Mac. If you open it with the builtin mac unarchiver, the compression format of the zip conflicts with the compression format of the unityPackage and its unusable. To get around this, put your unity package in a folder first, and zip the folder. Another thing is that firefox is saying a popup wants to open on your site, and yesterday when I went to close the page, using cmd+w, it refused to close, and for some reason in the backround I had downloaded the zip file 26 times. Not sure whats up there.

    I'll keep an eye on this thread though, but for now I give up.

    Cheers
    AC
     
  21. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Very very strange.

    I tested it last night, and just tested it again.

    I'm using Safari on Snow Leopard... I didn't test any other browsers or operating systems.

    I can d/l the file from the link on this page:
    http://theantranch.com/Unity/Entries/2010/5/17_Basic_Inventory_&_Looting_System.html

    I can import into both Unity 2.6 and Unity iPhone 1.7. They run in the editor in both, but I only used the remote this morning (whereas last night I tested with a compile to the iPhone).

    Can't say what your problem is.

    Try another browser? Make sure you're not opening the old version. The old version is 632k and the new one is 837k. They ARE named the same, as I didn't want to stay up and reprogram all the links on the web page. Check to see if you've got a file called InventoryAndLootSystem.untitypackage_1 or something. Check the file size.

    Also, they unpacked fine automatically using Safari, so I can't tell what the issue is there. All of my zips, and the last one iirc, were done the same.

    The only other detectable problem is that Unity iPhone 1.7 oddly doesn't like the "EmptySlot.tif" and I changed it to a .png file to get it to work, but didn't troubleshoot the reason why. I thought I'd updated the package when I found the issue last night, but I missed it. (var mySleep : float < 4.0)

    Lastly, if you take the original version and replace all of the text as above in the post, it should work fine. I took a fresh version and followed my own instructions...

    Could there be something else causing a road bump?

    Sorry I can't be much more help! Provide more details, and I'll see what I can do. This week is a busy week, tho'.
     
  22. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok so I found that by emptying my browser cache I could download the proper zip. Double clicking it still opens it as dozens of folders with garbled names though. This is a well documented problem however, and if you put the package in a folder and zip the folder its avoidable. For people that experience this problem you can get around it by using the "Unarchiver" program from

    http://wakaba.c3.cx/s/apps/unarchiver.html

    Using it by default, I found no mouseclicks worked in the scene, the only functionality in the scene was the "i" key toggling the inventory open and closed.

    So I tried to copy the changes in as you pointed out above, and got the errors as pictured.

    I think at line 98 you need to change

    if (openInventory) {

    to

    if (openInventoryWindow) {

    to match up with your naming conventions.

    line 120 throws the "Doesnt support slicing" error which I'm totally screwed on:
    Debug.Log ("Destroying " + inventory[k].itemName);

    And line 204 also throws an error:
    var success : boolean = AddItem (equipped); // Remove/Unequip it.

    Assets/Inventory/Scripts/Inventory.js(204,73): BCE0022: Cannot convert 'void' to 'boolean'.
    (I wouldn't know where to start with an error like that)

    Are you sure this works? Ive tried it in an existing project and as standalone. Same problems in both. (Both Unity iPhone)

    The download link I'm using is
    http://theantranch.com/tutorialsupport/downloads/InventoryAndLoot/InventoryAndLootSystem.zip

    Do let everyone know if you decide to post an iPhone version that works and doesnt have errors or need other demystifying. In an above post you wrote:

    "You will have to add the OnMouseDown() fix to get the events to work correctly."

    eg I dont know what you're referring to here, theres no OnMouseDown() in the Unity iPhone world I don't think.

    Its such a good example it seems a tease to share it when its like it is. Its kinda like a problem solving exercise rather than an example project. Try and keep it simple for the stupid people. The texture was corrupted here also.

    Cheers
    AC
     

    Attached Files:

  23. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Quickly, as I'm on set between shots and responding on my iPhone without my computer in front of me:

    I'll put it in a folder when I get the chance, but I can't test and optimise for every system and every browser. The archive and download work fine with snow leopard and safari. If you have not deleted safari from your system, and are working on a mac, it might be easier to just use safari rather than find a specialized un-archive app to get around a browser incompatibility.

    The suggested code replacements were to get you up and running with the previous version of the package. The current version of the package needs no modification. The only changes you need to do are change the ones to activate the onmousedown/ontouchdown code that is specific to the iPhone.

    My shot's up next, so I'll think some more about it. Unfrtinately, it
    seems to work for ithersat first blush.
     
  24. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Cool just to be clear the current version didnt respond to mouse clicking in the unity editor, so forget touch input for a second, clicking on the chests didnt do anything- not here at least.

    Thanks for the other info

    Aaron
     
  25. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    OK. Lunch! (Yeah for wifi on this stage!)

    The only thing I can say is: "It's on my phone and it's working."

    For use in 2.6, all you need to do is download.

    For use in 1.7, all you need to do is download, remove the function Update () from inventory.js and both activate and un-comment OnTouchDown.js, which you will find on the main camera.

    Don't forget that OnMouseDown() does not work on the iPhone, so *it has been eliminated from the editor code in all iPhone versions of Unity*. Clicking won't work in 1.7 at all unless you enable OnTouchDown.js and use the remote.

    This is a clue:
    Code (csharp):
    1. Debug.Log ("Destroying " + inventory[i][k].itemName);
    This code should not exist in the new package. There might be some confusion about which package you are using.

    There is no use of a 2D Array anywhere in the new code. So no [ i ][ k ] anywhere in the code! That's gotta be a mistaken bit from the original package. I've checked the current inventory.js searching for any remnant of Der Dude's 2D Array and I can't find any. I've searched for the double brackets ][ and i and k: nothing.

    So - there's got to be some mix up over there with which package you are using.

    Clear you cache again, and surf to this package:

    http://theantranch.com/tutorialsupport/downloads/InventoryAndLoot/InventoryAndLootSystem1.1.zip

    This is the exact same project with a new version number, just to be sure. The only change I've made, is that I've saved the EmptySlot.tif to EmptySlot.png, as that .tif file was not playing well with the 1.7 texture importer and I'd already made the change. Why doesn't 1.7 like it? No clue. Go figure.

    The only think you want to do for the iPhone with this file, other than import the asset package, is:

    Code (csharp):
    1.    1:   Open Inventory.js and comment out or delete function Update (){}. The only contents of this function should be scanning for input for the keystroke "i". This is irrelevant on the iPhone.
    2.  
    3.    2:   Open OnTouchDown.js and un-comment the only function within this script:
    4.  
    5. //  OnTouchDown.js
    6. //  Allows "OnMouseDown()" events to work on the iPhone.
    7. //  Attach to the main camera.
    8.  
    9. function Update () {
    10.     // Code for OnMouseDown in the iPhone. Uncomment this code to use.
    11.     var hit : RaycastHit;
    12.     for (var i = 0; i < iPhoneInput.touchCount; ++i) {
    13.         if (iPhoneInput.GetTouch(i).phase == iPhoneTouchPhase.Began) {
    14.         // Construct a ray from the current touch coordinates
    15.         var ray = camera.ScreenPointToRay (iPhoneInput.GetTouch(i).position);
    16.         if (Physics.Raycast (ray,hit)) {
    17.             hit.transform.gameObject.SendMessage("OnMouseDown");
    18.           }
    19.        }
    20.    }
    21. }
    22.  
    23. //  From the Unify Wiki
    24. //  [url]http://www.unifycommunity.com/wiki/index.php?title=OnMouseDown[/url]
    25.  
    26.    3:   Go to the MainCamera in the hierarchy and enable/activate the script OnTouchDown.js
    27.  
    28.    4:   There is no step 4.
    This will run in the editor with the remote (if it's not being too flakey), and will compile and run on your phone.

    This will work for both:

    http://theantranch.com/tutorialsupport/downloads/InventoryAndLoot/InventoryAndLootSystem.zip

    http://theantranch.com/tutorialsupport/downloads/InventoryAndLoot/InventoryAndLootSystem1.1.zip

    Download the 1.1 just to make sure nothing is being confused or over-written.

    I've retested this in both 2.6 and 1.7. They work fine in both environments.

    Screenies from my 3G:




    Unfortunately - no nice shadows on the floor like the ones seen in the webplayer... :-(
     
  26. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok thanks for that, I can get it working using a combination of mouseclicks and unity remote now. If I follow those instructions I can open with touch but cannot equip with touch-it just closes the window.

    EDIT - Ah I see, a texture was detached messing it up. Yay it works!

    thanks for your time.

    Cheers
    AC
     
  27. sharp911

    sharp911

    Joined:
    Apr 16, 2010
    Posts:
    28
    I downloaded this example and Im getting following error:
    How do I fix this, Ive checked spelling errors and other stuff, but cant figure out what is wrong.
     
  28. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Sharp911: (Sorry for not responding earlier, but this forum is still CRAP at notifying users about new posts to subscribed threads...)

    Just downloading this and running it in the editor? Odd. I've not tried this in 3.0, but it should still run out of the box... Let me look into this, and if you've not heard from me, send me a PM to light a fire. I can't get to it today, but I'll try for tomorrow.
     
  29. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The problem lies in Line 32 of LootableObject.js

    For some reason you need to cast the type in 3.0, even in Unity.js... Go figure.

    Line 32 works when you cast the type like this:
    Code (csharp):
    1. lootTable = FindObjectOfType (LootTable) as LootTable;  //  Create a reference to the Loot Table
    For what reason you'd need to cast the type in a FindObjectOfType in Unity.js, I have no clue. There are language updates associated with 3.0, but this is new to me...

    Huh...

    But that gets it to work!

    [edit]

    I just wanted to point out to anyone downloading this package that the LootTable is a temporary way of driving the display system, which was the main point to this exercise. My actual method is to create a new loot table associated with each lootable object and have each lootable object supply the individual loot list from its own loot table to the player. With prefabs (Chest, Monster Type, etc.) you can be pretty efficient with a loot table on each object, rather than a master loot table that each lootable object must test or poll for it's loot list.

    [/edit]
     
    Last edited: Nov 10, 2010
  30. sharp911

    sharp911

    Joined:
    Apr 16, 2010
    Posts:
    28
    Thanks Little Angel

    After playing with the scene for a few min, I realized that loot items got deleted if inventory was full, so I changed the code a little by replacing this code in inventory.js line 116



    with this



    so now the loot item will not get deleted if your inventory is full.
     
  31. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Yes. There are a number of traps and polish the system needs to work efficiently. Its really just a base to get started with.
     
  32. AtomicMarine

    AtomicMarine

    Joined:
    Nov 7, 2010
    Posts:
    53
    I love u man. this has helped me 500000%
    yes 500000%

    :) :) :) :) :) :)

    Brilliant system

    I converted it to work with iPhone for what I want to do, and everything went fine :)

    did my conversion with colliders instead of rays but it still works great
     
  33. AtomicMarine

    AtomicMarine

    Joined:
    Nov 7, 2010
    Posts:
    53
    I just have one question, How does one assign characteristics to the various objects the player will equip
    for example if I equip a sword that does 5 dmg, how would I increase my player damage when that sword is equipped?
    what would the script for that be?
     
  34. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    The actual system for adding characteristics to an item is not present in this code set, but it could easily be implemented. I would take the (iirc) InventoryItem class and add characteristics to it. Then, when looking at your character, you could iterate through the Equipped array and add all the characteristics to your player class.
     
  35. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    On another note, this system has gone a bit stale. It was written for 2.6 and could be updated to 3.x. It could easily be fixed up with a quest system and other add ons.

    To do this, I'd have to get a return on the investment, as I don't need this system right now.

    Would anyone be interested if I were to update this code and put it up on the asset store for an affordable price?

    Let me know.

    Otherwise, I'll just keep answering posts here.
     
  36. AtomicMarine

    AtomicMarine

    Joined:
    Nov 7, 2010
    Posts:
    53
    @Little angel, I would be interested if u were to update the system for the iOS as opposed to Unity3 as that is what I currently need it for :p haha

    Thanks :)
     
  37. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Atomicdomb:

    Part of the reason this went stale, is that I was worried that this system would not be performant enough during run-time on an iOS device. I found that the system in isolation ran just fine, but I was worried that in the context of a real game, too many resources would be used by OnGUI and the high number of drawcalls required. With that in mind, I created a new inventory and looting system in A&BSoft's EZGui.

    I would suggest that if you are really looking for a performant system for the iOS, you look at EZGui. Unfortunately, I don't have a complete Inventory system for distribution like this one for basic U2.x/U3.x.
     
  38. AtomicMarine

    AtomicMarine

    Joined:
    Nov 7, 2010
    Posts:
    53
    Does this EZGui work well with the IOS?

    if one is wanting to specialize in RPG's is it worth it? the 200$ that is for the EZGui?
    and how well did your Inventory system work with their system?
     
  39. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I had to rewrite the entire system from the ground up, as the analogy is completely different. OnGUI iterates through every image several times a frame, and each image is it's own drawcall to the GPU. This give OnGUI a great flexibility. It also uses a lot of resources. EZGui is based on sprites and sprite atlases. This means there is more up front decisions that need to be made, and a more complicated system of saving and moving the icon images around. But - this saves on internal resources and draw calls, making the entire system much more efficient.

    Does EZGui work well with iOS. Oh, yes! It it worth $200? Oh, yes! Is my inventory system working? Yes! But this OnGUI system here in this thread could only be used as a template for the logic, and all of the systems would need to be re-worked to function under EZGui. Some of the builtin functions with EZGui include touch - scrolling and animated transitions, which normally you'd need to code by hand. I have a touch scroller I've written for OnGUI (see my .sig for the link) but EZGui has that built in!

    That being said, currently there are no available inventory systems on the wiki or the forum that uses EZGui that you could just pick up and use. You'd have to roll your own.
     
  40. AtomicMarine

    AtomicMarine

    Joined:
    Nov 7, 2010
    Posts:
    53
    Hmm thanks I am considering writing my own with EZGui, but I would really like to test it out before putting down the cash :/ oh well. However I have been playing with your system you wrote with the OnGui, and it seems to work really great in the context of my game.

    The Draw calls are high but don't seem to affect the gameplay negatively and with some slight adaptions( like giving the items their owns stats like damage and requirements) I have found the system to be very effective.

    I don't know how daunting the task of re writing the system on EZGui would be, roughly how many days work was it for you out of interest?
    I say this because I have spent quite a bit of time editing your system to work well within the context of my game. Currently I would say it would take probably 2 more days of coding and trouble shooting to have ur system fully functional within my game (thats with loot drops which will depend on the monsters u kill and equipped items affecting the character)

    So my question to you is, do you think it is worth creating a new system to reduce draw calls? despite the fact that even with the high draw calls the game seems to run flawlessly?
     
  41. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm about to release a v2.0 of this system, probably thru the asset store. I'm curious what functionality people are still looking for, or functionality that people have added that must be in a new system.

    Currently I've added updates like:

    - An item editor window to add items and edit their attributes in edit-time.
    - - No parsing text files from external sources.
    - LootableObject based loot tables that are both fully editable and prefabbable.
    - - Multiple loot tables per object supported for organization and clarity.
    - - No need for one long annoying set of centralized loot tables as was temped in 1.0.
    - Instantiating objects removed from the inventory back into the game world.
    - Developer ready "Use Item" methods for using consumable items (potions, etc.).
    - Developer ready access to the items equipped for character instantiation.
    - Accounting for stat bouses on equipped items when items are equipped and removed.
    - Optionally Destroy lootable objects when empty.
    - - With an optional fade out before destroying check box.
    - Generally cleaner and quicker.
    - More things that I can't think of at the moment...

    There will be a full set of tutorial videos on setting up and working with the system.
    The code will be available in both Unity.js and C#.
    The code will be annotated and understandable.

    In addition - a set of temp icons, a chest model, sprites, textures and other items will be distributed with the demo-project.

    In the future, there could be a companion "quest" system that interfaces with the items in the inventory.

    What more would you need? What more would you want?

    Sneak Peek:
     
  42. brendonnelly

    brendonnelly

    Joined:
    Aug 5, 2010
    Posts:
    39
    Sorry for the slight resurrection, I was just wondering what our progress was on this, and if it would support stacking.
     
  43. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Inventory 2.0 is in the queue at the asset store:


    At this point, it supports everything seen in the Sneak Peek video - including instantiation and fading, multiple loot tables, de-centralized spawning and even some bells and whistles like an Inventory Item Editor:


    Features in the pipe include multiple bags and stacking, which I have partially implemented in an internal version, but I wanted to get the base system out to folks as soon as possible.

    The package will also include some models, textures, animations and icons... just to get the new user up and running.

    -

    ps: This package will be available in both C# and JS
     
  44. Hans

    Hans

    Joined:
    Feb 20, 2007
    Posts:
    422
    Are we there yet ?
     
  45. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    We wait for the approval of the gods. I continue to improve bits and pieces anyway. Soon, the runes say. So soon.
     
  46. Hans

    Hans

    Joined:
    Feb 20, 2007
    Posts:
    422
    still not there after 6 days........
     
  47. Frank Oz

    Frank Oz

    Joined:
    Oct 13, 2010
    Posts:
    1,560
    This is looking awesome, nice work indeed!
     
  48. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Well, MessageManager (http://theantranch.com/MessageManager.html) was just approved and had been sitting in the queue long enough for me to finish this update! I'll put it up on esellerate soon if it's not up on the Asset Store soon.
     
  49. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I withdrew this from the asset store. It seemed to have been lost in an eddy somewhere. MessageManager did get posted, but I realized that my internal version was so far ahead of the one on the asset store, it wasn't worth keeping it in the queue.

    Here is the most recent preview:


    You can see that it has true multiple bags, tooltips/info, drag drop with swapping, and more.
     
  50. afalk

    afalk

    Joined:
    Jun 21, 2010
    Posts:
    164
    This looks fantastic! how hard would it be to set up a store-front/merchant with your code base at its center?