public override void OnInspectorGUI()
        {
            if (EditorApplication.isPlayingOrWillChangePlaymode)
            {
                return;
            }

            var reference = this.target as GeneratedMeshes;

            if (!reference)
            {
                return;
            }

            var destination = reference.owner;

            if (!destination)
            {
                GUILayout.Space(10);
                GUILayout.Label("The owner of this mesh doesn't exist anymore!");
                GUILayout.Space(10);
            }
            else
            {
                var defaultModel = InternalCSGModelManager.GetDefaultCSGModelForObject(reference);
                GUILayout.Space(10);
                if (destination == defaultModel || !defaultModel || !destination)
                {
                    GUILayout.Label("This mesh was automatically generated by the default CSG-model");
                }
                else
                {
                    GUILayout.Label("This mesh was automatically generated by a CSG-model called '" + destination.name + "'");
                    GUILayout.Space(10);
                    if (GUILayout.Button("Take me there"))
                    {
                        Selection.activeObject = destination;
                    }
                }
                GUILayout.Space(10);
            }
        }
Exemplo n.º 2
0
        // returns true when modified
        internal static bool UpdateNodePosition(HierarchyItem node, ParentNodeData top)
        {
            // on each level, compare sibling position to nodes before and after it.
            // if it's different, remove self and find new position in array.
            // continue to next parent.
            var iteratorParent = node.Parent;

            if (iteratorParent == null)
            {
                AddNode(node, top);
                top.ChildrenModified = true;
                return(true);
            }

            var currentLoopCount = CurrentLoopCount;

            var iterator = node;

            while (iteratorParent != null)
            {
                var iteratorTransformID = iterator.TransformID;

                if (iteratorTransformID == 0)
                {
                    Debug.Log("iterator_transform == null");
                }
                else
                if (iterator.LastLoopCount != currentLoopCount)
                {
                    var iteratorTransform         = iterator.Transform;
                    var iteratorParentTransformID = iteratorTransform.parent == null ? 0 : iteratorTransform.parent.GetInstanceID();
                    // Compare the unity parent transform to the stored parent transform
                    if (iteratorParent.TransformID != iteratorParentTransformID)
                    {
                        var defaultCSGInstanceID = 0;
                        var defaultCSGModel      = InternalCSGModelManager.GetDefaultCSGModelForObject(iteratorTransform);
                        if (defaultCSGModel != null &&
                            defaultCSGModel.transform != null)
                        {
                            defaultCSGInstanceID = defaultCSGModel.transform.GetInstanceID();
                        }
                        if (defaultCSGInstanceID == 0 || defaultCSGInstanceID != iteratorParent.TransformID)
                        {
                            RemoveNode(node, top);
                            AddNode(node, top);
                            top.ChildrenModified = true;
                            return(true);
                        }
                    }

                    if (iterator.LastLoopCount != currentLoopCount)
                    {
                        iterator.CachedTransformSiblingIndex = iteratorTransform.GetSiblingIndex();
                        iterator.LastLoopCount = currentLoopCount;
                    }

                    // Does the child even exist in the parent transform?
                    int iteratorChildIndex;
                    if (!iteratorParent.FindSiblingIndex(iteratorTransform, iterator.CachedTransformSiblingIndex, iteratorTransformID, out iteratorChildIndex))
                    {
                        RemoveNode(node, top);
                        AddNode(node, top);
                        top.ChildrenModified = true;
                        return(true);
                    }

                    // See if the position of the child has changed ..
                    if (iteratorChildIndex != iterator.SiblingIndex)
                    {
                        iterator.SiblingIndex = iteratorChildIndex;
                        RemoveNode(node, top);
                        AddNode(node, top);
                        top.ChildrenModified = true;
                        return(true);
                    }

                    // Compare the child index to the one before and after it ..
                    var iteratorParentChildNodes = iteratorParent.ChildNodes;
                    var iteratorSiblingIndex     = iteratorTransform.GetSiblingIndex();
                    if (iteratorChildIndex > 0)
                    {
                        var prevTransform = iteratorParentChildNodes[iteratorChildIndex - 1].Transform;
                        if (!prevTransform)
                        {
                            return(false);
                        }
                        var iteratorPrevSiblingIndex = prevTransform.GetSiblingIndex();
                        if (iteratorPrevSiblingIndex >= iteratorSiblingIndex)
                        {
                            RemoveNode(node, top);
                            AddNode(node, top);
                            top.ChildrenModified = true;
                            return(true);
                        }
                    }
                    else
                    if (iteratorChildIndex < iteratorParentChildNodes.Length - 1)
                    {
                        var nextTransform = iteratorParentChildNodes[iteratorChildIndex + 1].Transform;
                        if (!nextTransform)
                        {
                            return(false);
                        }
                        var iteratorNextSiblingIndex = nextTransform.GetSiblingIndex();
                        if (iteratorNextSiblingIndex <= iteratorSiblingIndex)
                        {
                            RemoveNode(node, top);
                            AddNode(node, top);
                            top.ChildrenModified = true;
                            return(true);
                        }
                    }
                }

                iterator       = iteratorParent;
                iteratorParent = iterator.Parent;
            }

            return(false);
        }
