Owen McConnell
Owen McConnell

Reputation: 81

Menus in Unity3D, using UnityScript and subclasses

I am trying to set up a generic class that deals with menus within Unity3D.

The following code I intend to attach to an empty game object

import System.Collections.Generic;

#pragma strict

public enum MenuType { loadSceneOnMouseDown, enableGameObject, changeValue };

    public class MenuClass extends System.Object
    {
        public var menuObject           : GameObject;
        public var menuType             : MenuType;
    }

public var menuItems = List.<MenuClass>();

This results in the following within the editor:

https://dl.dropboxusercontent.com/u/12370860/Untitled17.png

I want each menu type to have its own parameter, so in this example I want the "loadSceneOnMouseDown" to have another public variable as a string defining the name of the scene to be loaded, once loadSceneOnMouseDown is selected from the enum. Whereas I want the "enableGameObject" Type to have a gameobject as a public var. However I dont want both to appear in both fields. For example I could make it like this:

public class MenuClass extends System.Object
{
    public var menuObject           : GameObject;
    public var menuType             : MenuType;
    public var sceneName            : String
    public var targetObject         : GameObject
}

But that would make it come under each element of the list. I considered doing a further subclass to resolve this, but then with the line "public var menuItems = List.();" that wouldnt work. (or would it? :D)

Apologies if I have been unclear, trying my best here but Im finding it difficult. Theres probably a very simple solution, or im going about thins entirely the wrong way. Any help with this problem would be very much appreciated!

Thanks!

Upvotes: 1

Views: 336

Answers (1)

rutter
rutter

Reputation: 11452

Unity's serialization doesn't really support polymorphism. A list of type MenuClass can technically contain subclassed objects at runtime, but as far as serializing the list Unity is going to assume they are all objects of the base type.

It's a bit inconvenient, sometimes.

There are a few popular workarounds:

  • Leave all of the possible fields serialized MenuClass, then write a custom inspector which exposes only the relevant fields while you're working. In simple cases like yours, this is often the quickest solution.
  • Serialize some basic data fields, then use that data to reconstruct your more elaborate objects at runtime. For example, Unity can't serialize a Dictionary, but it can serialize two lists which you stitch back together. Handy, still simple, has some limits.
  • Build in some custom serialization library, and go nuts with it. Popular choices include JsonFx, MiniJson, Protobufs, and C#'s built-in XML serialization. Lots of work, but very powerful.

Like I said, in your case I'd recommend starting with the custom inspector, if you think that'll cover your needs.

Aside from code that's in most common tutorials, you could switch based on that control value, then call functions like EditorGUILayout.FloatField(), EditorGUILayout.ObjectField() and their cousins to get data.

Custom editor scripting is often overlooked, but it's one of the most powerful features available to Unity developers.

Upvotes: 1

Related Questions