Skip to content

Instantly share code, notes, and snippets.

@beardordie
Created August 8, 2018 22:36
Show Gist options
  • Save beardordie/139affe81a0492e2a0725928c004adae to your computer and use it in GitHub Desktop.
Save beardordie/139affe81a0492e2a0725928c004adae to your computer and use it in GitHub Desktop.
Nested Classes
// Use a class that does not inherit from MonoBehaviour and has attribute [System.Serializable]
// then reference that in a MonoBehaviour class. This results in a dropdown in the Inspector
// for the referenced class.
// The documentation says to have the NestedClass inherit from System.Object but I've seen no need for that.
// For more robust functionality, consider creating a ScriptableObject/template for the Nested Classes,
// then reference those. It won't drop down in the inspector the same way, but it affords other benefits.
using UnityEngine;
public class NestedClassHolder : MonoBehaviour
{
public NestedClass1 nestedClass1;
}
[System.Serializable]
public class NestedClass1
{
public float myFloat = 5.5f;
public Color myColor = Color.blue;
}
@beardordie
Copy link
Author

Alternative setup:

public class Class : MonoBehaviour
{
   [System.Serializable]
   public class NestedClass
   {
             public float data;
   }
 
   public NestedClass data = new NestedClass() ;
};

I'm not sure when this method would be preferable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment