[RELEASED, free] EXPOSED - rule-based references

EXPOSED - rule-based references, new on Unity Asset Store
→ EXPOSED - rule-based references | Visual Scripting | Unity Asset Store

Have you ever faced such problems:

  • Recreating references for your components over and over again?
  • No control, how the references are actually configured?
  • No control over the references in the inspector when setting them in code?
  • End up frustrated with the slow scene loading when setting the references in code?

Well, here comes the solution

EXPOSED is a simple but powerful tool which helps you easily and automatically map object references for your components. You can set rules for creating references. Plus these references are completely reusable for other components. Everything is configurable in the Inspector and you can see the results of rules settings and references immediately.

EXPOSED vs. drag&drop
Comparing to ordinary drag&dropping EXPOSED dramaticaly reduces repeatable d&d or reference selecting.

EXPOSED vs. coding
EXPOSED helps you to visualize the reference settings and to get rid of initialization performance overhead when setting references by code in Awake method.

How to use it

  • Add ExposedReferences script on GameObject with your component.
  • Create reusable EXPOSED configuration asset file.
  • Define the rules.
  • Choose which references will be assigned automatically through EXPOSED and which will be handled manually.

Rules’ settings
There’s no need for coding when setting the rules, you can always use basic predefined rules. However, if you need to define extra conditions for obtaining references for scene objects, you can write your own rules and connect them through EXPOSED API. Or you will be able to use the query, which is realtime and the most flexible (query feature coming soon).

Predefined rules

  • GameObject – searching for components on the same GameObject
  • Children – searching for components or GameObjects in the children GameObjects
  • Parents – searching for components or GameObjects in the parents GameObjects
  • Tag – searching for components or GameObjects with specified tag in scene
  • Object of Type – searching for objects of specified type in scene

Plugin is entirely written in C# and the source code files are included.

I´m planning to keep working on the EXPOSED plugin and keep adding more features. So if you have some ideas what could make your work with references easier, please definitely get in touch.

EXPOSED tutorial: You´ll find how to create rules for mapping references to scene objects automatically and have always visual feedback about their configuration.

2 Likes

Hello everyone, I’ve just released EXPOSED plugin for free, so feel free to check it out. Let me know what do you think about it and what would you improve. I will continue working on it’s features and I’m looking forward to your feedback.

1 Like

It seems like a dependency injection tool, but differently from others available at asset store, the bindings are fully configured in a graphical way, so no need to set attributes in the scripts and no editor drag&drop references. Is that so?

How does it perform with static classes and singletons?

Has anyone used it with TDD?

I’d like to hear people that used it, seems very promissing…

I’ve tested the tool in a small project (attached), and it was worth it. The interface is easy to follow and the steps to put it working are very few. You have to add an asset in your project for each set of reference rules for each script, and this asset can be used by different GameObjects with the same references configuration (which does not mean that the references set with the same assets is gonna be the same for different objects, as you deal with rules and not with reference values). Besides, you have to add one ExposedReference component to each object that is gonna apply the rules. This component references the rules assets.

I tested with arrays and lists and works very nice.

I tested with multi-scene editing and found some issues. As Unity 5.? doesn’t support cross-references between scenes, as it could corrupt the scene file and lead to a mess of missing references (although cross-references can be set in script), Exposed had also a bad time trying to set the references. In the editor, I received the scene mismatch message in the reference box of my component. And for my bad surprise, after hitting Play, these references where set to none :frowning:

I tested with prefab reference (a GameObject reference in script where you attach a prefab to instantiate at runtime), but with the core rules it doesn’t work. The core rules search only in the scene objects, not the assets. The documentation says that there is no way to achieve this, but I think the method bellow could do the job:

public static string[] GetAllPrefabs () {
     string[] temp = AssetDatabase.GetAllAssetPaths();
     List<string> result = new List<string>();
     foreach ( string s in temp ) {
         if ( s.Contains( ".prefab" ) ) result.Add( s );
     }
     return result.ToArray();
}

Although, I don’t know if this feature can be a better practice than drag&drop in this case.

I tested with objects created at runtime (a prefab with preconfigured ExposedReferences), and for my great surprise, the references where correctly set, and for wonder surprise, the cross-scene references where set correctly(!). It seems some bug is preventing the cross-references between scenes, for non-dynamic objects.

