Пример #1
0
    /// <summary>
    /// Upgrades empty brush (v1.x) into an empty brush (v2.x).
    /// </summary>
    /// <param name="record">Record for existing v1.x brush.</param>
    private void UpgradeEmptyBrush(RtsBrushAssetRecordWrapper record)
    {
        if (useNewAssets)
        {
            // Is this the original "Empty Variation" master brush?
            if (record.assetPath == "Assets/Rotorz Tile System/Brushes/Empty Variation.prefab")
            {
                // This is a special case that can be switched to new version
                // if it was imported.
                EmptyBrush newMasterBrush = AssetDatabase.LoadAssetAtPath("Assets/Rotorz/Tile System/TileBrushes/Master/Empty Variation.brush.asset", typeof(EmptyBrush)) as EmptyBrush;
                if (newMasterBrush != null)
                {
                    RtsUpgradedBrushMap.BrushMappings.SetMapping(record.brush, newMasterBrush);
                    return;
                }

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

        // Create the new empty brush.
        EmptyBrush newBrush = BrushUtility.CreateEmptyBrush(record.displayName);

        FinalizeStandaloneBrush(record, newBrush);

        // If we have upgraded the original empty variation brush then let's
        // change our minds and make it visible by default.
        if (record.assetPath == "Assets/Rotorz Tile System/Brushes/Empty Variation.prefab")
        {
            newBrush.visibility = BrushVisibility.Shown;
            EditorUtility.SetDirty(newBrush);
        }
    }
Пример #2
0
    /// <summary>
    /// Upgrades alias brush (v1.x) into an alias brush (v2.x).
    /// </summary>
    /// <param name="record">Record for existing v1.x brush.</param>
    private void UpgradeAliasBrush(RtsBrushAssetRecordWrapper record)
    {
        AliasBrush newBrush;

        if (useNewAssets)
        {
            // Is this the original "Grass Block" or "Cave Block" example brushes?
            if (record.assetPath == "Assets/Rotorz Tile System/TilePrefabs/Cave Block.prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/TilePrefabs/Cave Block (Join).prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/TilePrefabs/Cave Block (Small).prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/TilePrefabs/Grass Block.prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/TilePrefabs/Grass Block (Join).prefab" ||
                record.assetPath == "Assets/Rotorz Tile System/TilePrefabs/Grass Block (Small).prefab")
            {
                // This is a special case that can be switched to new version
                // if it was imported.
                newBrush = AssetDatabase.LoadAssetAtPath("Assets/Rotorz/Tile System/Demo/TileBrushes/" + record.displayName.Replace("Block", "Platform") + ".brush.asset", typeof(AliasBrush)) as AliasBrush;
                if (newBrush != null)
                {
                    RtsUpgradedBrushMap.BrushMappings.SetMapping(record.brush, newBrush);
                    return;
                }

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

        Brush targetBrush = RtsUpgradedBrushMap.BrushMappings.Lookup((Object)_fiAliasTileBrush_aliasOf.GetValue(record.brush));

        newBrush = BrushUtility.CreateAliasBrush(record.displayName, targetBrush);

        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 (_fiAliasTileBrush_overrideFlags != null)
        {
            newBrush.overrideFlags = (bool)_fiAliasTileBrush_overrideFlags.GetValue(record.brush);
        }
        if (_fiAliasTileBrush_overrideTransforms != null)
        {
            newBrush.overrideTransforms = (bool)_fiAliasTileBrush_overrideTransforms.GetValue(record.brush);
        }

        FinalizeStandaloneBrush(record, newBrush);
    }
Пример #3
0
    /// <summary>
    /// Finalize a newly created standalone brush asset.
    /// </summary>
    /// <remarks>
    /// <para>Also moves asset into "Master" folder if needed.</para>
    /// </remarks>
    /// <param name="record">Record for v1.x brush.</param>
    /// <param name="newBrush">The new brush.</param>
    private void FinalizeStandaloneBrush(RtsBrushAssetRecordWrapper record, Brush newBrush)
    {
        CopyCommonBrushProperties(newBrush, record.brush);
        RtsUpgradedBrushMap.BrushMappings.SetMapping(record.brush, newBrush);

        // Was previous brush a master?
        if (record.master)
        {
            string newAssetPath    = AssetDatabase.GetAssetPath(newBrush);
            string masterAssetPath = newAssetPath.Replace(BrushUtility.GetBrushAssetPath(), BrushUtility.GetMasterBrushAssetPath());
            AssetDatabase.MoveAsset(newAssetPath, masterAssetPath);
        }

        EditorUtility.SetDirty(newBrush);
    }
Пример #4
0
    /// <summary>
    /// Upgrades basic brush (v1.x) into an oriented brush (v2.x).
    /// </summary>
    /// <remarks>
    /// <para>The concept of basic brushes was removed in v2.x of Rotorz Tile System.
    /// Instead the default orientation of an oriented brush can be used.</para>
    /// </remarks>
    /// <param name="record">Record for existing v1.x brush.</param>
    private void UpgradeBasicBrush(RtsBrushAssetRecordWrapper record)
    {
        // We need to take a copy of the original brush because that is the
        // tile prefab, but we'll remove the original brush component.
        GameObject tilePrefabCopyGO = PrefabUtility.InstantiatePrefab(record.brush.gameObject) as GameObject;
        GameObject newTilePrefab    = null;

        try {
            Object.DestroyImmediate(tilePrefabCopyGO.GetComponent(_tyTileBrush));

            // Create a new prefab for this prefab.
            newTilePrefab = PrefabUtility.CreatePrefab(GetUniqueMigratedPath(record.displayName + ".prefab"), tilePrefabCopyGO);
        }
        finally {
            // Destroy the temporary copy.
            Object.DestroyImmediate(tilePrefabCopyGO);
        }

        if (newTilePrefab == null)
        {
            Debug.LogError(string.Format("An error occurred whilst upgrading brush '{0}' of type '{1}'.", record.displayName, record.brush.GetType().FullName));
            return;
        }

        // Basic brushes will now become oriented brushes!
        OrientedBrush newBrush = BrushUtility.CreateOrientedBrush(record.displayName);

        newBrush.DefaultOrientation.variations = new Object[] { newTilePrefab };

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

        FinalizeStandaloneBrush(record, newBrush);

        // Do not override tag and layer by default.
        newBrush.overrideTag   = false;
        newBrush.overrideLayer = false;
    }
Пример #5
0
    /// <summary>
    /// Upgrades atlas brush (v1.x) into a tileset brush (v2.x).
    /// </summary>
    /// <remarks>
    /// <para>These are now called tileset brushes because they utilise a central
    /// tileset asset which makes it easier to manage such brushes.</para>
    /// </remarks>
    /// <param name="record">Record for existing v1.x brush.</param>
    private void UpgradeAtlasBrush(RtsBrushAssetRecordWrapper record)
    {
        MonoBehaviour oldBrush     = record.brush;
        Transform     oldTransform = oldBrush.transform;

        // Cannot upgrade atlas tile brush if tileset was not generated
        Tileset tileset = GenerateTilesetFromAtlasBrush(oldBrush);

        if (tileset == null)
        {
            Debug.LogError(string.Format("Could not generate tileset for atlas brush '{0}'.", oldBrush.name));
            return;
        }

        Texture2D atlasTexture   = (Texture2D)_fiAtlasTileBrush_atlasTexture.GetValue(oldBrush);
        int       atlasTileWidth = (int)_fiAtlasTileBrush_atlasTileWidth.GetValue(oldBrush);
        int       atlasRow       = (int)_fiAtlasTileBrush_atlasRow.GetValue(oldBrush);
        int       atlasColumn    = (int)_fiAtlasTileBrush_atlasColumn.GetValue(oldBrush);

        // Create the new tileset brush.
        int          atlasColumns = atlasTexture.width / atlasTileWidth;
        int          tileIndex    = atlasRow * atlasColumns + atlasColumn;
        TilesetBrush newBrush     = BrushUtility.CreateTilesetBrush(record.displayName, tileset, tileIndex, InheritYesNo.Inherit);

        // Was unit collider added to original atlas brush?
        BoxCollider automaticCollider = oldBrush.collider as BoxCollider;

        if (automaticCollider != null && automaticCollider.size == Vector3.one && automaticCollider.center == Vector3.zero)
        {
            newBrush.addCollider = true;
        }

        int componentCount = oldTransform.GetComponents <Component>().Length;

        if (newBrush.addCollider)
        {
            --componentCount;
        }

        // Should prefab be generated and attached?
        //   - Attach prefab if it contains child game objects.
        //   - Attach prefab if collider is non-standard.
        //   - Contains extra components (1=transform, 2=brush, 3=filter, 4=renderer).
        bool attachPrefab = (oldTransform.childCount > 0) ||
                            (!newBrush.addCollider && oldBrush.collider != null) ||
                            (componentCount != 4)
        ;

        if (attachPrefab)
        {
            GameObject attachment = PrefabUtility.InstantiatePrefab(oldBrush.gameObject) as GameObject;

            // Destroy the previous brush component.
            Object.DestroyImmediate(attachment.GetComponent(_tyTileBrush));
            // Destroy collider as it's not needed.
            if (newBrush.addCollider)
            {
                Object.DestroyImmediate(attachment.collider);
            }

            // Remove mesh filter and renderer components.
            Object.DestroyImmediate(attachment.renderer);
            Object.DestroyImmediate(attachment.GetComponent <MeshFilter>());

            string assetPath = GetUniqueMigratedPath(oldBrush.name + ".prefab");
            newBrush.attachPrefab = PrefabUtility.CreatePrefab(assetPath, attachment);

            Object.DestroyImmediate(attachment);
        }

        CopyCommonBrushProperties(newBrush, record.brush);
        RtsUpgradedBrushMap.BrushMappings.SetMapping(record.brush, newBrush);

        if (newBrush.visibility == BrushVisibility.Shown)
        {
            newBrush.visibility = BrushVisibility.Favourite;
        }
        else
        {
            newBrush.visibility = BrushVisibility.Shown;
        }
    }
Пример #6
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);
    }