A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace UnityEditor.Tilemaps
5{
6 internal class TilePaletteSaveUtility
7 {
8 private static class Styles
9 {
10 public static readonly string invalidFolderTitle = L10n.Tr("Cannot save to an invalid folder");
11 public static readonly string invalidFolderContent = L10n.Tr("You cannot save to an invalid folder.");
12 public static readonly string nonAssetFolderTitle = L10n.Tr("Cannot save to a non-asset folder");
13 public static readonly string nonAssetFolderContent = L10n.Tr("You cannot save to a non-asset folder.");
14 public static readonly string readOnlyFolderTitle = L10n.Tr("Cannot save to a read-only path");
15 public static readonly string readOnlyFolderContent = L10n.Tr("You cannot save to a read-only path.");
16 public static readonly string ok = L10n.Tr("OK");
17 }
18
19 private class TilePaletteSaveScope : IDisposable
20 {
21 private GameObject m_GameObject;
22
23 public TilePaletteSaveScope(GameObject paletteInstance)
24 {
25 m_GameObject = paletteInstance;
26 if (m_GameObject != null)
27 {
28 GridPaintingState.savingPalette = true;
29 SetHideFlagsRecursively(paletteInstance, HideFlags.HideInHierarchy);
30 var renderers = paletteInstance.GetComponentsInChildren<Renderer>();
31 foreach (var renderer in renderers)
32 renderer.gameObject.layer = 0;
33 }
34 }
35
36 public void Dispose()
37 {
38 if (m_GameObject != null)
39 {
40 SetHideFlagsRecursively(m_GameObject, HideFlags.HideAndDontSave);
41 GridPaintingState.savingPalette = false;
42 }
43 }
44
45 private void SetHideFlagsRecursively(GameObject root, HideFlags flags)
46 {
47 root.hideFlags = flags;
48 for (int i = 0; i < root.transform.childCount; i++)
49 SetHideFlagsRecursively(root.transform.GetChild(i).gameObject, flags);
50 }
51 }
52
53 public static bool SaveTilePalette(GameObject originalPalette, GameObject paletteInstance)
54 {
55 var path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(originalPalette);
56 if (path == null)
57 return false;
58
59 using (new TilePaletteSaveScope(paletteInstance))
60 {
61 PrefabUtility.SaveAsPrefabAssetAndConnect(paletteInstance, path, InteractionMode.AutomatedAction);
62 }
63 return true;
64 }
65
66 public static bool ValidateSaveFolder(string folderPath)
67 {
68 if (string.IsNullOrEmpty(folderPath))
69 {
70 EditorUtility.DisplayDialog(Styles.invalidFolderTitle, Styles.invalidFolderContent, Styles.ok);
71 return false;
72 }
73 if (!AssetDatabase.TryGetAssetFolderInfo(folderPath, out bool rootFolder, out bool immutable))
74 {
75 EditorUtility.DisplayDialog(Styles.nonAssetFolderTitle, Styles.nonAssetFolderContent, Styles.ok);
76 return false;
77 }
78 if (immutable)
79 {
80 EditorUtility.DisplayDialog(Styles.readOnlyFolderTitle, Styles.readOnlyFolderContent, Styles.ok);
81 return false;
82 }
83 return true;
84 }
85 }
86}