简体   繁体   English

Unity3D中的菜单,使用UnityScript和子类

[英]Menus in Unity3D, using UnityScript and subclasses

I am trying to set up a generic class that deals with menus within Unity3D. 我正在尝试建立一个处理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. 我希望每种菜单类型都有自己的参数,因此在此示例中,我希望从枚举中选择loadSceneOnMouseDown后,“ loadSceneOnMouseDown”具有另一个公共变量作为定义要加载的场景名称的字符串。 Whereas I want the "enableGameObject" Type to have a gameobject as a public var. 而我希望“ enableGameObject”类型具有一个游戏对象作为公共变量。 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.();" 我考虑过做一个进一步的子类来解决这个问题,但是在“ 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! 谢谢!

Unity's serialization doesn't really support polymorphism. Unity的序列化实际上并不支持多态。 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. MenuClass类型的列表从MenuClass可以在运行时包含子类对象,但是就序列化列表而言,Unity会假设它们都是基本类型的对象。

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. 保留所有可能的字段序列化MenuClass ,然后编写一个自定义检查器 ,该检查器在工作时仅公开相关字段。 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. 例如,Unity无法序列化Dictionary,但是可以序列化您缝合在一起的两个列表。 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. 流行的选择包括JsonFx,MiniJson,Protobufs和C#的内置XML序列化。 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. 除了大多数普通教程中的代码外,您还可以基于该控件值进行切换,然后调用诸如EditorGUILayout.FloatField()EditorGUILayout.ObjectField()及其表亲之类的函数来获取数据。

Custom editor scripting is often overlooked, but it's one of the most powerful features available to Unity developers. 自定义编辑器脚本经常被忽略,但这是Unity开发人员可用的最强大的功能之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM