예제 #1
0
    /// <summary>
    /// Upgrades oriented brush (v1.x) into an oriented brush (v2.x).
    /// </summary>
    /// <param name="record">Record for existing v1.x brush.</param>
    private void UpgradeOrientedBrush(RtsBrushAssetRecordWrapper record)
    {
        if (useNewAssets)
        {
            // Is this the original "Smooth Platform" master brushes?
            if (record.assetPath == "Assets/Rotorz Tile System/Brushes/Smooth Platform.prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/Brushes/Smooth Platform (Join).prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/Brushes/Smooth Platform (Small).prefab")
            {
                // This is a special case that can be switched to new version
                // if it was imported.
                OrientedBrush newMasterBrush = AssetDatabase.LoadAssetAtPath("Assets/Rotorz/Tile System/TileBrushes/Master/" + record.displayName + ".brush.asset", typeof(OrientedBrush)) as OrientedBrush;
                if (newMasterBrush != null)
                {
                    RtsUpgradedBrushMap.BrushMappings.SetMapping(record.brush, newMasterBrush);
                    return;
                }

                // If new version was not found then just proceed to upgrade
                // existing version.
            }
            // Is this one of the original demo brushes?
            else if (record.assetPath == "Assets/Rotorz Tile System/_Demos/TilePrefabs/Diamond.prefab" ||
                     record.assetPath == "Assets/Rotorz Tile System/_Demos/TilePrefabs/Hat Guy.prefab" ||
                     record.assetPath == "Assets/Rotorz Tile System/_Demos/TilePrefabs/Steel Brick.prefab")
            {
                // This is a special case that can be switched to new version
                // if it was imported.
                OrientedBrush newDemoBrush = AssetDatabase.LoadAssetAtPath("Assets/Rotorz/Tile System/Demo/Hat Guy/TileBrushes/" + record.displayName + ".brush.asset", typeof(OrientedBrush)) as OrientedBrush;
                if (newDemoBrush != null)
                {
                    RtsUpgradedBrushMap.BrushMappings.SetMapping(record.brush, newDemoBrush);
                    return;
                }

                // If new version was not found then just proceed to upgrade
                // existing version.
            }
        }

        Transform oldBrushTransform = record.brush.transform;

        OrientedBrush newBrush = BrushUtility.CreateOrientedBrush(record.displayName);

        newBrush.RemoveOrientation(newBrush.DefaultOrientationMask);
        List <Object> variations = new List <Object>();

        RtsUpgradedBrushMap map = RtsUpgradedBrushMap.BrushMappings;

        // Copy orientations from old brush.
        for (int ti = 0; ti < oldBrushTransform.childCount; ++ti)
        {
            MonoBehaviour oldOrientation = oldBrushTransform.GetChild(ti).GetComponent(_tyTileBrushOrientation) as MonoBehaviour;
            if (oldOrientation == null)
            {
                continue;
            }

            Object[] oldVariations = (Object[])_fiTileBrushOrientation_variations.GetValue(oldOrientation);

            BrushOrientation newOrientation = newBrush.AddOrientation(OrientationUtility.MaskFromName(oldOrientation.name));

            variations.Clear();
            for (int i = 0; i < oldVariations.Length; ++i)
            {
                if (oldVariations[i] == null)
                {
                    continue;
                }

                Type       variationType  = oldVariations[i].GetType();
                GameObject variationGO    = oldVariations[i] as GameObject;
                Object     variationBrush = null;

                // If game object is nested, check if it is a tile brush prefab!
                if (variationGO != null)
                {
                    variationBrush = variationGO.GetComponent(_tyTileBrush);
                }
                // If variation is a tile brush then...
                else if (_tyTileBrush.IsAssignableFrom(variationType))
                {
                    variationBrush = oldVariations[i];
                }

                // Need to isolate nested brushes!
                if (variationBrush != null)
                {
                    // Note: This only works because oriented brushes are processed last,
                    //		 and it is not possible to nest oriented brushes.

                    // Use new version of brush!
                    Brush replacementBrush = map.Lookup(variationBrush);
                    if (replacementBrush != null)
                    {
                        variations.Add(replacementBrush);
                    }
                }
                else if (variationGO != null)
                {
                    variations.Add(variationGO);
                }
            }
            newOrientation.variations = variations.ToArray();
        }

        newBrush.DefaultOrientationMask = OrientationUtility.MaskFromName((string)_fiOrientedTileBrush_defaultOrientation.GetValue(record.brush));
        newBrush.FallbackMode           = (FallbackMode)_fiOrientedTileBrush_fallbackMode.GetValue(record.brush);

        if (_fiTileBrush_coalesce != null)
        {
            newBrush.Coalesce = (Coalesce)_fiTileBrush_coalesce.GetValue(record.brush);
        }
        if (_fiTileBrush_coalesceTileGroup != null)
        {
            newBrush.CoalesceBrushGroup = (int)_fiTileBrush_coalesceTileGroup.GetValue(record.brush);
        }

        if (_fiOrientedTileBrush_forceOverrideFlags != null)
        {
            newBrush.forceOverrideFlags = (bool)_fiOrientedTileBrush_forceOverrideFlags.GetValue(record.brush);
        }

        FinalizeStandaloneBrush(record, newBrush);
    }
    /// <summary>
    /// Upgrade tile system from v1.0.0-v1.0.8 to v2.0.0.
    /// </summary>
    /// <remarks>
    /// <para>Replicates upgrade process that was included in v1.0.9+ but converts straight
    /// to v2.0.0 instead of v1.0.9.</para>
    /// </remarks>
    /// <param name="v1">Old tile system.</param>
    public static void UpgradeTileSystemA(MonoBehaviour v1)
    {
        RtsUpgradedBrushMap map = RtsBrushUpgradeUtility.BrushMappings;

        EditorUtility.DisplayProgressBar("Upgrade Tile System", "Initializing new data structure...", 0.0f);
        try {
            PropertyInfo piTileData_hasGameObject = typeof(TileData).GetProperty("HasGameObject", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            Vector3 tileSize = (Vector3)_fiTileSystem_tileSize.GetValue(v1);
            int     rows     = (int)_fiTileSystem_rows.GetValue(v1);
            int     columns  = (int)_fiTileSystem_columns.GetValue(v1);

            // Create v2.x tile system.
            TileSystem v2 = v1.gameObject.AddComponent <TileSystem>();
            v2.CreateSystem(tileSize.x, tileSize.y, tileSize.z, rows, columns, 30, 30);
            CopyProperties(v1, v2);

            // Assume value that was consistent with original default settings
            v2.applyRuntimeStripping = true;
            v2.StrippingPreset       = StrippingPreset.NoStripping;
            v2.BeginBulkEdit();

            Component[] instanceComponents = v1.GetComponentsInChildren(_tyTileInstance, true);

            float task      = 0.0f;
            float taskCount = instanceComponents.Length;
            float taskRatio = 1.0f / taskCount;

            TileData tile = new TileData();

            // Retrieve all tile instance components
            foreach (MonoBehaviour instance in instanceComponents)
            {
                EditorUtility.DisplayProgressBar("Upgrade Tile System", "Processing tile data...", (task++) * taskRatio);

                int row    = (int)_fiTileInstance_row.GetValue(instance);
                int column = (int)_fiTileInstance_column.GetValue(instance);

                tile.Clear();

                // Create and assign tile data
                tile.brush           = map.Lookup((Object)_piTileInstance_brush.GetValue(instance, null));
                tile.orientationMask = (byte)OrientationUtility.MaskFromName((string)_fiTileInstance_orientationName.GetValue(instance));
                tile.variationIndex  = (byte)(int)_fiTileInstance_variationIndex.GetValue(instance);
                tile.Empty           = false;
                tile.gameObject      = instance.gameObject;
                piTileData_hasGameObject.SetValue(tile, true, null);

                v2.SetTileFrom(row, column, tile);

                Chunk chunk = v2.GetChunkFromTileIndex(row, column);
                ForceRepaintForAtlasTiles(v2.GetTile(row, column), chunk);
                if (instance == null)
                {
                    continue;
                }

                // Cleanup original tile instance?
                if (!StrippingUtility.StripEmptyGameObject(instance.transform))
                {
                    // Reparent game object to its shiny new chunk!
                    instance.gameObject.transform.parent = chunk.transform;
                }

                // Destroy unwanted tile instance component
                Object.DestroyImmediate(instance);
            }

            int count = v2.EndBulkEdit();
            RemoveTileSystem(v1);

            if (count > 0)
            {
                Debug.Log(string.Format("Upgrade of tile system '{0}' completed and {1} tile(s) were force refreshed.", v2.name, count));
            }
            else
            {
                Debug.Log(string.Format("Upgrade of tile system '{0}' completed.", v2.name));
            }
        }
        finally {
            EditorUtility.ClearProgressBar();
        }
    }