Unity Declaration of structures

Recommended Posts

How can one declare a structure (more specifically an array of structures) inside another structure in C#? Tried something I found on the Microsoft website and it doesn't appear to work for Unity. If anyone could provide me an example or point me in the right direction, that would be extremely helpful ...

Thanks.

Share on other sites

Because C# structs are value types, rather than reference types, fixed arrays like that are not permitted.

You can create a struct member that is a reference, such as:  public MyOtherStruct[] name;  You'll then need to allocate the array and manipulate it on your own.

You cannot create a fixed array except for primitive types, such as: public fixed int items[256];  However, that use is not recommended for several reasons. Value types should be kept small if possible.

Share on other sites
public struct SimpleStruct
{
public int Foo;
public int Bar;
}

public struct StructOfArrayOfStructs
{
public SimpleStruct[] Elements;
}

When you say "Doesn't appear to work for Unity", EXACTLY what do you mean? Edited by Nypyren

Share on other sites
When you say "Doesn't appear to work for Unity", EXACTLY what do you mean?

No, I meant that for structures inside other structures, not for vectors of structures. I can't seem to be able to instantiate the variables. I'm not even sure if it's possible right now.

Share on other sites

I'm going to take a wild guess that you're trying to use an array of structs as a member of a MonoBehaviour or ScriptableObject.

If that's the case, you need to put the 'Serializable' attribute on your structs for them to appear in Unity's inspector and get serialized to asset files:

[Serializable] // using System;
public struct SimpleStruct
{
public int Foo;
public int Bar;
}

[Serializable]
public struct StructOfArrayOfStructs
{
public SimpleStruct[] Elements;
}


Share on other sites

When you say "Doesn't appear to work for Unity", EXACTLY what do you mean?

No, I meant that for structures inside other structures, not for vectors of structures. I can't seem to be able to instantiate the variables. I'm not even sure if it's possible right now.

Ok, I posted my second message before I read this.

Structs have slightly different construction rules than classes. What are you trying? Can you show some code?

Share on other sites

when you post questions like this without providing the exact and complete error messages.

(Bold fix)

I would also add that you (4detc) should also include the actual code which produces those error messages.

Share on other sites

When you say "Doesn't appear to work for Unity", EXACTLY what do you mean?

No, I meant that for structures inside other structures, not for vectors of structures. I can't seem to be able to instantiate the variables. I'm not even sure if it's possible right now.

For reasons that only god knows, Unity uses a terribly outdated version of mono. Possibily for stability and support reasons. Though that won't make much sense give that prior to the upgrade to Unity 5, they upgraded to a SLIGHTLY newer version of mono. Which is really just maybe a slight step up the revision.

Now... are you talking about defining a structure inside a structure? If so I wasn't even aware that was a legal action.

Share on other sites

Now... are you talking about defining a structure inside a structure? If so I wasn't even aware that was a legal action.

It's allowed. I haven't seen it in real code yet, though...

I don't know of anything Mono or Unity specific regarding structs that differs from the usual .Net, either (besides internal implementation details, of course). Edited by Nypyren

Share on other sites

Thank you all for the help. :)

Create an account

Register a new account

• Forum Statistics

• Total Topics
628378
• Total Posts
2982334
• Similar Content

• itSeez3D, a leading developer of mobile 3d scanning software, announced today a new SDK for its automatic 3D avatar generation technology, Avatar SDK for Unity. The Avatar SDK for Unity is a robust plug-n-play toolset which enables developers and creatives to integrate realistic user-generated 3D avatars into their Unity-based applications. SDK users can allow players to create their own avatars in the application or integrate the SDK into their own production processes for character design and animation.
“Virtual avatars have recently become increasingly popular, especially in sports games and social VR apps. With the advance of VR and AR, the demand to get humans into the digital world is only increasing”, said Victor Erukhimov, itSeez3D CEO. “Our new Avatar SDK for Unity makes it super-easy to bring the avatar technology into any Unity-based game or VR/AR experience. With the Avatar SDK for Unity now every developer can bring face scanning technology into their games and allow players to create their own personalized in-game avatars, making the gameplay much more exciting and immersive.”
Key features of the Avatar SDK for Unity:
Automatic generation of a color 3D face model from a single selfie photo in 5-10 seconds (!). Works best with selfies, but can be used with any portrait photo.
Shape and texture of the head model are unique for each person, synthesized with a deep learning algorithm crafted by computer vision experts
Head models support runtime blendshape facial animations (45 different expressions)
Generated 3D heads include eyes, mouth, and teeth
Algorithms synthesize 3D meshes in mid-poly resolution, ~12k vertices, and ~24k triangles
Six predefined hairstyles with hair-recoloring feature (many more available on request)
Avatar generation API can be used in design-time and in run-time, which means you can allow users to create their own avatars in your game
Cloud version is cross-platform, and offline version currently works on PCs with 64-bit Windows (support for more platforms is coming soon)
Well-documented samples showcasing the functionality.

Availability
The Avatar SDK for Unity is offered in two modes - “Cloud” and “Offline”. The “Cloud” version is available at http://avatarsdk.com/ and the “Offline” version is available by request at support@itseez3d.com.
###