Ejemplo n.º 1
0
        private static void TransformTileSystemUsingPreset(TileSystemPreset preset, Transform tileSystemTransform)
        {
            switch (preset.Direction)
            {
            default:
            case WorldDirection.Forward:
                break;

            case WorldDirection.Backward:
                tileSystemTransform.Rotate(Vector3.up, 180f, Space.Self);
                break;

            case WorldDirection.Up:
                tileSystemTransform.Rotate(Vector3.right, 90f, Space.Self);
                break;

            case WorldDirection.Down:
                tileSystemTransform.Rotate(Vector3.right, 270f, Space.Self);
                break;

            case WorldDirection.Left:
                tileSystemTransform.Rotate(Vector3.up, 90f, Space.Self);
                break;

            case WorldDirection.Right:
                tileSystemTransform.Rotate(Vector3.up, 270f, Space.Self);
                break;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new tile system using properties from the specified preset.
        /// </summary>
        /// <remarks>
        /// <para>This method does not automatically record the new game object with
        /// Unity's undo system. If undo functionality is desired then the callee should
        /// do this.</para>
        /// </remarks>
        /// <param name="preset">Tile system preset.</param>
        /// <returns>
        /// A new game object with an initialized <see cref="TileSystem"/>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="preset"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// <list type="bullet">
        /// <item>If preset defines an invalid name for a tile system.</item>
        /// <item>If preset defines a tile system with less than one cell.</item>
        /// </list>
        /// </exception>
        public static GameObject CreateTileSystemFromPreset(TileSystemPreset preset)
        {
            if (preset == null)
            {
                throw new ArgumentNullException("preset");
            }

            string name = preset.SystemName.Trim();

            if (string.IsNullOrEmpty(name))
            {
                throw new InvalidOperationException("Invalid name for tile system.");
            }
            if (preset.Rows < 1 || preset.Columns < 1)
            {
                throw new InvalidOperationException("Tile system must have at least one cell.");
            }

            // Create empty game object and add tile system component.
            var go         = new GameObject(name);
            var tileSystem = go.AddComponent <TileSystem>();

            tileSystem.CreateSystem(preset.TileWidth, preset.TileHeight, preset.TileDepth, preset.Rows, preset.Columns, preset.ChunkWidth, preset.ChunkHeight);

            TransformTileSystemUsingPreset(preset, go.transform);
            SetTileSystemPropertiesFromPreset(preset, tileSystem);

            // Place at end of the scene palette listing.
            tileSystem.sceneOrder = int.MaxValue;

            ToolUtility.RepaintScenePalette();

            return(go);
        }
Ejemplo n.º 3
0
        private static TileSystemPreset SavePresetAsHelper(TileSystemPreset source, string presetAssetPath)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            if (string.IsNullOrEmpty(presetAssetPath))
            {
                throw new ArgumentNullException("presetAssetPath");
            }

            // Load existing preset if it exists.
            var preset = AssetDatabase.LoadAssetAtPath(presetAssetPath, typeof(TileSystemPreset)) as TileSystemPreset;

            if (preset != null)
            {
                EditorUtility.CopySerialized(source, preset);
                EditorUtility.SetDirty(preset);
                AssetDatabase.SaveAssets();
            }
            else
            {
                preset      = Object.Instantiate(source) as TileSystemPreset;
                preset.name = Regex.Match(presetAssetPath, @"/([a-z0-9_\- ]+)\.asset$", RegexOptions.IgnoreCase).Groups[1].Value;
                AssetDatabase.CreateAsset(preset, presetAssetPath);
                AssetDatabase.Refresh();
            }

            return(preset);
        }
 private void VerifySelectedPresetValue()
 {
     if (this.selectedPreset == null && !ReferenceEquals(this.selectedPreset, null))
     {
         this.selectedPreset        = null;
         s_SelectedPresetGuid.Value = "";
     }
 }
        /// <summary>
        /// Set the currently selected preset.
        /// </summary>
        /// <param name="presetGuid">Name of preset.</param>
        private void SetSelectedPreset(string presetGuid)
        {
            this.selectedPreset = null;

            // Figure out a valid preset GUID.
            if (presetGuid != "F:3D" && presetGuid != "F:2D")
            {
                this.selectedPreset = TileSystemPresetUtility.LoadPresetFromGUID(presetGuid);
                if (this.selectedPreset == null)
                {
                    presetGuid = TileSystemPresetUtility.DefaultPresetGUID;
                }
            }

            // Persist current preset selection.
            s_SelectedPresetGuid.Value = presetGuid;

            // Preserve user input for tile system name!
            string preserveSystemName = this.currentPreset.SystemName;

            // Update the new tile system configuration from the selected preset.
            switch (presetGuid)
            {
            case "F:3D":
                this.currentPreset.SetDefaults3D();
                this.currentPreset.name = TileLang.ParticularText("Preset Name", "Default: 3D");
                this.newPresetName      = "";
                break;

            case "F:2D":
                this.currentPreset.SetDefaults2D();
                this.currentPreset.name = TileLang.ParticularText("Preset Name", "Default: 2D");
                this.newPresetName      = "";
                break;

            default:
                EditorUtility.CopySerialized(this.selectedPreset, this.currentPreset);
                this.newPresetName = this.currentPreset.name;
                break;
            }

            // Remove input focus from any control but most specifically "Preset Name".
            GUIUtility.keyboardControl = 0;

            if (this.currentPresetInspector.HasModifiedTileSystemName)
            {
                this.currentPreset.SystemName = preserveSystemName;
            }
            else
            {
                this.AutoAddPostfixToName();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Determines whether the specified tile system preset is located within the
        /// main "User Data" directory structure.
        /// </summary>
        /// <param name="preset">Tile system preset.</param>
        /// <returns>
        /// A value of <c>true</c> if the preset is located in the main "User Data"
        /// directory; otherwise, a value of <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="preset"/> is <c>null</c>.
        /// </exception>
        public static bool IsUserPreset(TileSystemPreset preset)
        {
            if (preset == null)
            {
                throw new ArgumentNullException("preset");
            }

            string presetAssetPath     = AssetDatabase.GetAssetPath(preset);
            string userPresetsBasePath = PackageUtility.ResolveDataAssetPath("@rotorz/unity3d-tile-system", "Presets") + "/";

            return(presetAssetPath.StartsWith(userPresetsBasePath));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Overwrites an existing tile system preset by copying the properties of the
        /// source preset into the destination preset.
        /// </summary>
        /// <param name="source">Source preset.</param>
        /// <param name="dest">Destination preset.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <list type="bullet">
        /// <item>If <paramref name="source"/> is <c>null</c>.</item>
        /// <item>If <paramref name="dest"/> is <c>null</c>.</item>
        /// </list>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// If <paramref name="dest"/> is not a persisted asset file.
        /// </exception>
        public static void OverwritePreset(TileSystemPreset source, TileSystemPreset dest)
        {
            if (dest == null)
            {
                throw new ArgumentNullException("dest");
            }
            if (!EditorUtility.IsPersistent(dest))
            {
                throw new InvalidOperationException("Cannot overwrite non-persistent preset.");
            }

            string presetAssetPath = AssetDatabase.GetAssetPath(dest);

            SavePresetAsHelper(source, presetAssetPath);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Creates a new tile system preset by copying the properties of the source
        /// preset. If a preset already exists with the same name then it will be
        /// overwritten.
        /// </summary>
        /// <param name="source">Source preset.</param>
        /// <param name="presetName">Name of the new preset.</param>
        /// <returns>
        /// A reference to the new <see cref="TileSystemPreset"/> asset.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">
        /// <list type="bullet">
        /// <item>If <paramref name="source"/> is <c>null</c>.</item>
        /// <item>If <paramref name="presetName"/> is <c>null</c>.</item>
        /// </list>
        /// </exception>
        /// <exception cref="System.ArgumentException">
        /// If <paramref name="presetName"/> is not a valid preset name.
        /// </exception>
        public static TileSystemPreset CreatePreset(TileSystemPreset source, string presetName)
        {
            if (presetName == null)
            {
                throw new ArgumentNullException("presetName");
            }
            if (!IsValidPresetName(presetName))
            {
                throw new ArgumentException("Invalid preset name.", "presetName");
            }

            string presetAssetPath = PackageUtility.GetDataAssetPath("@rotorz/unity3d-tile-system", "Presets", presetName.Trim() + ".asset");

            return(SavePresetAsHelper(source, presetAssetPath));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Deletes an unwanted tile system preset.
        /// </summary>
        /// <param name="preset">Tile system preset.</param>
        /// <exception cref="System.ArgumentNullException">
        /// If <paramref name="preset"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.InvalidOperationException">
        /// If <paramref name="preset"/> is not a known asset file.
        /// </exception>
        public static void DeletePreset(TileSystemPreset preset)
        {
            if (preset == null)
            {
                throw new ArgumentNullException("preset");
            }
            if (!AssetDatabase.Contains(preset))
            {
                throw new InvalidOperationException(string.Format("Cannot delete preset '{0}' because it is not an asset file.", preset.name));
            }

            string assetPath = AssetDatabase.GetAssetPath(preset);

            AssetDatabase.MoveAssetToTrash(assetPath);

            PackageUtility.DeleteDataFolderIfEmpty("@rotorz/unity3d-tile-system", "Presets");
        }
Ejemplo n.º 10
0
        private static void SetTileSystemPropertiesFromPreset(TileSystemPreset preset, TileSystem tileSystem)
        {
            // Grid
            tileSystem.TilesFacing = preset.TilesFacing;

            // Stripping
            tileSystem.StrippingPreset = preset.StrippingPreset;
            if (preset.StrippingPreset == StrippingPreset.Custom)
            {
                tileSystem.StrippingOptions = preset.StrippingOptions;
            }

            // Build Options
            tileSystem.combineMethod        = preset.CombineMethod;
            tileSystem.combineChunkWidth    = preset.CombineChunkWidth;
            tileSystem.combineChunkHeight   = preset.CombineChunkHeight;
            tileSystem.combineIntoSubmeshes = preset.CombineIntoSubmeshes;
            tileSystem.staticVertexSnapping = preset.StaticVertexSnapping;
            tileSystem.vertexSnapThreshold  = preset.VertexSnapThreshold;

            tileSystem.GenerateSecondUVs   = preset.GenerateSecondUVs;
            tileSystem.SecondUVsHardAngle  = preset.GenerateSecondUVsParams.hardAngle;
            tileSystem.SecondUVsPackMargin = preset.GenerateSecondUVsParams.packMargin;
            tileSystem.SecondUVsAngleError = preset.GenerateSecondUVsParams.angleError;
            tileSystem.SecondUVsAreaError  = preset.GenerateSecondUVsParams.areaError;

            tileSystem.pregenerateProcedural = preset.PregenerateProcedural;

            tileSystem.ReduceColliders.SetFrom(preset.ReduceColliders);

            // Runtime Options
            tileSystem.hintEraseEmptyChunks  = preset.HintEraseEmptyChunks;
            tileSystem.applyRuntimeStripping = preset.ApplyRuntimeStripping;

            tileSystem.updateProceduralAtStart = preset.UpdateProceduralAtStart;
            tileSystem.MarkProceduralDynamic   = preset.MarkProceduralDynamic;
            tileSystem.addProceduralNormals    = preset.AddProceduralNormals;

            tileSystem.SortingLayerID = preset.SortingLayerID;
            tileSystem.SortingOrder   = preset.SortingOrder;
        }
        /// <inheritdoc/>
        protected override void DoEnable()
        {
            AutoInitializeUserSettings();

            this.titleContent = new GUIContent(TileLang.ParticularText("Action", "Create Tile System"));
            this.InitialSize  = this.minSize = new Vector2(580, 375);
            this.maxSize      = new Vector2(580, Screen.currentResolution.height);

            if (this.currentPreset == null)
            {
                this.currentPreset           = ScriptableObject.CreateInstance <TileSystemPreset>();
                this.currentPreset.hideFlags = HideFlags.DontSave;
            }

            if (this.currentPresetInspector == null)
            {
                this.currentPresetInspector = (TileSystemPresetInspector)UnityEditor.Editor.CreateEditor(this.currentPreset, typeof(TileSystemPresetInspector));
            }
            this.currentPresetInspector.DisableUndoOnSerializedObject = true;

            // Restore previous preset selection?
            this.SetSelectedPreset(s_SelectedPresetGuid);
        }
Ejemplo n.º 12
0
 internal static string GetPresetGUID(TileSystemPreset preset)
 {
     return(preset != null
         ? AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(preset))
         : null);
 }