1using NaughtyAttributes;
3using System.Collections;
4using System.Collections.Generic;
19 if (_instance ==
null)
24 if (scene !=
null && scene.Length > 0)
28 for (
int i = 1; i < scene.Length; i++)
30 pb_ObjectUtility.Destroy(scene[i]);
35 GameObject go =
new GameObject();
36 go.name =
"PrefabBrowser Singleton";
45 [Header(
"Local")]
public GameObject
Models;
48 [Header(
"Global")]
public GameObject
GModels;
57 private bool showGlobals =
true;
58 private bool showLocals =
true;
59 private string lastSearchValue =
"";
60 internal readonly Dictionary<GameObject, LoadPanel> prefabs =
new Dictionary<GameObject, LoadPanel>();
61 readonly Dictionary<GameObject, LoadPanel> globalPrefabs =
new Dictionary<GameObject, LoadPanel>();
62 readonly Dictionary<GameObject, LoadPanel> uploadedModels =
new Dictionary<GameObject, LoadPanel>();
63 readonly Dictionary<GameObject, LoadPanel> globalUploadedModels =
new Dictionary<GameObject, LoadPanel>();
64 readonly List<string> filePaths =
new List<string>();
65 readonly List<string> globalFilePaths =
new List<string>();
66 readonly List<AssetBundle> assetBundles =
new List<AssetBundle>();
75 showGlobals = !showGlobals;
81 showLocals = !showLocals;
88 if (
Models ==
null)
return;
91 foreach (var o
in resourcePrefabs) prefabs.Add(o,
LoadPanel.None);
106 foreach (var keyPair
in prefabs)
108 GameObject icon =
null;
109 switch (keyPair.Value)
124 icon =
Helps.AddChild();
135 var button = icon.AddComponent<pb_PrefabBrowserItemButton>();
136 button.asset = keyPair.Key;
138 button.onClick.AddListener(pb_Hierarchy.instance.RebuildInspector);
145 nameLabel.GetComponentInChildren<Text>().text =
147 Path.GetFileNameWithoutExtension
149 (keyPair.Key.name.ToUpper());
151 textFormatting(button, nameLabel.GetComponentInChildren<Text>(), keyPair.Key.name);
155 icon.AddComponent<Mask>();
159 var ssol = FindObjectOfType<StartStageOnLoad>();
160 if (ssol && ssol.filesToLoad !=
null)
162 var withPath = GetFilesFullPath(ssol.filesToLoad.ToArray());
163 var envFiles = GetFilesFullPath(GetEnvFiles(ssol.levelName).ToArray());
164 var luaFiles =
Directory.GetFiles(Application.streamingAssetsPath,
"*.lua").ToList()
167 var allFile = envFiles;
168 allFile.AddRange(withPath);
169 allFile.AddRange(luaFiles);
170 StartCoroutine(FindObjectOfType<FileDragAndDrop>().ThreadedLoad(allFile.Distinct().ToList()));
174 var list =
Directory.GetFiles(Application.streamingAssetsPath).ToList()
175 .Concat(
Directory.GetDirectories(Application.streamingAssetsPath).ToList());
176 StartCoroutine(FindObjectOfType<FileDragAndDrop>().ThreadedLoad(list.ToList()));
179 StartCoroutine(WebGLLateLoadingFix());
185 var withPath = GetFilesFullPath(ssol.
filesToLoad.ToArray());
186 var envFiles = GetFilesFullPath(GetEnvFiles(ssol.
levelName).ToArray());
187 var luaFiles =
Directory.GetFiles(Application.streamingAssetsPath,
"*.lua").ToList()
190 var allFile = envFiles;
191 allFile.AddRange(withPath);
192 allFile.AddRange(luaFiles);
193 StartCoroutine(FindObjectOfType<FileDragAndDrop>().ThreadedLoad(allFile.Distinct().ToList()));
195 private List<string> GetEnvFiles(
string currentLevelName)
197 var retval =
new List<string>();
198 var envs =
new DirectoryInfo(Application.streamingAssetsPath)
199 .EnumerateFiles(
"*.env", SearchOption.AllDirectories).ToList();
200 var currentLoadedMap = envs.FirstOrDefault(x => x.Name == currentLevelName +
".env");
201 var env_path = Path.Combine(Application.streamingAssetsPath,
"environment.env");
203 if (
File.Exists(env_path) || currentLoadedMap !=
default(FileInfo))
205 var env = GameObject.Find(
"Environment");
211 if (currentLoadedMap !=
default(FileInfo))
213 json =
File.ReadAllText(currentLoadedMap.FullName);
217 json = File.ReadAllText(env_path);
222 var sceneNode = JsonConvert.DeserializeObject<Serialization.pb_SceneNode>(scenenodeJson, Serialization.pb_Serialization.ConverterSettings);
228 private List<string> GetFilesFullPath(
string[] files,
229 SearchOption searchOption = SearchOption.AllDirectories)
231 var allfiles =
Directory.EnumerateFiles(Application.streamingAssetsPath,
"*", searchOption).ToList();
232 var retval =
new List<string>();
233 foreach (var filepath
in allfiles)
235 foreach (var file
in files)
237 if (filepath.EndsWith(file))
239 retval.Add(filepath);
247 private void OnDestroy()
249 foreach (var assetBundle
in assetBundles) assetBundle.Unload(
true);
252 private bool flag =
true;
255 private IEnumerator WebGLLateLoadingFix()
257 yield
return new WaitForSeconds(1.0f);
262 private void Update()
272 if (uploadedModels.ContainsKey(asset))
274 uploadedModels.Remove(asset);
275 Debug.Log(
"Remove successful!");
279 Debug.LogError(
"Asset does not exists in the uploadedModels dictionary!");
285 if (
Models ==
null)
return;
286 StartCoroutine(ReloadWork());
289 private IEnumerator ReloadWork()
291 yield
return new WaitForEndOfFrame();
302 globalPrefabs.Clear();
309 foreach (var o
in resourcePrefabs) globalPrefabs.Add(o,
LoadPanel.None);
311 foreach (var assetBundle
in assetBundles)
313 var temp = assetBundle.LoadAllAssets<GameObject>();
314 foreach (var o
in temp) prefabs.Add(o,
LoadPanel.None);
318 foreach (var o
in globalUploadedModels)
319 globalPrefabs.Add(o.Key, o.Value);
321 foreach (var o
in uploadedModels)
322 prefabs.Add(o.Key, o.Value);
326 foreach (var keyPair
in global ? globalPrefabs : prefabs)
328 if (keyPair.Key ==
null)
continue;
330 GameObject icon =
null;
332 switch (keyPair.Value)
347 icon = global ?
Helps.AddChild() :
Helps.AddChild();
359 button.
asset = keyPair.Key;
362 button.fullFilePath = globalFilePaths[i];
364 button.fullFilePath = filePaths[i];
375 nameLabel.GetComponentInChildren<Text>().text =
377 Path.GetFileNameWithoutExtension
379 (keyPair.Key.name.ToUpper());
381 textFormatting(button, nameLabel.GetComponentInChildren<Text>(), keyPair.Key.name);
385 icon.AddComponent<Mask>();
391 GameObject go =
new GameObject();
393 var rtText = text.GetComponent<RectTransform>();
395 topLeft(text.gameObject);
396 var newGO = Instantiate(go, text.transform);
398 newGO.AddComponent<RectTransform>();
399 newGO.AddComponent<CanvasRenderer>();
400 var newText = newGO.AddComponent<Text>();
401 newText.text = Path.GetExtension(name.ToUpper());
402 newText.fontSize = 12;
403 topLeft(newText.gameObject);
404 newText.transform.localPosition =
new Vector3(0, -rtText.sizeDelta.y, 0);
405 copyPasteFormat(newText, text);
407 var posY = text.transform.localPosition.y-4;
408 var posZ = text.transform.localPosition.z;
409 text.transform.localPosition =
new Vector3(posX, posY, posZ);
411 private void copyPasteFormat(Text text1, Text text2)
413 text1.color = text2.color;
414 text1.font = text2.font;
415 text1.fontStyle = text2.fontStyle;
417 private void topLeft(GameObject uiObject)
419 RectTransform uitransform = uiObject.GetComponent<RectTransform>();
421 uitransform.anchorMin =
new Vector2(0, 1);
422 uitransform.anchorMax =
new Vector2(0, 1);
423 uitransform.pivot =
new Vector2(0, 1);
429 globalPrefabs.Clear();
435 foreach (var o
in resourcePrefabs) globalPrefabs.Add(o,
LoadPanel.None);
437 foreach (var assetBundle
in assetBundles)
439 var temp = assetBundle.LoadAllAssets<GameObject>();
440 foreach (var o
in temp) prefabs.Add(o,
LoadPanel.None);
443 foreach (var o
in globalUploadedModels) globalPrefabs.Add(o.Key, o.Value);
444 foreach (var o
in uploadedModels) prefabs.Add(o.Key, o.Value);
449 foreach (var keyPair
in globalPrefabs)
451 if (keyPair.Key ==
null)
continue;
453 GameObject icon =
null;
455 switch (keyPair.Value)
470 icon =
Helps.AddChild();
482 button.
asset = keyPair.Key;
484 button.fullFilePath = globalFilePaths[i];
495 nameLabel.GetComponentInChildren<Text>().text =
497 Path.GetFileNameWithoutExtension
499 (keyPair.Key.name.ToUpper());
501 textFormatting(button, nameLabel.GetComponentInChildren<Text>(), keyPair.Key.name);
505 icon.AddComponent<Mask>();
513 foreach (var keyPair
in prefabs)
515 if (keyPair.Key ==
null)
517 Debug.Log($
"keyPair key is null!");
521 GameObject icon =
null;
523 switch (keyPair.Value)
538 icon =
Helps.AddChild();
550 button.
asset = keyPair.Key;
552 button.fullFilePath = filePaths[i];
564 nameLabel.GetComponentInChildren<Text>().text =
566 Path.GetFileNameWithoutExtension
568 (keyPair.Key.name.ToUpper());
570 textFormatting(button, nameLabel.GetComponentInChildren<Text>(), keyPair.Key.name);
574 icon.AddComponent<Mask>();
583 assetBundles.Add(ab);
584 var temp = ab.LoadAllAssets<GameObject>();
585 foreach (var o
in temp) prefabs.Add(o,
LoadPanel.None);
587 foreach (var s
in ab.GetAllAssetNames())
Debug.Log(s);
589 foreach (Transform child
in transform) Destroy(child.gameObject);
591 foreach (var keyPair
in prefabs)
593 GameObject icon =
null;
594 switch (keyPair.Value)
609 icon =
Helps.AddChild();
621 button.
asset = keyPair.Key;
626 icon.AddComponent<Mask>();
633 foreach (var key
in prefabs.Keys)
635 if (key.name == gobject.name)
639 var allspawned = FindObjectsOfType<UploadedFile>(
true);
640 foreach (var obj
in allspawned)
642 if (obj.fileName != gobject.name)
continue;
644 if (obj.GetComponent<TMPro.TextMeshPro>())
654 obj.GetComponent<TMPro.TextMeshPro>().text = gobject.GetComponent<TMPro.TextMeshPro>().text;
674 globalPrefabs.Add(gobject, panel);
676 prefabs.Add(gobject, panel);
680 globalUploadedModels.Add(gobject, panel);
682 uploadedModels.Add(gobject, panel);
685 globalFilePaths.Add(filePath);
687 filePaths.Add(filePath);
692 foreach (var keyPair
in globalPrefabs)
694 if (keyPair.Key ==
null)
continue;
696 GameObject icon =
null;
698 switch (keyPair.Value)
713 icon =
Helps.AddChild();
725 button.
asset = keyPair.Key;
727 button.fullFilePath = globalFilePaths[i];
738 nameLabel.GetComponentInChildren<Text>().text =
740 Path.GetFileNameWithoutExtension
742 (keyPair.Key.name.ToUpper());
744 textFormatting(button, nameLabel.GetComponentInChildren<Text>(), keyPair.Key.name);
748 icon.AddComponent<Mask>();
754 foreach (var keyPair
in prefabs)
756 if (keyPair.Key ==
null)
758 Debug.Log($
"keyPair key is null!");
762 GameObject icon =
null;
764 switch (keyPair.Value)
779 icon =
Helps.AddChild();
791 button.
asset = keyPair.Key;
793 button.fullFilePath = filePaths[i];
799 icon.AddComponent<Mask>();
808 foreach (var prefab
in isGlobalNow ? globalPrefabs : prefabs)
809 if (prefab.Key.name == tcName)
811 var tc = prefab.Key.AddComponent<
TagCloud>();
813 var text = Encoding.UTF8.GetString(data);
814 tc.tags = text.Split(
'\n').ToList();
822 private void ClearPanel(
bool global =
false)
824 foreach (Transform child
in global ?
Models.transform :
Models.transform) Destroy(child.gameObject);
826 foreach (Transform child
in global ?
Pictures.transform :
Pictures.transform)
827 Destroy(child.gameObject);
829 foreach (Transform child
in global ?
Sounds.transform :
Sounds.transform) Destroy(child.gameObject);
831 foreach (Transform child
in global ?
Videos.transform :
Videos.transform) Destroy(child.gameObject);
833 foreach (Transform child
in global ?
Helps.transform :
Helps.transform)
835 if (child.name ==
"AddNewPOI")
838 Destroy(child.gameObject);
841 foreach (Transform child
in global ?
Scripts.transform :
Scripts.transform) Destroy(child.gameObject);
843 foreach (Transform child
in global ?
Unknown.transform :
Unknown.transform) Destroy(child.gameObject);
848 foreach (var prefab
in prefabs)
849 if (prefab.Key.name == goName.Replace(
"(Clone)",
"").Replace(
"/fileuploader/",
""))
852 foreach (var prefab
in globalPrefabs)
853 if (prefab.Key.name == goName.Replace(
"(Clone)",
"").Replace(
"/fileuploader/",
""))
858 Debug.Log(
"TODO: null GetPrefabFromName");
862 private bool IsLocal(Transform t)
864 if (!t.gameObject.GetComponentInChildren<
GlobalFile>())
Debug.Log($
"Local go: {t.name}");
865 return !t.gameObject.GetComponentInChildren<
GlobalFile>();
870 value = value.ToLower();
871 lastSearchValue = value;
873 var forceShow =
string.IsNullOrEmpty(value);
874 foreach (Transform child
in Models.transform)
876 child.gameObject.SetActive(
881 if (!showLocals && IsLocal(child))
882 child.gameObject.SetActive(
false);
884 if (!showGlobals && !IsLocal(child))
885 child.gameObject.SetActive(
false);
888 foreach (Transform child
in Pictures.transform)
890 child.gameObject.SetActive(
895 if (!showLocals && IsLocal(child))
896 child.gameObject.SetActive(
false);
898 if (!showGlobals && !IsLocal(child))
899 child.gameObject.SetActive(
false);
902 foreach (Transform child
in Sounds.transform)
904 child.gameObject.SetActive(
909 if (!showLocals && IsLocal(child))
910 child.gameObject.SetActive(
false);
912 if (!showGlobals && !IsLocal(child))
913 child.gameObject.SetActive(
false);
916 foreach (Transform child
in Videos.transform)
918 child.gameObject.SetActive(
923 if (!showLocals && IsLocal(child))
924 child.gameObject.SetActive(
false);
926 if (!showGlobals && !IsLocal(child))
927 child.gameObject.SetActive(
false);
930 foreach (Transform child
in Helps.transform)
932 if (child.name ==
"AddNewPOI")
935 child.gameObject.SetActive(
940 if (!showLocals && IsLocal(child))
941 child.gameObject.SetActive(
false);
943 if (!showGlobals && !IsLocal(child))
944 child.gameObject.SetActive(
false);
947 foreach (Transform child
in Scripts.transform)
950 var tc = asset.GetComponent<
TagCloud>();
951 child.gameObject.SetActive(
952 asset.name.ToLower().Contains(value) ||
953 forceShow || (tc && tc.SearchHelp(value)));
955 if (!showLocals && IsLocal(child))
956 child.gameObject.SetActive(
false);
958 if (!showGlobals && !IsLocal(child))
959 child.gameObject.SetActive(
false);
962 foreach (Transform child
in Unknown.transform)
964 child.gameObject.SetActive(
969 if (!showLocals && IsLocal(child))
970 child.gameObject.SetActive(
false);
972 if (!showGlobals && !IsLocal(child))
973 child.gameObject.SetActive(
false);
979 foreach (Transform child
in GModels.transform)
980 child.gameObject.SetActive(
984 foreach (Transform child
in GPictures.transform)
985 child.gameObject.SetActive(
989 foreach (Transform child
in GSounds.transform)
990 child.gameObject.SetActive(
994 foreach (Transform child
in GVideos.transform)
995 child.gameObject.SetActive(
999 foreach (Transform child
in GHelps.transform)
1001 if (child.name ==
"AddNewPOI")
1004 child.gameObject.SetActive(
1010 foreach (Transform child
in GScripts.transform)
1011 child.gameObject.SetActive(
1015 foreach (Transform child
in GUnknown.transform)
1016 child.gameObject.SetActive(
1023 StartCoroutine(RefreshPanel());
1026 internal IEnumerator RefreshPanel()
1029 foreach (Transform child
in transform) child.gameObject.SetActive(
false);
1033 foreach (Transform child
in transform) child.gameObject.SetActive(
true);
UnityEngine.UI.Button Button
void SearchFilter(string value)
void LoadJsonsFile(StartStageOnLoad ssol)
IEnumerator AddTagCloud(string tcName, byte[] data)
void Reload(bool global=false)
GameObject PrefabGlobalFileBadge
void AddGameObject(GameObject gobject, LoadPanel panel=LoadPanel.None, string filePath="", bool global=false)
void AddBundle(AssetBundle ab)
GameObject GetPrefabFromName(string goName)
static pb_PrefabBrowser instance
GameObject PrefabBrowserDeleteButton
GameObject PrefabFileNameBadge
void DeleteAsset(GameObject asset)
static void GetAllPossibleFileName(GILES.Serialization.pb_SceneNode node, ref List< string > list)
static int FindClosingBracketIndex(string text, char openedBracket='{', char closedBracket='}')
static PanelActuator instance
List< string > filesToLoad
bool SearchHelp(string value)