Search Unity

Need working example of loading an object array

Discussion in 'Scripting' started by zircher, Nov 29, 2012.

  1. zircher

    zircher

    Joined:
    Apr 9, 2011
    Posts:
    65
    To be more precise, using the LoadAll method in javascript to populate an object array and then loop through that object array to populate an array of textures so I can plug that in to the selection grid GUI. I tried creating one from scratch and beat my head on it for a few days without success. I really learn better from examples and I couldn't find anything that did this.
    --
    Thanks for reading this, TAZ
     
  2. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    In your case you should Create a Custom Dictionary which will hold your custom data. I am not good in Javascript with Unity, So i am writing a concept in C#.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collection.Generic;
    5.  
    6. [Serializable]
    7. public class MyCustomDataDictionary
    8. {
    9. public int index;
    10. public Texture texture;
    11. //Add other data here
    12. }
    13.  
    In your class populate data in below way:

    Code (csharp):
    1.  
    2. List<MyCustomDataDictionary> customDataDictList = new List<MyCustomDataDictionary>();
    3.  
    4. MyCustomDataDictionary myCustomDataObj = new MyCustomDataDictionary();
    5. myCustomDataObj.index = 10;
    6. myCustomDataObj.texture = texture; // with some texture;
    7.  
    8. customDataDictList.add(myCustomDataObj);    //Add new Object to List
    9.  
    After this you need to extract all Textures to use in GUI SelectionGrid. Below is a manual way, but i was wondering if somebody here could teach us a better way to do below code.
    Code (csharp):
    1.  
    2. Texture[] textureList  = new Texture[customDataDictList.count];
    3. foreach(MyCustomDataDictionary myCustomDataTempObj in customDataDictList )
    4. {
    5. textureList.add(myCustomDataTempObj.texture);
    6. }
    7.  

    And Now you are ready to Use textureList in GUI Selection Grid. Hope this Helps..
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Erm - a lot of that code is incorrect. Not sure if your goal was to write workable code or just show an example....

    Firstly - what you call a Dictionary is most certainly not a Dictionary - it's a List.
    Secondly - System.Array doesn't have an add method.
    Thirdly - List does not have a method called add - it's actually Add.

    If OP is referencing Resources.LoadAll then he'll have to be a bit more specific about his goal. LoadAll returns an object array which you can enumerate etc straight away. Are you loading an array of Texture2D objects wit this or something else?
     
  4. MicroEyes

    MicroEyes

    Joined:
    Jul 3, 2012
    Posts:
    309
    Very Nice KelsoMRK,
    As i wrote in my first line its just a Concept in C# not a working example.
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    No worries - just wanted to clear any confusion that might occur.
     
  6. zircher

    zircher

    Joined:
    Apr 9, 2011
    Posts:
    65
    Correct, the plan/attempt that I tried was to have a sub folder under assets that had all the images that I wanted to load. Then use Resources.LoadAll to grab those images, move them from the object array to a texture or content array, and use that with the selection grid UI. My efforts have been less than stellar. Instead of saying 'fix my code' I was hoping to see how other people have solved this so I can learn by example.
    --
    TAZ
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You're on the right track so I would say post code and explain specific issues that you're running into.
     
  8. zircher

    zircher

    Joined:
    Apr 9, 2011
    Posts:
    65
    Well, I poked around and got rid of one error, so now I only get a warning. Screen shot here:

    http://i265.photobucket.com/albums/ii221/zircher/screen_00.png

    It looks like LoadAll is returning zero objects, the screen shot shows that Unity is seeing the images and I included a window that shows the path as well.

    The codez: :D

    Code (csharp):
    1. #pragma strict
    2.  
    3. var selTexture : Texture2D[];
    4. var selObjects : Object[];
    5.  
    6. var windowRect0 : Rect = Rect (20, 20, 400, 400);   // DAT
    7. var windowRect1 : Rect = Rect (440, 20, 100, 200);  // Values
    8. var windowRect3 : Rect = Rect (440, 240, 100, 240); // Turret
    9. var windowRect4 : Rect = Rect (440, 500, 100, 200); // System
    10. var windowRect5 : Rect = Rect (560, 20, 100, 460);  // Text
    11. var windowRect6 : Rect = Rect (680, 20, 100, 300);  // Select
    12. var windowRect7 : Rect = Rect (680, 340, 100, 200); // Control
    13.  
    14. var selectedIcon : int = 0;
    15. var selectedStrings : String[] = ["00", "01", "02", "03","04", "05", "06", "07","08", "09", "10", "11","12", "13", "14", "15","16", "17", "18", "19"];
    16. var statusMessage : String;
    17. var unitText : String;
    18.  
    19. function Awake() {
    20.   statusMessage = "This is Awake.";
    21. }
    22.  
    23. function Start () {
    24.   var a : int;
    25.   var b : int;
    26.  
    27.   selObjects = new Resources.LoadAll("textures/icons", Texture2D);
    28.  
    29.   selObjects.GetLength(a);
    30.  
    31.   selTexture = new Texture2D[a];
    32.  
    33.   statusMessage = "Objects loaded: " + a;
    34.  
    35.   for(b = 0; b < a; b++) {
    36.     selTexture[b] = selObjects[b];
    37.   }
    38.   statusMessage = "Textures loaded: " + selTexture.Length;
    39. }
    40.  
    41. function Update () {
    42. }
    43.  
    44. function OnGUI () {
    45.   // Register the windows
    46.   windowRect0 = GUI.Window (0, windowRect0, makeWindowDAT, "DAT Window");
    47.   windowRect1 = GUI.Window (1, windowRect1, makeWindowValues, "Info");
    48.   windowRect3 = GUI.Window (3, windowRect3, makeWindowTurret, "Turret");
    49.   windowRect4 = GUI.Window (4, windowRect4, makeWindowSystem, "System");
    50.   windowRect5 = GUI.Window (5, windowRect5, makeWindowText, "Text");
    51.   windowRect6 = GUI.Window (6, windowRect6, makeWindowSelect, "Select Icon");
    52.   windowRect7 = GUI.Window (7, windowRect7, makeWindowControl, "Controls");
    53.  
    54.   GUI.Label (Rect (20, 440, 400, 20), statusMessage);
    55. }
    56.  
    57. // Make the contents of the window
    58. function makeWindowDAT (windowID : int) {
    59.   GUI.DragWindow (Rect (0,0, 10000, 20));
    60.   if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    61.     statusMessage  = "Got a click in DAT.";
    62. }
    63.  
    64.  
    65. function makeWindowValues (windowID : int) {
    66.   GUI.DragWindow (Rect (0,0, 10000, 20));
    67.   if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    68.     statusMessage  = "Got a click in Values.";
    69. }
    70.  
    71. function makeWindowTurret (windowID : int) {
    72.   GUI.DragWindow (Rect (0,0, 10000, 20));
    73.   if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    74.     statusMessage  = "Got a click in Turret.";
    75. }
    76.  
    77. function makeWindowSystem (windowID : int) {
    78.   GUI.DragWindow (Rect (0,0, 10000, 20));
    79.   if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    80.     statusMessage  = "Got a click in System.";
    81. /*
    82. Green
    83. Regular
    84. Veteran
    85. Elite
    86. Battle Computers
    87. Burrrowing
    88. Cloaking
    89. Crash Built
    90. Photovore
    91. Sentient
    92. Stealth
    93. Submerged
    94. Submersible
    95. Wild
    96. Equipment
    97. */
    98. }
    99.  
    100. function makeWindowText (windowID : int) {
    101.   GUI.DragWindow (Rect (0,0, 10000, 20));
    102.   unitText = GUI.TextArea (Rect (10, 20, 80, 420), unitText);
    103. }
    104.  
    105. function makeWindowSelect (windowID : int) {
    106.   GUI.DragWindow (Rect (0,0, 10000, 20));
    107.   selectedIcon = GUI.SelectionGrid (Rect (10, 20, 80, 250), selectedIcon, selTexture, 2);
    108.   if (GUI.Button (Rect (20,280,20,20), "<-"))
    109.     statusMessage  = "selected page left";
    110.   if (GUI.Button (Rect (60,280,20,20), "->"))
    111.     statusMessage  = "selected page right";    
    112. }
    113.  
    114. function makeWindowControl (windowID : int) {
    115.   GUI.DragWindow (Rect (0,0, 10000, 20));
    116.   if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    117.     statusMessage  = "Got a click in Control.";
    118. /*
    119. nudge arrows
    120. buttons; load, save, print, config, help, batch
    121. [] hide lables
    122. [] configure labels
    123. [] short DAT   // text field for bottom label //
    124. [] long DAT
    125. [] force print to file
    126. button, load custom background
    127. button, select new font for data and labels
    128. button, load custom set of icons
    129. */
    130. }
    131.  
    132. @script ExecuteInEditMode() // display GUI in editor
    133.  
    --
    TAZ
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    It's returning 0 items because your texture folder isn't in a folder called Resources. You're getting the warning because you're not explicitly casting selObjects[n] to a Texture2D before adding to the Texture2D array.

    Code (csharp):
    1.  
    2. Object[] selObjects = Resources.LoadAll(...);
    3. Texture2D[] selTexture = new Texture2D[selObjects.Length];
    4. for (var i = 0; i < selObjects.Length; i++)
    5.     selTexture[i] = selObjects[i] as Texture2D;
    6.  
     
  10. zircher

    zircher

    Joined:
    Apr 9, 2011
    Posts:
    65
    Cool, I was wondering how to do casting in javascript. The docs said to place the image under the assets folder, we'll give resources a try.

    Thanks for looks at this.
    --
    TAZ
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    http://docs.unity3d.com/Documentation/ScriptReference/Resources.LoadAll.html

    Not entirely clear - but yes there has to be a folder named Resources somewhere in your project. :)

    My code is C# but the casting is the same. There are 2 methods.
    Code (csharp):
    1.  
    2. Texture2D tex = obj as Texture2D; // makes tex null if cast fails
    3.  
    4. Texture2D tex = (Texture2D)obj; // throws exception if cast fails
    5.  
     
  12. zircher

    zircher

    Joined:
    Apr 9, 2011
    Posts:
    65
    Gah! That's why every entry needs example code. That was way way too easy to miss.

    Moved the folder under Assets/Resources and changed this line...

    a = selObjects.Length;
    // selObjects.GetLength(a);

    And, VICTORY! I has icons. [Now to play with the style settings.]

    Thank you for being patient and pointing out the not so obvious.
    --
    TAZ