Exemplo n.º 3
0
        // returns true on success
        internal static bool AddNode(HierarchyItem node, ParentNodeData top)
        {
            if (node.Parent != null)
            {
                //Debug.Log("node.parent != null");
                return(false);
            }

            if (!top.Transform)
            {
//				var top_transform_name	= (top.transform == null) ? "null" : top.transform.name;
//				var node_transform_name = (node.transform == null) ? "null" : node.transform.name;
//				Debug.Log("!top.transform (top.transform=" + top_transform_name + ", node.transform = " + node_transform_name + ")", node.transform);
                return(false);
            }

            var ancestors = new List <Transform>();

            var leafTransform = node.Transform;

            if (leafTransform == null)
            {
                //Debug.Log("node.transform == null");
                return(false);
            }
            var iterator = leafTransform.parent;

            while (iterator != null &&
                   iterator != top.Transform)
            {
                ancestors.Add(iterator);
                iterator = iterator.parent;
            }

            var defaultModel = InternalCSGModelManager.GetDefaultCSGModelForObject(iterator);

            if (!defaultModel)
            {
                return(false);
            }

            var defaultModelTransform = defaultModel.transform;

            if (iterator == null || top.Transform == null ||
                top.Transform == defaultModelTransform)
            {
                iterator = defaultModelTransform;
            }

            if (iterator == null)
            {
                node.Reset();
                top.Reset();
                //Debug.Log("iterator == null");
                return(false);
            }
            if (iterator != top.Transform)
            {
                //Debug.Log("iterator != top.transform");
                return(false);
            }

//			var currentLoopCount = CurrentLoopCount;

            HierarchyItem lastParent    = top;
            var           ancestorDepth = ancestors.Count - 1;

            while (ancestorDepth >= 0)
            {
                var ancestor = ancestors[ancestorDepth];
                int childIndex;
                if (!lastParent.FindSiblingIndex(ancestor, ancestor.GetSiblingIndex(), ancestor.GetInstanceID(), out childIndex))
                {
                    break;
                }

                lastParent = lastParent.ChildNodes[childIndex];
                ancestorDepth--;
            }
            while (ancestorDepth >= 0)
            {
                var newAncestor = new HierarchyItem();
                newAncestor.Transform   = ancestors[ancestorDepth];
                newAncestor.TransformID = newAncestor.Transform.GetInstanceID();
                newAncestor.Parent      = lastParent;

                if (!lastParent.AddChildItem(newAncestor))
                {
                    return(false);
                }

                lastParent = newAncestor;
                ancestorDepth--;
            }

            node.Parent          = lastParent;
            top.ChildrenModified = true;
            return(lastParent.AddChildItem(node));
        }
