Sign in to follow this  
4d69636861656c

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628378
    • Total Posts
      2982334
  • Similar Content

    • By sveta_itseez3D
      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.
      ###
      About itSeez3D
      At itSeez3D, we are working on the computer vision technology that turns mobile devices into powerful 3D scanners. itSeez3D has developed the world's first mobile 3D scanning application that allows to create high-resolution photorealistic 3D models of people's' faces, bodies and objects. The application is available for iOS and Windows OS mobile devices powered with 3D cameras. In 2016 the company introduced Avatar SDK that creates a realistic 3D model of a face from a single selfie photo. To learn more about itSeez3D scanning software and 3D avatar creation technology, please visit www.itseez3d.com and www.avatarsdk.com.

      View full story
  • Popular Now