Add alpha property to scene object
Hello,
After I started making more complex lenses that have multiple UI elements that need to be displayed and then removed from the screen I noticed that the enabled (true/false) way of doing it is very annoying.
For example, when I have a UI Toggle (from the user interface extra bundle provided by Snap) and I want to remove it from the screen I would do enabled=false either on itself or on it's parent scene. The problem is once I set enabled=true again now none of the api methods of the UI Toggle are recognized. It's like if it doesn't understand anymore that it's a UI Toggle.
This is how I get a reference to the UIToggle element via script:
var toggle=option.getChild(0).getComponent("Component.ScriptComponent");
then I set
toggle.enabled=true;
then
toggle.enabled=false;
then again
toggle.enabled=true;
and now the UIToggle is broken:
toggle.api.toggleOff();
18:44:55 [Scenarium] TypeError: undefined not callable (property 'toggleOff' of [object Object])
at loadAnswers (Create/CreateController.js:68)
at loadQuestion (Create/CreateController.js:40)
at [anon] (Create/CreateController.js:14) preventsyield
What I am going to do to hide the Toggle then is move it outside of the view bounds which is a terrible solution but gets the job done. Then when I want to display it again on the screen I just set its position back to the original one.
The same thing holds true for SceneObjects. It would be great if a scene object could have at least an alpha property so it could be not displayed instead of setting enabled=false which makes it lose all of its knowledge of methods once it's re-enabled again.
In iOS I would say the equivalent of the SceneObject is a UIView which has an alpha property just like any of its subclasses. Everything basically inherits from it. It's odd that an image object on Lens Studio would have an alpha property but a SceneObject wouldn't.
thanks,
Marco
Hi Marco,
There's a few issues with your approach right now.
1. The script API isn't working because the widget script needs to run on the first frame to set itself up. If you disable it before it has a chance to run, it won't initialize itself and the API won't be ready when you call it. The issue you're seeing is not that the script loses all methods, it actually never had a chance to create them in the first place. You can fix this by making sure your script runs after the widget script, so that it disables the widget after it's been initialized. You can do this by running your script in Lens Turn On instead of Initialized, or waiting until a later frame, or by putting your script further down in the scene hierarchy since scripts run in that order. You could even run it in the first Update or LateUpdate if you really have to.
2. Right now you're disabling the ScriptComponent, not the SceneObject. Disabling the ScriptComponent will just stop the script from running, it won't actually disable any visuals or child objects. Disabling the SceneObject will disable all components, including visuals, and any child objects. You can disable the SceneObject instead like this: toggle.getSceneObject().enabled=false;
From my experience, enabling/disabling the SceneObject after the script has been initialized works well for hiding and showing UI elements. Let me know if this isn't working for you, and I'll help you find a solution.
Jacob
Jacob, I think this doesn't really answer the title of the post. All elements should have an Alpha value. This will allow for smooth fade on's and off's of any and all elements you may want to do that with. Right now you can only do this with images. Text and 3D objects should have this capability for sure. I am sure there are things I am not thinking of off the top of my head.
It's a pretty frustrating limitation.