playMaker

Author Topic: Reset a prefab (UI element)  (Read 1431 times)

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 773
Reset a prefab (UI element)
« on: June 08, 2018, 02:07:57 PM »
Hi,

I've built some UI, with menus, buttons, panels and all that.
I use effects for their activation and deactivation (DoTweens, which include a nice "TO/FROM" switch so you can build your entire UI and leave the panels where they're meant to show up on the screen).
The opening effects are launched when the object is activated.
The closing effects are waiting, in the same FSM, for some specific events like CLOSE from a closing button, etc., to be ran.
But once an UI element has been hidden from the screen, I need to reset its values so the next time it's activated/called, the tweens will work flawlessly.

For that, I had to transform my entire UI into a mass of prefabs, so I could revert to an UI element's original state before activation. But now I need to find a way to do that resetting.

Here I found the following answer:

Quote
Make your object an prefab and try
UnityEditor.PrefabUtility.ResetToPrefabState(this.gameObject)
It not seems to work for me though, but the docs says "Resets the properties of the component or game object to the parent prefab state"

Is there a way to make this work in PlayMaker? Is there an action that can do that already?

This would be almost similar to option #2 described in this thread but without creating/destroying UI elements; today it's impossible because uGUI uses its rectTransforms and there's no way I know of to create an uGUI element from a prefab and place it on screen, and anyway I'd rather not destroy elements but simply deactivate them.

Broken Stylus

  • Beta Group
  • Hero Member
  • *
  • Posts: 773
Re: Reset a prefab (UI element)
« Reply #1 on: June 09, 2018, 06:44:05 PM »
As an extra note about creating UI elements from prefabs.
I create the instance, then parent it to a canvas or a child of that canvas with the Reset Local Position option, then activate the UI element.

I get the following warning:

Quote
Parent of RectTransform is being set with parent property. Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues.

This makes me quite nervous. It's such a messy way to place UI elements, when one considers how complex the uGUI system is with all its nested rectTransforms and references.
If I had to use prefabs, I would really, really prefer being able to simply reset an instance of a prefab that was already in the scene, where I saved it as I built my pretty UI tree, without having to destroy it, than having to duplicate the panel's prefab every single time and pray that it's going to be placed exactly where I expected it to fit during the parenting phase I described above.  :-X