1+ using System ;
2+ using System . IO ;
3+ using TarasK8 . SaveSystem ;
4+ using UnityEditor ;
5+ using UnityEngine ;
6+
7+ namespace TarasK8 . SaveSystemEditor
8+ {
9+ public class AssetsContainerEditor : EditorWindow
10+ {
11+ private AssetsContainer _container ;
12+ private SerializedObject _serializedObject ;
13+ private SerializedProperty _paths ;
14+ private SerializedProperty _assets ;
15+
16+ private Vector2 _assetsScroll ;
17+ private Vector2 _pathsScroll ;
18+
19+ [ MenuItem ( "Window/Saveable References" ) ]
20+ public static void ShowWindow ( )
21+ {
22+ GetWindow < AssetsContainerEditor > ( "Saveable References" ) . Show ( ) ;
23+ }
24+
25+ [ MenuItem ( "Assets/Add to Saveable References" ) ]
26+ public static void AddToContainer ( )
27+ {
28+ AssetsContainer container = GetOrCreateContainer ( ) ;
29+ UnityEngine . Object [ ] objects = Selection . objects ;
30+ if ( objects . Length == 0 )
31+ return ;
32+ container . AddWithContextMenu ( objects ) ;
33+ }
34+
35+ private void Initialize ( )
36+ {
37+ _container = GetOrCreateContainer ( ) ;
38+ _serializedObject = new SerializedObject ( _container ) ;
39+ _paths = _serializedObject . FindProperty ( "_paths" ) ;
40+ _assets = _serializedObject . FindProperty ( "_assets" ) ;
41+ }
42+
43+ private void OnEnable ( )
44+ {
45+ base . minSize = new Vector2 ( 400f , 400f ) ;
46+ Initialize ( ) ;
47+ }
48+
49+ public void OnGUI ( )
50+ {
51+ if ( _container == null )
52+ {
53+ if ( GUILayout . Button ( "Create Assets Container" ) )
54+ {
55+ Initialize ( ) ;
56+ }
57+ return ;
58+ }
59+ _serializedObject . Update ( ) ;
60+ DrawPaths ( ) ;
61+ DrawAssetsList ( ) ;
62+ _serializedObject . ApplyModifiedProperties ( ) ;
63+ }
64+
65+ private void DrawPaths ( )
66+ {
67+ EditorGUILayout . BeginVertical ( "box" ) ;
68+ EditorGUILayout . BeginHorizontal ( ) ;
69+ EditorGUILayout . LabelField ( $ "Paths ({ _paths . arraySize } )", EditorStyles . boldLabel , GUILayout . MinWidth ( 120f ) ) ;
70+ if ( GUILayout . Button ( "Add" , GUILayout . MinWidth ( 70f ) ) )
71+ _paths . InsertArrayElementAtIndex ( 0 ) ;
72+ EditorGUILayout . EndHorizontal ( ) ;
73+ _pathsScroll = EditorGUILayout . BeginScrollView ( _pathsScroll , GUILayout . MaxHeight ( 160f ) , GUILayout . MinHeight ( 0f ) , GUILayout . Height ( 160f ) ) ;
74+ for ( int i = 0 ; i < _paths . arraySize ; i ++ )
75+ {
76+ DrawPathElement ( i ) ;
77+ }
78+ EditorGUILayout . EndScrollView ( ) ;
79+ EditorGUILayout . EndVertical ( ) ;
80+ }
81+
82+ private void DrawPathElement ( int index )
83+ {
84+ var element = _paths . GetArrayElementAtIndex ( index ) ;
85+ EditorGUILayout . BeginHorizontal ( ) ;
86+ EditorGUILayout . LabelField ( $ "Path { index } ", GUILayout . Width ( 60f ) ) ;
87+ EditorGUILayout . PropertyField ( element , new GUIContent ( string . Empty ) ) ;
88+ if ( GUILayout . Button ( "Select" , GUILayout . Width ( 50f ) ) )
89+ {
90+ string path = EditorUtility . OpenFolderPanel ( "Select path" , "Assets" , "" ) ;
91+
92+ if ( path != "Assets" || path . Length != 0 )
93+ path = path . Substring ( path . IndexOf ( "Assets" ) ) ;
94+
95+ if ( AssetDatabase . IsValidFolder ( path ) )
96+ element . stringValue = path ;
97+ else
98+ _paths . DeleteArrayElementAtIndex ( index ) ;
99+ }
100+ DrawDeleteButton ( _paths , index ) ;
101+ EditorGUILayout . EndHorizontal ( ) ;
102+ }
103+
104+ private void DrawAssetsList ( )
105+ {
106+ EditorGUILayout . BeginVertical ( "box" ) ;
107+ EditorGUILayout . BeginHorizontal ( ) ;
108+ EditorGUILayout . LabelField ( $ "Saved Assets ({ _assets . arraySize } )", EditorStyles . boldLabel , GUILayout . MinWidth ( 120f ) ) ;
109+ if ( GUILayout . Button ( "Load From Paths" ) )
110+ {
111+ _container . LoadAssets ( ) ;
112+ }
113+ if ( GUILayout . Button ( "Clear All" ) )
114+ {
115+ _container . ClearAll ( ) ;
116+ }
117+ EditorGUILayout . EndHorizontal ( ) ;
118+ _assetsScroll = EditorGUILayout . BeginScrollView ( _assetsScroll , /*GUILayout.MaxHeight(200f), */ GUILayout . MinHeight ( 0f ) ) ;
119+ for ( int i = 0 ; i < _assets . arraySize ; i ++ )
120+ {
121+ DrawAssetElement ( i ) ;
122+ }
123+ EditorGUILayout . EndScrollView ( ) ;
124+ EditorGUILayout . EndVertical ( ) ;
125+ }
126+
127+ private void DrawAssetElement ( int index )
128+ {
129+ var element = _assets . GetArrayElementAtIndex ( index ) ;
130+ EditorGUILayout . BeginHorizontal ( ) ;
131+ EditorGUILayout . LabelField ( new GUIContent ( $ "Asset { index } ") , GUILayout . Width ( 70f ) ) ;
132+ GUI . enabled = false ;
133+ EditorGUILayout . PropertyField ( element . FindPropertyRelative ( "_asset" ) , new GUIContent ( string . Empty ) ) ;
134+ GUI . enabled = true ;
135+ if ( GUILayout . Button ( "Copy Ref" , GUILayout . Width ( 70f ) ) )
136+ {
137+ string reference = element . FindPropertyRelative ( "_guid" ) . stringValue ;
138+ EditorGUIUtility . systemCopyBuffer = reference ;
139+ Debug . Log ( $ "Copied Reference: { reference } ") ;
140+ }
141+ DrawDeleteButton ( _assets , index ) ;
142+ EditorGUILayout . EndHorizontal ( ) ;
143+ }
144+
145+ private void DrawDeleteButton ( SerializedProperty array , int index )
146+ {
147+ if ( GUILayout . Button ( "Remove" , GUILayout . Width ( 60f ) ) )
148+ {
149+ array . DeleteArrayElementAtIndex ( index ) ;
150+ }
151+ }
152+
153+ private static AssetsContainer GetOrCreateContainer ( )
154+ {
155+ var container = Resources . Load < AssetsContainer > ( AssetsContainer . RESOURCES_PATH ) ;
156+
157+ if ( container )
158+ {
159+ return container ;
160+ }
161+
162+ container = ScriptableObject . CreateInstance < AssetsContainer > ( ) ;
163+ container . Initialize ( ) ;
164+
165+ string directory = Path . GetDirectoryName ( AssetsContainer . PATH ) ;
166+ if ( ! Directory . Exists ( directory ) )
167+ {
168+ Directory . CreateDirectory ( directory ) ;
169+ }
170+
171+ AssetDatabase . CreateAsset ( container , AssetsContainer . PATH ) ;
172+ AssetDatabase . SaveAssets ( ) ;
173+
174+ return container ;
175+ }
176+ }
177+
178+ [ CustomEditor ( typeof ( AssetsContainer ) ) ]
179+ public class AssetsContainerEditorTemp : Editor
180+ {
181+ public override void OnInspectorGUI ( )
182+ {
183+ if ( GUILayout . Button ( "Open Editor" ) )
184+ {
185+ AssetsContainerEditor . ShowWindow ( ) ;
186+ }
187+ }
188+ }
189+ }
0 commit comments