Ejemplo n.º 1
0
        /// <summary>
        /// Ensures that <see cref="ProceduralMesh"/> component is presents and returns it.
        /// </summary>
        /// <returns>
        /// The <see cref="ProceduralMesh"/> component.
        /// </returns>
        internal ProceduralMesh PrepareProceduralMesh()
        {
            if (this.ProceduralMesh == null)
            {
                var t = this.transform;
                this.proceduralMesh = t.GetComponentInChildren <ProceduralMesh>();

                if (this.proceduralMesh == null)
                {
                    // Entire procedural mesh component is missing, restore it!
                    var proceduralGO        = new GameObject("_procedural");
                    var proceduralTransform = proceduralGO.transform;
                    proceduralTransform.SetParent(t, false);
                    proceduralTransform.localPosition = new Vector3(
                        this.firstColumn * this.tileSystem.CellSize.x,
                        this.firstRow * -this.tileSystem.CellSize.y,
                        0f
                        );

                    this.proceduralMesh = proceduralGO.AddComponent <ProceduralMesh>();
                    this.proceduralMesh.InitialUpdateMesh();

                    InternalUtility.HideEditorWireframe(proceduralGO);
                }
                else if (this.proceduralMesh.mesh == null)
                {
                    // Procedural mesh component is available, but mesh is missing.
                    this.proceduralMesh.InitialUpdateMesh();

                    InternalUtility.HideEditorWireframe(this.proceduralMesh.gameObject);
                }
            }

            return(this.proceduralMesh);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Apply runtime stripping options to a tile system.
        /// </summary>
        /// <remarks>
        /// <para>At runtime only a basic level of stripping will be automatically applied
        /// to tile system upon <c>Awake</c> when runtime stripping is enabled. Tile systems
        /// should be optimised to take full advantage of the provided stripping capabilities.</para>
        /// </remarks>
        /// <param name="tileSystem">Tile system.</param>
        public static void ApplyRuntimeStripping(TileSystem tileSystem)
        {
            if (tileSystem.chunks != null)
            {
                // Does tile data need to be stripped?
                if (tileSystem.StripSystemComponent || tileSystem.StripChunkMap || tileSystem.StripTileData)
                {
                    foreach (var chunk in tileSystem.chunks)
                    {
                        if (chunk == null)
                        {
                            continue;
                        }

                        chunk.tiles = null;
                        InternalUtility.Destroy(chunk);
                    }

                    // Tile system is no longer editable.
                    tileSystem.isEditable = false;
                }
                // If not, should brush references be stripped from tile data?
                else if (tileSystem.StripBrushReferences)
                {
                    foreach (var chunk in tileSystem.chunks)
                    {
                        if (chunk == null || chunk.tiles == null)
                        {
                            continue;
                        }

                        foreach (var tile in chunk.tiles)
                        {
                            if (tile != null)
                            {
                                tile.brush = null;
                            }
                        }
                    }
                }

                // Is chunk map to be stripped?
                if (tileSystem.StripChunkMap)
                {
                    tileSystem.chunks = null;

                    // Tile system is no longer editable.
                    tileSystem.isEditable = false;
                }
            }

            // Should this tile system component be stripped?
            if (tileSystem.StripSystemComponent)
            {
                InternalUtility.Destroy(tileSystem);
            }

            // Runtime stripping has already been applied!
            tileSystem.applyRuntimeStripping = false;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Strip chunk game objects from tile system and reparent tile objects into game
        /// object of tile system.
        /// </summary>
        /// <param name="tileSystem">Tile system.</param>
        private static void StripChunks(TileSystem tileSystem)
        {
            if (tileSystem.chunks != null)
            {
                foreach (var chunk in tileSystem.chunks)
                {
                    if (chunk == null)
                    {
                        continue;
                    }

                    ReparentChildren(chunk.transform, tileSystem.transform);

                    // Dereference tile data and destroy associated game object.
                    chunk.tiles = null;
                    InternalUtility.Destroy(chunk.gameObject);
                }

                // Clear chunk map.
                tileSystem.chunks = null;
            }

            // Tile system is no longer editable.
            tileSystem.isEditable = false;
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Strip game object when it contains no components and has no children.
 /// </summary>
 /// <param name="obj">Transform of game object to consider stripping.</param>
 /// <returns>
 /// A value of <c>true</c> if game object was stripped; otherwise <c>false</c>.
 /// </returns>
 public static bool StripEmptyGameObject(Transform obj)
 {
     if (obj.childCount == 0)
     {
         var components = obj.GetComponents <Component>();
         if (components.Length == 1 && components[0] is Transform)
         {
             InternalUtility.Destroy(obj.gameObject);
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Strip plop instance and group components for tile system.
        /// </summary>
        /// <param name="system">Tile system.</param>
        private static void StripPlopComponents(TileSystem system)
        {
            // Strip `PlopGroup` components from tile system.
            foreach (var plopGroup in system.GetComponentsInChildren <PlopGroup>())
            {
                InternalUtility.Destroy(plopGroup);
            }

            // Strip `PlopInstance` components which are associated from tile system.
            foreach (var plopInstance in Resources.FindObjectsOfTypeAll <PlopInstance>())
            {
                if (plopInstance.Owner == system)
                {
                    InternalUtility.Destroy(plopInstance);
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Remove empty chunk game objects.
        /// </summary>
        /// <param name="tileSystem">Tile system.</param>
        private static void StripEmptyChunks(TileSystem tileSystem)
        {
            if (tileSystem.chunks == null)
            {
                return;
            }

            Transform chunkTransform;

            Component[] components;
            int         extraCount;

            for (int i = 0; i < tileSystem.chunks.Length; ++i)
            {
                if (tileSystem.chunks[i] == null)
                {
                    continue;
                }

                chunkTransform = tileSystem.chunks[i].transform;

                // Strip chunk if it doesn't contain any child game objects and if it
                // doesn't contain extra components.
                if (chunkTransform.childCount == 0)
                {
                    components = chunkTransform.GetComponents <Component>();

                    // Count extra components (other than Transform and Chunk).
                    extraCount = components.Length;
                    for (int j = 0; j < components.Length; ++j)
                    {
                        if (components[j] is Transform || components[j] is Chunk)
                        {
                            --extraCount;
                        }
                    }

                    // Chunk can be stripped if there are no extra components.
                    if (extraCount == 0)
                    {
                        InternalUtility.Destroy(chunkTransform.gameObject);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        /// <inheritdoc/>
        protected internal override void PostProcessTile(IBrushContext context, TileData tile)
        {
            if (tile == null)
            {
                return;
            }

            var go = tile.gameObject;

            if (go != null)
            {
                // Assign tag and layer to tile.
                go.tag   = this.tag;
                go.layer = this.layer;

                InternalUtility.HideEditorWireframe(go);
            }
        }
Ejemplo n.º 8
0
 private void OnDestroy()
 {
     //!HACK: Workaround bug in Unity where delayed action of Object.Destroy
     //       is not respected when stopping play mode in-editor.
     InternalUtility.DestroyImmediate(this.mesh);
 }