Exemplo n.º 4
0
        public static void OnPrefabSaving(GameObject obj)
        {
            var foundGeneratedMeshes = obj.GetComponentsInChildren <GeneratedMeshes>();

            if (foundGeneratedMeshes.Length == 0)
            {
                return;
            }

            var defaultModel = InternalCSGModelManager.GetDefaultCSGModelForObject(obj.transform);

            newMeshes.Clear();
            foreach (var generatedMeshesInstance in foundGeneratedMeshes)
            {
                if (generatedMeshesInstance.owner == defaultModel)
                {
                    continue;
                }

                foreach (var generatedMeshInstance in generatedMeshesInstance.GetComponentsInChildren <GeneratedMeshInstance>())
                {
                    if (!generatedMeshInstance) // possible when it's deleted in a prefab
                    {
                        continue;
                    }
                    foundGeneratedMeshInstances.Add(generatedMeshInstance);
                    if (generatedMeshInstance.SharedMesh)
                    {
                        newMeshes.Add(generatedMeshInstance.SharedMesh);
                    }
                }

                foreach (var helperSurface in generatedMeshesInstance.HelperSurfaces)
                {
                    foundHelperSurfaces.Add(helperSurface);
                    newMeshes.Add(helperSurface.SharedMesh);
                }
            }

            var asset     = CSGPrefabUtility.GetPrefabAsset(obj);
            var assetPath = AssetDatabase.GetAssetPath(asset);

            var assetObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath);

            foreach (var assetObject in assetObjects)
            {
                var mesh = assetObject as Mesh;
                if (!mesh)
                {
                    continue;
                }
                oldMeshes.Add(mesh);
            }

            if (newMeshes.Count == 0 && oldMeshes.Count == 0)
            {
                return;
            }

            // We might be modifying a prefab, in which case we need to store meshes inside it that belong to it
            AssetDatabase.StartAssetEditing();
            try
            {
                foreach (var oldMesh in oldMeshes)
                {
                    if (newMeshes.Contains(oldMesh))
                    {
                        continue;
                    }

                    if (CanRemoveObjectFromAsset(assetPath, oldMesh, ignoreWhenPartOfOtherAsset: true))
                    {
                        AssetDatabase.RemoveObjectFromAsset(oldMesh);
                    }
                }

                foreach (var _newMesh in newMeshes)
                {
                    var newMesh = _newMesh;
                    if (oldMeshes.Contains(newMesh))
                    {
                        if (IsObjectPartOfAsset(assetPath, newMesh))
                        {
                            continue;
                        }
                    }
                    if (CanAddObjectToAsset(assetPath, newMesh))
                    {
                        AssetDatabase.AddObjectToAsset(newMesh, asset);
                    }
                }
            }
            finally
            {
                AssetDatabase.StopAssetEditing();
            }
        }
        // returns true on success
        internal static bool AddNode(HierarchyItem node, ParentNodeData top)
        {
            if (node.Parent != null)
            {
                return(false);
            }

            if (!top.Transform)
            {
                return(false);
            }

            var ancestors = new List <Transform>();

            var leafTransform = node.Transform;

            if (leafTransform == null)
            {
                return(false);
            }
            var iterator = leafTransform.parent;

            while (iterator != null &&
                   iterator != top.Transform)
            {
                ancestors.Add(iterator);
                iterator = iterator.parent;
            }

            var defaultModel = InternalCSGModelManager.GetDefaultCSGModelForObject(iterator);

            if (!defaultModel)
            {
                return(false);
            }

            var defaultModelTransform = defaultModel.transform;

            if (iterator == null || top.Transform == null ||
                top.Transform == defaultModelTransform)
            {
                iterator = defaultModelTransform;
            }

            if (iterator == null)
            {
                node.Reset();
                top.Reset();
                return(false);
            }
#if !UNITY_2018_3_OR_NEWER
            // TODO: figure out how to check this, yet still support prefab isolation mode
            if (iterator != top.Transform)
            {
                return(false);
            }
#endif

            //var currentLoopCount = CurrentLoopCount;

            HierarchyItem lastParent    = top;
            var           ancestorDepth = ancestors.Count - 1;
            while (ancestorDepth >= 0)
            {
                var ancestor = ancestors[ancestorDepth];
                int childIndex;
                if (!lastParent.FindSiblingIndex(ancestor, ancestor.GetSiblingIndex(), ancestor.GetInstanceID(), out childIndex))
                {
                    break;
                }

                lastParent = lastParent.ChildNodes[childIndex];
                ancestorDepth--;
            }
            while (ancestorDepth >= 0)
            {
                var newAncestor = new HierarchyItem();
                newAncestor.Transform   = ancestors[ancestorDepth];
                newAncestor.TransformID = newAncestor.Transform.GetInstanceID();
                newAncestor.Parent      = lastParent;

                if (!lastParent.AddChildItem(newAncestor))
                {
                    return(false);
                }

                lastParent = newAncestor;
                ancestorDepth--;
            }

            node.Parent          = lastParent;
            top.ChildrenModified = true;
            return(lastParent.AddChildItem(node));
        }