コード例 #1
0
    // Save as hierarchical prefab
    public static bool SaveAsHierarchicalPrefab(GameObject a_rGameObjectToSave, string a_rSavePath)
    {
        // Convert to hierarchical prefab
        HierarchicalPrefabInstance rHierarchicalPrefabInstanceModel = NestedPrefabEditorUtility.ConvertIntoHierarchicalPrefab(a_rGameObjectToSave);

        // The prefab to save into
        Object rPrefabObjectToSaveInto = AssetDatabase.LoadAssetAtPath(a_rSavePath, typeof(Object));

        // Check if the hierarchical prefab is cyclic
        if (IsHierarchicalPrefabInstanceCyclic(rHierarchicalPrefabInstanceModel, rPrefabObjectToSaveInto))
        {
            Debug.LogError("You can't create cyclic hierarchy!");
            rHierarchicalPrefabInstanceModel.RevertToHierarchicalPrefab();
            return(false);
        }

        // Save into a prefab at save path
        if (rPrefabObjectToSaveInto == null)
        {
            rPrefabObjectToSaveInto = PrefabUtility.CreateEmptyPrefab(a_rSavePath);
        }
        else
        {
            // If an object replace a prefab copy the replaced prefab version number
            // To the one replacing
            if (rPrefabObjectToSaveInto != rHierarchicalPrefabInstanceModel.HierarchicalPrefab)
            {
                // Try to get the hierarchical prefab instance model of the soon to de replaced prefab
                GameObject rPrefabToBeReplacedGameObject = NestedPrefabEditorUtility.GetPrefabGameObject(rPrefabObjectToSaveInto);
                if (rPrefabToBeReplacedGameObject != null)
                {
                    HierarchicalPrefabInstance rHierarchicalPrefabInstanceModelToBeReplaced = rPrefabToBeReplacedGameObject.GetComponent <HierarchicalPrefabInstance>();
                    if (rHierarchicalPrefabInstanceModelToBeReplaced != null)
                    {
                        // Copy the version number of the instance to be replaced
                        rHierarchicalPrefabInstanceModel.CopyVersionNumber(rHierarchicalPrefabInstanceModelToBeReplaced);
                    }
                }
            }
        }

        // Notify model instance of it's saving
        rHierarchicalPrefabInstanceModel.OnSaveModelInstanceBeforePrefabReplacement(PrefabUtility.GetPrefabObject(rPrefabObjectToSaveInto));

                #if BEFORE_UNITY_4_3
        EditorUtility.ReplacePrefab(a_rGameObjectToSave, rPrefabObjectToSaveInto, ReplacePrefabOptions.ConnectToPrefab);
                #else
        PrefabUtility.ReplacePrefab(a_rGameObjectToSave, rPrefabObjectToSaveInto, ReplacePrefabOptions.ConnectToPrefab);
                #endif

        // Compilation is not up to date
        NestedPrefabEditorSettings.MustCompile = true;

        return(true);
    }
コード例 #2
0
    // On Hierarchical prefab update
    public void OnHierarchicalPrefabUpdate(HierarchicalPrefabInstance a_rHierarchicalInstanceCaller)
    {
        if (this != null)
        {
            // if the connection with the hierarchical prefab is broken
            if (HierarchicalPrefab == null)
            {
                // If the prefab is still there and has just been replaced
                if (m_rPrefabObject != null)
                {
                    // Try to grab the nested prefab data from the potential instantiator
                    NestedPrefabData rNestedPrefabData = TryGrabNestedPrefabData();

                    // Replace the current object by an instance of the new prefab
                    GameObject rNewInstance = PrefabUtility.InstantiatePrefab(NestedPrefabEditorUtility.GetPrefabGameObject(m_rPrefabObject)) as GameObject;
                    HierarchicalPrefabInstance rNewHierarchicalPrefabInstance = rNewInstance.GetComponent <HierarchicalPrefabInstance>();
                    // If nested
                    if (rNestedPrefabData != null && rNewHierarchicalPrefabInstance != null && transform.parent != null)
                    {
                        rNewHierarchicalPrefabInstance.ReloadNestedPrefabData(rNestedPrefabData);
                        // Change the parent without changing the local transform information
                        NestedPrefabUtility.ChangeParentAndKeepSameLocalTransform(rNewHierarchicalPrefabInstance.transform, transform.parent);
                    }
                    else
                    {
                        Vector3 f3LocalScaleSave = rNewInstance.transform.localScale;
                        rNewInstance.transform.parent        = transform.parent;
                        rNewInstance.transform.localPosition = transform.localPosition;
                        rNewInstance.transform.localRotation = transform.localRotation;
                        rNewInstance.transform.localScale    = f3LocalScaleSave;
                    }

                    // Auto destruction
                    Editor.DestroyImmediate(gameObject);
                }
            }
            else
            {
                // Revert to hierarchical prefab
                //RevertToHierarchicalPrefab();
                if (this == a_rHierarchicalInstanceCaller)
                {
                    // if it's the updated instance we just redeploy the hierarchy
                    DeployHierarchy();
                }
                else
                {
                    // Revert to hierarchical prefab
                    RevertToHierarchicalPrefab();
                }
            }
        }
    }