Adjunte la clase base a la escena o proyecto de Unity3D

Per scene in Unity3D, I always require some base class implementing interfaces and functionality such as delegating events for changing scenes via Application.LoadLevel().

Typically I've attached these scripts to the Camera; however, I'm not sure this is a best practice especially when multiple cameras are present.

How is a base class for a scene specified?

When the scene is selected from the project no components may be added nor any option available in the inspector.



How would I subtype or attach a base class directly to the scene?

Likewise for an entire Unity3D project, is there any way to base class or subtype to enable code for a Unity application independent of scenes?

preguntado el 04 de julio de 12 a las 05:07

AFAIK you can't attach scripts to a scene. Why not create an empty Game Object as parent for your cameras and apply the scripts on that object? -

Interesting... that makes a lot of sense. Post that as an answer and I'll accept it. -

Just disambiguation from Unidad de Microsoft -

It doesn't need disambiguation when tagged with unity3d. The tag is itself a problem, but we don't need to reinforce that. -

I realize this is an old post, but I feel this is a valuable response, especially since 1 year later things are still as they were then. Search Unity: The first result is, home of the Unity game engine (popularly referred to as Unity3D). I also happen to like the opening line of Wikipedia article on Unity, which states: "Unity (also called Unity3D) is a cross-platform game engine with a built-in IDE developed by Unity Technologies." Many people know it as Unity3D. The developer's home site url has the 3D. I think Unity3D is a fair tag. Plus, sites like LinkedIn call it Unity3D. -

2 Respuestas

Scripts must always be attached to Game Objects. Seeing as Game Objects can be parented you can group your cameras under one single parent and apply your scripts to that parent.

Respondido 04 Jul 12, 05:07

I think Wesley's answer speaks more to the OP's direct question, what is best practice for attaching scripts to the scene. Your answer below will certainly work but you still need some script attached to the scene to call ScriptableObject.CreateInstance. Somewhere someone has to call 'a' script and it will be a script attached to a GameObject. - Jerdak

If you're not using Unity's serialization, then you don't need to bother with ScriptableObject; you can just use any base or derived class. As such, creating a ScriptableObject from a MonoBehaviour doesn't make sense, unless you're triggering it from a drop-down menu or custom Inspector. An Editor script is just as likely a candidate for who calls ScriptableObject. - Jessy

Puedes usar ScriptableObject.CreateInstance to create a class that can be stored with a scene, but not have to be attached to a Game Object. However, you'll need a reference to the instance, serialized with a MonoBehaviour instance, and visible in the Inspector, in order to be able to double-click it and see the serialized instance variables in the Inspector (if you don't need the Inspector for the ScriptableObject, then as long as you have a reference to it, it will persist with the scene data). This may or may not make more sense than just making the ScriptableObject a MonoBehaviour instead. A ScriptableObject instance can also be stored as an Asset, like a prefab, which is what you should do, if you need it in more than one scene.

Respondido 05 Jul 12, 17:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.