Need working example of loading an object array

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

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#.

using UnityEngine;
using System;
using System.Collection.Generic;

[Serializable]
public class MyCustomDataDictionary
{
public int index;
public Texture texture;
//Add other data here
}

In your class populate data in below way:

List<MyCustomDataDictionary> customDataDictList = new List<MyCustomDataDictionary>();

MyCustomDataDictionary myCustomDataObj = new MyCustomDataDictionary();
myCustomDataObj.index = 10;
myCustomDataObj.texture = texture; // with some texture;

customDataDictList.add(myCustomDataObj);    //Add new Object to List

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.

Texture[] textureList  = new Texture[customDataDictList.count];
foreach(MyCustomDataDictionary myCustomDataTempObj in customDataDictList )
{
textureList.add(myCustomDataTempObj.texture);
}

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

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?

Very Nice KelsoMRK,
As i wrote in my first line its just a Concept in C# not a working example.

No worries - just wanted to clear any confusion that might occur.

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

You’re on the right track so I would say post code and explain specific issues that you’re running into.

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: :smile:

#pragma strict

var selTexture : Texture2D[];
var selObjects : Object[];

var windowRect0 : Rect = Rect (20, 20, 400, 400);   // DAT
var windowRect1 : Rect = Rect (440, 20, 100, 200);  // Values
var windowRect3 : Rect = Rect (440, 240, 100, 240); // Turret
var windowRect4 : Rect = Rect (440, 500, 100, 200); // System
var windowRect5 : Rect = Rect (560, 20, 100, 460);  // Text
var windowRect6 : Rect = Rect (680, 20, 100, 300);  // Select
var windowRect7 : Rect = Rect (680, 340, 100, 200); // Control

var selectedIcon : int = 0;
var selectedStrings : String[] = ["00", "01", "02", "03","04", "05", "06", "07","08", "09", "10", "11","12", "13", "14", "15","16", "17", "18", "19"];
var statusMessage : String;
var unitText : String;

function Awake() {
  statusMessage = "This is Awake.";
}

function Start () {
  var a : int;
  var b : int;
  
  selObjects = new Resources.LoadAll("textures/icons", Texture2D);
  
  selObjects.GetLength(a);
  
  selTexture = new Texture2D[a];
  
  statusMessage = "Objects loaded: " + a;

  for(b = 0; b < a; b++) {
    selTexture[b] = selObjects[b];
  }
  statusMessage = "Textures loaded: " + selTexture.Length;
}

function Update () {
}

function OnGUI () {
  // Register the windows
  windowRect0 = GUI.Window (0, windowRect0, makeWindowDAT, "DAT Window");
  windowRect1 = GUI.Window (1, windowRect1, makeWindowValues, "Info");
  windowRect3 = GUI.Window (3, windowRect3, makeWindowTurret, "Turret");
  windowRect4 = GUI.Window (4, windowRect4, makeWindowSystem, "System");
  windowRect5 = GUI.Window (5, windowRect5, makeWindowText, "Text");
  windowRect6 = GUI.Window (6, windowRect6, makeWindowSelect, "Select Icon");
  windowRect7 = GUI.Window (7, windowRect7, makeWindowControl, "Controls");

  GUI.Label (Rect (20, 440, 400, 20), statusMessage);
}

// Make the contents of the window
function makeWindowDAT (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    statusMessage  = "Got a click in DAT.";
}


function makeWindowValues (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    statusMessage  = "Got a click in Values.";
}

function makeWindowTurret (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    statusMessage  = "Got a click in Turret.";
}

function makeWindowSystem (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    statusMessage  = "Got a click in System.";
/*
Green
Regular
Veteran
Elite
Battle Computers
Burrrowing
Cloaking
Crash Built
Photovore
Sentient
Stealth
Submerged
Submersible
Wild
Equipment
*/
}

function makeWindowText (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  unitText = GUI.TextArea (Rect (10, 20, 80, 420), unitText);
}

function makeWindowSelect (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  selectedIcon = GUI.SelectionGrid (Rect (10, 20, 80, 250), selectedIcon, selTexture, 2);
  if (GUI.Button (Rect (20,280,20,20), "<-"))
    statusMessage  = "selected page left";
  if (GUI.Button (Rect (60,280,20,20), "->"))
    statusMessage  = "selected page right";    
}

function makeWindowControl (windowID : int) {
  GUI.DragWindow (Rect (0,0, 10000, 20));
  if (GUI.Button (Rect (10,20,60,20), "Hello World"))
    statusMessage  = "Got a click in Control.";
/*
nudge arrows
buttons; load, save, print, config, help, batch
[] hide lables
[] configure labels
[] short DAT   // text field for bottom label //
[] long DAT
[] force print to file
button, load custom background
button, select new font for data and labels
button, load custom set of icons
*/
}

@script ExecuteInEditMode() // display GUI in editor


TAZ

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.

Object[] selObjects = Resources.LoadAll(...);
Texture2D[] selTexture = new Texture2D[selObjects.Length];
for (var i = 0; i < selObjects.Length; i++)
    selTexture[i] = selObjects[i] as Texture2D;

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

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. :slight_smile:

My code is C# but the casting is the same. There are 2 methods.

Texture2D tex = obj as Texture2D; // makes tex null if cast fails

Texture2D tex = (Texture2D)obj; // throws exception if cast fails

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