Hi,
I’m having a bit of trouble, as I find no documentation about this. I wish to do a script that takes the user input from two serialized fields, one containing the metadata key, and the other containing the metadata value that should be contained in that key. Basically, I want to find all objects that have a certain metadata property with a certain value, and add a script to them on the fly. This will be done inside the Awake() method. However, I don’t know how to find objects by metadata. Is it possible?
Kind regards, Samuel Lourenço
What sort of metadata are we talking about?
The term metadata is pretty generic. For example you can use attributes to apply metadata to classes/fields/etc and reflecting out attributes requires one process. Another is the metadata that is associated with assets in Unity (the *.meta file created in the directory along side your asset/scripts), which is pretty proprietary to Unity itself. There’s also program metadata used for consuming assemblies. Or it could just be something custom you’ve come up with yourself.
There’s any number of things you could mean by “metadata”… so what do you mean?
Hi,
I’m refering to the metadata imported from a BIM model in Reflect (if I’m correct).
Please, see the screenshot for an example.
Kind regards, Samuel Lourenço
That looks like perhaps just a MonoBehaviour-based script with some public fields containing the metadata.
You would need to simply get ahold of those script instances.
At editor time you can write code to look through your assets for instances of such a thing. Look into Editor scripting.
At runtime if those are loaded in scene you can look through your scene with FindObjectsOfType() and get at the script instances that store the metadata.
Thanks!
I’ve came up with this script, thanks to your help, but now I’m stuck again. Here is what I’m doing:
private void Awake()
{
Metadata[] metadataObjects = FindObjectsOfType<Metadata>();
foreach (Metadata metadataObject in metadataObjects)
{
metadataObject.GetParameter(parameterKey);
}
}
Basically, inside the foreach loop, I also want to check if the value corresponding to the parameterKey corresponds to a certain value, which is also serialized. Inside the Awake method, I want to obtain the GameObject to which the Metadata is attached, which should be relatively easy, and put it in a array. However, the GetParameter() method is not documented in a very explanatory way.
I’m stuck. Please, help me.
Kind regards, Samuel Lourenço
7111651–848521–AlarmZone.cs (815 Bytes)
I assume this returns the data.
Compare it to the value you are interested in and decide if you want to do something, then do it.
It worked. Thanks! I haven’t paid attention when reading the documentation.
Kind regards, Samuel Lourenço
I want to display the data on the UI. How did you do it? Please write up the code.