A nice feature to be added would be to mark the GameObjects that ExposedReferences found to have any field with no reference set. Exposed already knows of missing/none references (there is a red 0x label besides the reference field in the component inspector). What I mean is a visual way to see the GameObjects in hierarchy.

I see there are some features left to be complete. Specially the cross-scene references, and maybe the not-instantiated prefab search.

2862535–209533–testExposed.unitypackage (18.9 KB)

I was able to solve the cross-references of not-dynamically-created scene objects after hitting Play. What happens is that at the moment the references are set (PropertyProcessorManager.cs,
ProcessSingleFieldWithProcessor() ) the scenes are not loaded yet, so the FindObject methods return null.

I commented the condition that only allows late references update for prefabs (ExposedReferencesEditor.cs, OnInspectorGUI()), and by this the cross-references where set correctly after Play.

I don’t know the side effects of it, could you explain if this is really a correct fix?

EDIT: After rolling back that change, that is, using the original Exposed package, the scenes where loaded at the moment the references are set after hitting Play. I don’t know what is happening now. Tried changing the script execution order but now the crossreferences are always being set correctly…

EDIT: I was trying something with PreInitialization, and discovered that I must disable it for cross-references to be found after hitting Play. I think the documentation for PreInitialization is very misleading, for example:

should mean (?):

Do you think the PreInitialization feature is really needed, as enabling it causes these undesired behaviours with cross-references and prefabs? If the searches are done just at Awake/Start, I don’t think it will harm the runtime as a whole.

Found another missing feature: the ability to expose references for persistent events, like OnClick() of a canvas Button.

Can this attach reference to unity conponents?
This is what I mean, I have a custom unit with a lot of colliders as children. When I call physics.overlappingsphere, I get a load of collider references to which each I have to call getcomponentinparent() on. I image this would be computationally expensive. Can exposed add a reference to collider conponent to cache so I save on all the getcomponents?

Hi @brunoleos , many thanks for your feedback and your time spent experimenting with Exposed.

I would like to clarify the caching (PreInitialization) principle used in Exposed first. The main purpose is to save some performance for loading scenes, because the references are set in advance (both in editor mode or for platform builds during postprocessing phase). You’re right that generally, it might not be so much time to save, but it can make more significant impact for larger scenes and when referencing gameobjects by it’s type or even it’s name. This is basically also the way how to reference the class type in a singleton or static way (or rather like single instance than proper Singleton). But there’s need for the referenced classes to derive from UnityEngine.Object type to be able to save the references for final build during caching process.

Therefore, you’re right that when not using this caching (PreInitialization) mechanism, it can work even for setting references between the scenes. There’s a common problem in Unity with saving references to scene objects, which basically prevents from referencing scene components from prefabs and also establishing references for objects from different scene (even if added additively) - you will always lose these references when entering playmode (or exiting Unity editor), when deserialization takes place. So, this is basically caused by Unity’s internal serialization principles, which might change in the future, but is quite limiting at the moment.

I originally planned to allow this caching (PreInitialization) more atomically for each reference, but it stayed as planned feature for future releases. This would probably solve your problems by having disabled caching for cross-references between monobehaviours in additively added scenes and you could still benefit from saving some more performance by caching the others.

Thanks for your great suggestion of showing highlighted references in the scene hiearchy window - this could be very useful feature and could save some headaches with having unexpected null pointer exceptions when using invalid references. It would probably need more complex solution with regards to be able for user to set for each reference whether it is required or not, but I think that it might even bring some other interesting code inspection’s features. And regarding referencing the prefab’s components - it seems more complicated to solve, because it won’t be possible to use it without PreInitialization enabled (AssetDatabase is UnityEditor script which won’t work during runtime on any platform except Editor itself).

It should be possible to use Exposed caching mechanism in that case. The only condition is that your structure has to be defined and saved to the scene (or prefab) and not generated dynamically during playmode. Than, you would need to add custom script to your children objects with colliders and exposed component where you set the rule for searching the reference to MyCustomUnit in parents. But in this specific case, I’m not sure, whether Exposed will bring you much more benefits than simply doing this manually by drag&dropping reference to MyCustomUnit from your script on children gameobject with collider in Hierarchy window. Because as you can forget to set references when adding some more children objects in the future, you can forget to add this custom script and exposed as well.

But you can definitely use Exposed for referencing the Collider[ ] components in children from your parent gameObject and it will always contain updated list of your children’s colliders.

@mazzino Is this product still supported?