- Home /
Loading an element from an array without loading the whole array?
(I am developing on android) I have a big array which i rarely use but from time to time i need to load some data from it.The problem is that because of the array size the whole game freezes until the data loads. Is there a way to only get the array item that i need without loading the whole array first.
Hello, $$anonymous$$aybe you could give more context: Where is this array co$$anonymous$$g from? Array is a pretty generic word: for me an array is already in the memory, so you wouldn't have to "load" it.
From your question I guess what you name "array" is a list of data from a file?
well my code looks something like this:
using UnityEngine;
[System.Serializable]
public class Item
{
public string name;
public string belongingSet;
public Texture2D image;
public string rarity;
public int price;
public bool isLegacy;
}
then i do something like this
public Item[] itemList = new Item[]
I need to access a random item in the array but if i reference the script on a gameobject the scene takes considerably longer to load. So my question is.. is there any way i can access the array without making a reference of it inside another script.
No, there definitely isn't a way to reference it without loading it. Not unless you wrote it to a file, then wrote a table of contents with the byte indices of each item. Then you could load it specifically, but you're adding file and disk operations which would easily destroy any net benefit.
But, more to the point, I don't see anything in the code you've posted that would cause a long load time. Can you post the code showing how you instantiate each Item instance? A small performance boost in a big loop can bring big gains.
Answer by $$anonymous$$ · Aug 26, 2017 at 12:16 AM
You could use a B-Tree, unfortunatelly unity doesn't have a library for this by default but there are resources out there that can do the job. I haven't personally tried these but a google search came up with this: https://www.assetstore.unity3d.com/en/#!/content/21652 https://github.com/psychobolt/unity-btree
Your answer
Follow this Question
Related Questions
Reference scriptable object by path 0 Answers
(Yet another) Accessing array in another script - ref or copy? 1 Answer
Why does gameobject.Find/GetComponent not work when a new scene is loaded? 1 Answer
Saving and loading array (/list) of integers locally 1 Answer
Temporary textures or Materials in Editor removed in build 1 Answer