Example #1
0
#pragma warning disable 612
        /// <summary>
        /// Utility to help convert objects modified with the Beta version of Polybrush (Asset Store)
        /// to Polybrus 1.x.
        /// </summary>
        /// <param name="component"></param>
        internal static PolybrushMesh ConvertGameObjectToNewFormat(z_AdditionalVertexStreams component)
        {
            GameObject    go           = component.gameObject;
            PolybrushMesh newComponent = go.GetComponent <PolybrushMesh>();
            MeshFilter    mf           = go.GetComponent <MeshFilter>();
            Mesh          mesh         = component.m_AdditionalVertexStreamMesh;

            Undo.DestroyObjectImmediate(component);

            // Cancel conversion if no mesh if found on Z_AdditionalVertexStreams
            if (mesh == null)
            {
                return(null);
            }

            if (newComponent == null)
            {
                newComponent = Undo.AddComponent <PolybrushMesh>(go);
                newComponent.Initialize();
            }

            newComponent.mode = PolybrushMesh.Mode.AdditionalVertexStream;
            newComponent.SetMesh(PolyMeshUtility.DeepCopy(mf.sharedMesh));
            newComponent.SetAdditionalVertexStreams(mesh);

            return(newComponent);
        }
        static HierarchyChanged()
        {
            EditorApplication.hierarchyChanged += () =>
            {
                foreach (var gameObject in Selection.gameObjects)
                {
                    var mesh = Util.GetMesh(gameObject);
                    var id   = EditableObject.GetMeshId(mesh);

                    // if the mesh is an instance managed by polybrush check that it's not a duplicate.
                    if (id != -1)
                    {
                        if (id != gameObject.GetInstanceID() && EditorUtility.InstanceIDToObject(id) != null)
                        {
                            mesh      = PolyMeshUtility.DeepCopy(mesh);
                            mesh.name = EditableObject.k_MeshInstancePrefix + gameObject.GetInstanceID();

                            var mf       = gameObject.GetComponent <MeshFilter>();
                            var sf       = gameObject.GetComponent <SkinnedMeshRenderer>();
                            var polyMesh = gameObject.GetComponent <PolybrushMesh>();

                            if (polyMesh != null)
                            {
                                polyMesh.SetMesh(mesh);
                                PrefabUtility.RecordPrefabInstancePropertyModifications(polyMesh);
                            }
                            else if (mf != null)
                            {
                                mf.sharedMesh = mesh;
                            }
                            else if (sf != null)
                            {
                                sf.sharedMesh = mesh;
                            }
                        }
                    }
                }
            };
        }
        private void Initialize(GameObject go)
        {
            CheckBackwardCompatiblity(go);

            gameObjectAttached = go;
            isProBuilderObject = false;

#if PROBUILDER_4_0_OR_NEWER
            if (ProBuilderBridge.ProBuilderExists())
            {
                isProBuilderObject = ProBuilderBridge.IsValidProBuilderMesh(gameObjectAttached);
            }
#endif
            Mesh         mesh         = null;
            MeshRenderer meshRenderer = gameObjectAttached.GetComponent <MeshRenderer>();
            meshFilter        = gameObjectAttached.GetComponent <MeshFilter>();
            _skinMeshRenderer = gameObjectAttached.GetComponent <SkinnedMeshRenderer>();

            originalMesh = go.GetMesh();

            if (originalMesh == null && _skinMeshRenderer != null)
            {
                originalMesh = _skinMeshRenderer.sharedMesh;
            }

            m_PolybrushMesh = gameObjectAttached.GetComponent <PolybrushMesh>();

            if (m_PolybrushMesh == null)
            {
                m_PolybrushMesh = Undo.AddComponent <PolybrushMesh>(gameObjectAttached);
                m_PolybrushMesh.Initialize();
                m_PolybrushMesh.mode = (s_UseAdditionalVertexStreams) ? PolybrushMesh.Mode.AdditionalVertexStream : PolybrushMesh.Mode.Mesh;
            }

            //attach the skinmesh ref to the polybrushmesh
            //it will be used when making a prefab containing a skin mesh. The limitation here is that the skin mesh must comes from an asset (which is 99.9999% of the time)
            if (_skinMeshRenderer != null)
            {
                Mesh sharedMesh = _skinMeshRenderer.sharedMesh;
                if (AssetDatabase.Contains(sharedMesh))
                {
                    m_PolybrushMesh.skinMeshRef = sharedMesh;
                }
            }

#if PROBUILDER_4_0_OR_NEWER
            // if it's a probuilder object rebuild the mesh without optimization
            if (isProBuilderObject)
            {
                if (ProBuilderBridge.IsValidProBuilderMesh(gameObjectAttached))
                {
                    ProBuilderBridge.ToMesh(gameObjectAttached);
                    ProBuilderBridge.Refresh(gameObjectAttached);
                }
            }
#endif

            if (meshRenderer != null || _skinMeshRenderer != null)
            {
                mesh = m_PolybrushMesh.storedMesh;

                if (mesh == null)
                {
                    mesh             = PolyMeshUtility.DeepCopy(originalMesh);
                    hadVertexStreams = false;
                }
                else
                {
                    //prevents leak
                    if (!MeshInstanceMatchesGameObject(mesh, gameObjectAttached))
                    {
                        mesh = PolyMeshUtility.DeepCopy(mesh);
                    }
                }

                mesh.name = k_MeshInstancePrefix + gameObjectAttached.GetInstanceID();
            }

            polybrushMesh.SetMesh(mesh);
            PrefabUtility.RecordPrefabInstancePropertyModifications(polybrushMesh);
            _graphicsMesh = m_PolybrushMesh.storedMesh;

            source = polybrushMesh.mode == PolybrushMesh.Mode.AdditionalVertexStream? ModelSource.AdditionalVertexStreams : PolyEditorUtility.GetMeshGUID(originalMesh);

            GenerateCompositeMesh();
        }