- Home /
Get All GameObject in Prefab Mode
I have a custom script that Inherit from TextMeshProUGUI and now I want to convert all of my GameObjects UI that have TextMeshProUGUI into my custom script. I have created a MenuItem and the logic for changing it. It works wonderfully. The problem is that, I wanted to be able to change the TMPUGUI in prefabs as well, but my current code only gets the GameObjects that is in the current scene. How do I / Is there a function to get all the GameObjects in Prefab Scene mode? Is there a way to detect if I'm currently in Normal Scene or in Prefab Scene mode? because I want to use 1 Menu Item that can be used both in Normal Scene and Prefab Scene mode if the way to get the GameObjects is different between modes. Thank you.
[MenuItem("Tools/Migrate TMPUGUI to TMPLanguage")]
public static void MigrateTMPToTMPLang()
{
TextMeshProUGUI[] TMPs = GameObject.FindObjectsOfType<TextMeshProUGUI>();
...
}
Did you find a solution? I am running into the same issue...
Answer by Yanfundi · Apr 06 at 02:51 PM
Alright, after all this time, I finally found it. PrefabStageutility.GetCurrentPrefabStage(). It's using UnityEditor.Experimental.SceneManagement;
TextMeshProUGUI[] TMPs = new TextMeshProUGUI[0];
if (PrefabStageUtility.GetCurrentPrefabStage() != null) //In Prefab Mode
TMPs = PrefabStageUtility.GetCurrentPrefabStage().FindComponentsOfType<TextMeshProUGUI>();
else if (Selection.activeGameObject != null) //For changing selected only (include child). Works for prefab as well (no need to open the prefab).
TMPs = Selection.activeGameObject.GetComponentsInChildren<TextMeshProUGUI>(); //Could use Selection.gameobjects for multiple selection, probs change TMPs to List
else //In Scene Mode not selecting anything
TMPs = GameObject.FindObjectsOfType<TextMeshProUGUI>();
Answer by GeroNL · Aug 30, 2021 at 03:07 AM
Hello, you can not find gameobect if it not exist in the scene, you need to instantiate first.
Reference : https://docs.unity3d.com/Manual/InstantiatingPrefabs.html
Hope it help
Your answer
