Search Unity

How to detect whether a prefab instance is restructurable?

Discussion in 'Prefabs' started by Ludiq, Aug 14, 2019.

  1. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    576
    I'm working on a simple editor script that groups/ungroups objects under a new parent empty transform.

    I'm using Undo.SetTransformParent and it works great for regular objects.

    For children of prefab instances, it simply fails silently.

    This is consistent with the limitation imposed by the hierarchy drag & drop. However, the hierarchy gives me this nice warning:

    upload_2019-8-14_17-40-45.png

    Is there a specific API I could use to get the same warning, or at least check whether a given GameObject/Transform is restructurable? This way I could abort the grouping operation early before creating an unusable parent game object.
     
  2. Ludiq

    Ludiq

    Joined:
    Mar 6, 2015
    Posts:
    576
    Right now I'm using this simple check, but I'm not sure if it's sufficient to cover all edge cases:

    Code (CSharp):
    1. public static bool IsRestructurable(Transform target)
    2. {
    3.     var go = target.gameObject;
    4.  
    5.     if (PrefabUtility.IsPartOfPrefabInstance(go) && !PrefabUtility.IsOutermostPrefabInstanceRoot(go))
    6.     {
    7.         return false;
    8.     }
    9.  
    10.     return true;
    11. }
     
  3. runevision

    runevision

    Unity Technologies

    Joined:
    Nov 28, 2007
    Posts:
    1,598
    Looks good to me.
     
    Ludiq likes this.