using UnityEngine; using UnityEditor; using System.IO; using System.Xml; using System.Collections; using System.Collections.Generic; using ParticlePlayground; using ParticlePlaygroundLanguage; class PlaygroundParticleWindowC : EditorWindow { ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // PlaygroundParticleWindow variables ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Update check bool hasInternet; WWW versionRequest; string onlineVersion; // Extensions download public static WWW extensionsRequest; public static string extensionsText; public static bool extensionsAvailable; public static bool canDisplayExtensions; public static List extensionObjects; public static int iconDownloadIndex; // Presets public static List presetObjects; public static List presetCategories; public static List presetNames; // Settings public static PlaygroundSettingsC playgroundSettings; public static PlaygroundLanguageC playgroundLanguage; // Editor Window specific public static Vector2 scrollPosition; public static GUIStyle presetButtonStyle; public static string searchString = ""; public static string searchStringExtensions = ""; public static GUIStyle toolbarSearchSkin; public static GUIStyle toolbarSearchButtonSkin; public static GUIStyle boxStyle; public static bool didSendVersionCheck = false; public static bool updateAvailable = false; public static bool assetsFound = true; public static bool didSendExtensionsCheck = false; [MenuItem ("Window/Particle Playground")] public static void ShowWindow () { PlaygroundParticleWindowC window = GetWindow(); #if UNITY_4_3 || UNITY_4_5 || UNITY_4_6 || UNITY_5_0 window.title = "Playground"; #else window.titleContent.text = "Playground"; #endif window.Show(); } public void OnEnable () { Initialize(); } public void OnProjectChange () { Initialize(); } public void Initialize () { presetButtonStyle = new GUIStyle(); presetButtonStyle.stretchWidth = true; presetButtonStyle.stretchHeight = true; // Load settings playgroundSettings = PlaygroundSettingsC.SetReference(); PlaygroundInspectorC.playgroundSettings = playgroundSettings; PlaygroundParticleSystemInspectorC.playgroundSettings = playgroundSettings; // Load language playgroundLanguage = PlaygroundSettingsC.GetLanguage(); PlaygroundInspectorC.playgroundLanguage = playgroundLanguage; PlaygroundParticleSystemInspectorC.playgroundLanguage = playgroundLanguage; // Get all asset presets string assetsDataPath = Application.dataPath; if (Directory.Exists (assetsDataPath+"/"+playgroundSettings.playgroundPath+playgroundSettings.examplePresetPath)) { assetsFound = true; } else { assetsFound = false; playgroundSettings.settingsFoldout = true; playgroundSettings.settingsPathFoldout = true; return; } // Set default particle image Texture2D particleImageDefault = AssetDatabase.LoadAssetAtPath("Assets/"+playgroundSettings.playgroundPath+playgroundSettings.iconPath+"Default.png", typeof(Texture2D)) as Texture2D; // Prepare any previously entered search words to filter presets string[] searchSplits = searchString.Split(new string[]{" "}, System.StringSplitOptions.None); // Get the list of categories and presets string[] editorPresetCategories = Directory.GetDirectories(assetsDataPath+"/"+playgroundSettings.playgroundPath+playgroundSettings.examplePresetPath); presetCategories = new List(); // A linear list of preset objects is kept for easier access presetObjects = new List(); // A linear list of preset names is kept for easier access presetNames = new List(); // List all categories in asset folders for (int i = 0; i0 && loosePresetsHasGo) { presetCategories.Add(new PresetCategory("Uncategorized")); int categoryCount = presetCategories.Count-1; for (int x = 0; x 0) { presetCategories.Add(new PresetCategory("Resources")); int categoryCount = presetCategories.Count-1; for (int i = 0; iPlaygroundC.version); } didSendVersionCheck = true; } } } string extensionsXmlLocation = "PlaygroundCache/extensions.xml"; void CheckExtensions () { if (!playgroundSettings.enableExtensions) return; // Download extensions list if (!didSendExtensionsCheck) { if (extensionsRequest==null) extensionsRequest = new WWW(playgroundSettings.extensionsUrl); if (extensionsRequest.isDone) { if (extensionsRequest.error==null) { extensionsText = extensionsRequest.text; LoadExtensionItems(true); } else { if (File.Exists(extensionsXmlLocation)) { extensionsText = File.ReadAllText(extensionsXmlLocation); if (extensionsText!=null || extensionsText!="") LoadExtensionItems(false); } } didSendExtensionsCheck = true; } } } void LoadExtensionItems (bool saveXml) { XmlDocument xml = new XmlDocument(); xml.LoadXml(extensionsText); if (saveXml) { Directory.CreateDirectory ("PlaygroundCache/"); xml.Save (extensionsXmlLocation); } IEnumerator xmlRoot = xml.DocumentElement.GetEnumerator(); extensionObjects = new List(); while (xmlRoot.MoveNext()) { ExtensionObjectC extObj = new ExtensionObjectC(); extensionObjects.Add (extObj); XmlElement xmlElement = (XmlElement)xmlRoot.Current; extObj.title = xmlElement.SelectSingleNode("title").InnerText; extObj.id = xmlElement.SelectSingleNode("id").InnerText; extObj.iconUrl = xmlElement.SelectSingleNode("icon").InnerText; extObj.iconId = xmlElement.SelectSingleNode("iconid").InnerText; extObj.publisher = xmlElement.SelectSingleNode("publisher").InnerText; extObj.publisherUrl = xmlElement.SelectSingleNode("publisherurl").InnerText; extObj.category = xmlElement.SelectSingleNode("category").InnerText; if (File.Exists ("PlaygroundCache/"+extObj.iconId+".png")) { byte[] fileData = File.ReadAllBytes("PlaygroundCache/"+extObj.iconId+".png"); extObj.icon = new Texture2D(2, 2); extObj.icon.LoadImage(fileData); } else { extObj.PrepareDownload(); } extObj.searchMeta = extObj.title+" \n"+extObj.id+" \n"+extObj.category+" \n"+extObj.publisher; extObj.unfiltered = (searchStringExtensions==""?true:extObj.searchMeta.ToLower().Contains(searchStringExtensions.ToLower())); } extensionsAvailable = true; GetWindow().Repaint(); } void OnSelectionChange() { // Detect selection changes to make the Playground Component buttons responsive Transform currentSelection = Selection.activeTransform; if (currentSelection != null) { GetWindow().Repaint(); Selection.activeTransform = currentSelection; EditorApplication.ExecuteMenuItem("Window/Hierarchy"); } } void OnGUI () { if (boxStyle==null) boxStyle = GUI.skin.FindStyle("box"); if (toolbarSearchSkin==null) { toolbarSearchSkin = GUI.skin.FindStyle("ToolbarSeachTextField"); if (toolbarSearchButtonSkin==null) toolbarSearchButtonSkin = GUI.skin.FindStyle("ToolbarSeachCancelButton"); } EditorGUILayout.BeginVertical(); scrollPosition = GUILayout.BeginScrollView(scrollPosition, false, false); EditorGUILayout.BeginVertical(boxStyle); EditorGUILayout.Separator(); EditorGUILayout.LabelField(playgroundLanguage.playgroundName+" "+PlaygroundC.version+PlaygroundC.specialVersion, EditorStyles.largeLabel, GUILayout.Height(20)); EditorGUILayout.Separator(); // Playground Settings is an instance (give option to search and assign the stored reference) if (playgroundSettings.IsInstance()) { GUI.backgroundColor = Color.red; EditorGUILayout.BeginVertical(boxStyle); EditorGUILayout.HelpBox (playgroundLanguage.findPlaygroundSettingsMsg, MessageType.Warning); if (GUILayout.Button(playgroundLanguage.findPlaygroundSettings, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))){ PlaygroundSettingsC.SetPlaygroundSettingsLocation(); } GUI.backgroundColor = Color.white; EditorGUILayout.EndVertical(); EditorGUILayout.Separator(); } // New version message if (hasInternet) { if (playgroundSettings.checkForUpdates && !didSendVersionCheck) CheckUpdate(); if (playgroundSettings.checkForUpdates && updateAvailable) { GUI.backgroundColor = Color.yellow; EditorGUILayout.BeginVertical(boxStyle); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField(playgroundLanguage.updateAvailable); GUILayout.FlexibleSpace(); if (GUILayout.Button("x", EditorStyles.miniButton, new GUILayoutOption[]{GUILayout.Width(18), GUILayout.Height(18)})){ updateAvailable = false; } EditorGUILayout.EndHorizontal(); EditorGUILayout.LabelField(onlineVersion+" "+playgroundLanguage.updateAvailableText, EditorStyles.wordWrappedMiniLabel); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(playgroundLanguage.unityAssetStore, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))){ Application.OpenURL("com.unity3d.kharma:content/13325"); } GUI.backgroundColor = Color.white; EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); EditorGUILayout.Separator(); } } EditorGUILayout.BeginVertical(boxStyle); // Create New-buttons EditorGUILayout.BeginHorizontal(); if(GUILayout.Button(playgroundLanguage.newParticlePlaygroundSystem, EditorStyles.toolbarButton)){ if (PlaygroundC.reference==null) CreateManager(); PlaygroundParticlesC newParticlePlayground = PlaygroundC.Particle(); newParticlePlayground.EditorYieldSelect(); } GUI.enabled = PlaygroundC.reference==null; if(GUILayout.Button(playgroundLanguage.playgroundManager, EditorStyles.toolbarButton)){ PlaygroundC.ResourceInstantiate("Playground Manager"); } GUI.enabled = true; if(GUILayout.Button(playgroundLanguage.presetWizard, EditorStyles.toolbarButton)){ PlaygroundCreatePresetWindowC.ShowWindow(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); // Components menu EditorGUILayout.BeginVertical(boxStyle); EditorGUILayout.LabelField(playgroundLanguage.playgroundComponents); EditorGUILayout.BeginHorizontal(); GameObject currentSelection = Selection.activeGameObject; bool hasSelection = currentSelection != null; bool hasSpline = hasSelection? currentSelection.GetComponent() != null : false; bool hasFollow = hasSelection? currentSelection.GetComponent() != null : false; bool hasTrail = hasSelection? currentSelection.GetComponent() != null : false; bool hasRecorder = hasSelection? currentSelection.GetComponent() != null : false; GUI.enabled = hasSelection; // Playground Spline GUI.color = hasSpline?Color.green : Color.white; if(GUILayout.Button(hasSpline? playgroundLanguage.playgroundSpline+" (Attached)" : playgroundLanguage.playgroundSpline, EditorStyles.toolbarButton)){ if (!hasSpline) currentSelection.AddComponent(); else if (EditorUtility.DisplayDialog(playgroundLanguage.removeComponent, playgroundLanguage.removeComponentMsg, playgroundLanguage.yes, playgroundLanguage.no)) DestroyImmediate(currentSelection.GetComponent()); } // Playground Trails GUI.color = hasTrail?Color.green : Color.white; if(GUILayout.Button(hasTrail? playgroundLanguage.playgroundTrails+" (Attached)" : playgroundLanguage.playgroundTrails, EditorStyles.toolbarButton)){ if (!hasTrail) currentSelection.AddComponent(); else if (EditorUtility.DisplayDialog(playgroundLanguage.removeComponent, playgroundLanguage.removeComponentMsg, playgroundLanguage.yes, playgroundLanguage.no)) DestroyImmediate(currentSelection.GetComponent()); } // Playground Follow GUI.color = hasFollow?Color.green : Color.white; if(GUILayout.Button(hasFollow? playgroundLanguage.playgroundFollow+" (Attached)" : playgroundLanguage.playgroundFollow, EditorStyles.toolbarButton)){ if (!hasFollow) currentSelection.AddComponent(); else if (EditorUtility.DisplayDialog(playgroundLanguage.removeComponent, playgroundLanguage.removeComponentMsg, playgroundLanguage.yes, playgroundLanguage.no)) DestroyImmediate(currentSelection.GetComponent()); } // Playground Recorder GUI.color = hasRecorder?Color.green : Color.white; if(GUILayout.Button(hasRecorder? playgroundLanguage.playgroundRecorder+" (Attached)" : playgroundLanguage.playgroundRecorder, EditorStyles.toolbarButton)){ if (!hasRecorder) currentSelection.AddComponent(); else if (EditorUtility.DisplayDialog(playgroundLanguage.removeComponent, playgroundLanguage.removeComponentMsg, playgroundLanguage.yes, playgroundLanguage.no)) DestroyImmediate(currentSelection.GetComponent()); } GUI.enabled = true; EditorGUILayout.EndHorizontal(); GUI.color = Color.white; EditorGUILayout.EndVertical(); EditorGUILayout.EndVertical(); if (assetsFound) { // Presets EditorGUILayout.BeginVertical(boxStyle); playgroundSettings.presetsFoldout = GUILayout.Toggle(playgroundSettings.presetsFoldout, playgroundLanguage.presets, EditorStyles.foldout); if (playgroundSettings.presetsFoldout) { EditorGUILayout.BeginHorizontal("Toolbar"); // Search string prevSearchString = searchString; searchString = GUILayout.TextField(searchString, toolbarSearchSkin, new GUILayoutOption[]{GUILayout.ExpandWidth(false), GUILayout.Width(Mathf.FloorToInt(Screen.width)-120), GUILayout.MinWidth(100)}); if (GUILayout.Button("", toolbarSearchButtonSkin)) { searchString = ""; for (int i = 0; i0) { for (int c = 0; c0; if (GUILayout.Button(presetCategories[c].categoryName+" ("+presetCategories[c].presetObjects.Count+")", EditorStyles.toolbarDropDown)) presetCategories[c].foldout = !presetCategories[c].foldout; } else presetCategories[c].foldout = true; if (presetCategories[c].foldout && GUI.enabled) { if (!listSize) EditorGUILayout.BeginHorizontal(); int rows = 1; int iconwidths = 0; int skippedPresets = 0; for (int i = 0; iScreen.width && i>0) { iconwidths=342+iconSize; EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); } if (Screen.width>=644) { EditorGUILayout.BeginVertical(boxStyle, GUILayout.MaxWidth (Mathf.CeilToInt(Screen.width/(rows))-(44/(rows)))); } else EditorGUILayout.BeginVertical(boxStyle); EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(46)); if(GUILayout.Button(presetCategories[c].presetObjects[i].presetImage, EditorStyles.miniButton, new GUILayoutOption[]{GUILayout.Width(iconSize+12), GUILayout.Height(iconSize+12)})){ CreatePresetObject(c, i); } EditorGUILayout.BeginVertical(); if (GUILayout.Button(presetCategories[c].presetObjects[i].presetObject.name, EditorStyles.label, GUILayout.Height(18))) CreatePresetObject(c, i); EditorGUILayout.LabelField(presetCategories[c].categoryName, EditorStyles.miniLabel); EditorGUILayout.EndVertical(); GUILayout.FlexibleSpace(); EditorGUILayout.BeginVertical(); if(GUILayout.Button("x", EditorStyles.miniButton, new GUILayoutOption[]{GUILayout.Width(18), GUILayout.Height(18)})){ RemovePreset(presetCategories[c].presetObjects[i].presetObject); return; } EditorGUILayout.EndVertical(); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } } GUI.enabled = true; if (skippedPresets==presetObjects.Count) { if (searchString!="") { EditorGUILayout.HelpBox(playgroundLanguage.searchNoPresetFound+" \""+searchString+"\".", MessageType.Info); } else { EditorGUILayout.HelpBox(playgroundLanguage.noPresetsFound, MessageType.Info); } } if (!listSize) EditorGUILayout.EndHorizontal(); } } } else { EditorGUILayout.HelpBox(playgroundLanguage.noPresetsFoundInProject+" \""+"Assets/"+playgroundSettings.playgroundPath+playgroundSettings.examplePresetPath+"\"", MessageType.Info); } GUI.enabled = true; EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(playgroundLanguage.create, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))) { PlaygroundCreatePresetWindowC.ShowWindow(); } EditorGUILayout.Separator(); if (GUILayout.Button(playgroundLanguage.refresh, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))) { AssetDatabase.Refresh(); Initialize(); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } EditorGUILayout.EndVertical(); } else { EditorGUILayout.HelpBox (playgroundLanguage.noAssetsFoundMessage, MessageType.Warning); } // Extension check if (hasInternet) { if (playgroundSettings.enableExtensions && !didSendExtensionsCheck) CheckExtensions(); // Extension icon download if (playgroundSettings.enableExtensions && extensionObjects!=null && iconDownloadIndexScreen.width && i>0) { iconwidths=322; EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); } if (Screen.width>=644) { EditorGUILayout.BeginVertical(boxStyle, GUILayout.MaxWidth (Mathf.CeilToInt(Screen.width/rows)-(45/rows))); } else EditorGUILayout.BeginVertical(boxStyle); EditorGUILayout.BeginHorizontal(GUILayout.MinHeight(iconSize+16)); if(GUILayout.Button(extensionObjects[i].icon, EditorStyles.miniButton, new GUILayoutOption[]{GUILayout.Width(iconSize+12), GUILayout.Height(iconSize+12)})){ OpenUAS(extensionObjects[i].id); } EditorGUILayout.BeginVertical(); EditorGUILayout.BeginVertical(boxStyle, GUILayout.MinHeight(iconSize+10)); if (GUILayout.Button(extensionObjects[i].title, EditorStyles.boldLabel, GUILayout.ExpandWidth(true))) OpenUAS(extensionObjects[i].id); if (smallSize) { EditorGUILayout.BeginHorizontal(); } if (GUILayout.Button(extensionObjects[i].category, EditorStyles.label, GUILayout.ExpandWidth(false))) { searchStringExtensions = extensionObjects[i].category; for (int p = 0; p0 && searchStringExtensions!="") EditorGUILayout.HelpBox(playgroundLanguage.listHasBeenFilteredWith+": \""+searchStringExtensions+"\".", MessageType.Info); } else EditorGUILayout.HelpBox (playgroundLanguage.extensionsDownloadError, MessageType.Warning); } else EditorGUILayout.HelpBox (playgroundLanguage.extensionsDownloading, MessageType.Info); } EditorGUILayout.EndVertical(); } PlaygroundInspectorC.RenderPlaygroundSettings(); // Playground Settings EditorGUILayout.BeginVertical(boxStyle); playgroundSettings.settingsFoldout = GUILayout.Toggle(playgroundSettings.settingsFoldout, playgroundLanguage.settings, EditorStyles.foldout); if (playgroundSettings.settingsFoldout) { EditorGUILayout.BeginVertical(boxStyle); if (playgroundSettings==null || playgroundSettings.IsInstance()) { EditorGUILayout.HelpBox(playgroundLanguage.noSettingsFile+" \""+PlaygroundSettingsC.settingsPath+"\".", MessageType.Warning); if(GUILayout.Button(playgroundLanguage.create, EditorStyles.toolbarButton, GUILayout.ExpandWidth(false))){ PlaygroundSettingsC.New(); Initialize(); return; } } else { playgroundSettings.checkForUpdates = EditorGUILayout.Toggle (playgroundLanguage.checkForUpdates, playgroundSettings.checkForUpdates); playgroundSettings.enableExtensions = EditorGUILayout.Toggle (playgroundLanguage.extendYourPlayground, playgroundSettings.enableExtensions); playgroundSettings.presetsHasPrefabConnection = EditorGUILayout.Toggle (playgroundLanguage.prefabConnection, playgroundSettings.presetsHasPrefabConnection); EditorGUI.BeginChangeCheck(); playgroundSettings.hierarchyIcon = EditorGUILayout.Toggle (playgroundLanguage.hierarchyIcon, playgroundSettings.hierarchyIcon); if (EditorGUI.EndChangeCheck()) { PlaygroundHierarchyIcon.Set(); } EditorGUILayout.Separator(); EditorGUILayout.BeginVertical(boxStyle); playgroundSettings.settingsLanguageFoldout = GUILayout.Toggle(playgroundSettings.settingsLanguageFoldout, playgroundLanguage.language, EditorStyles.foldout); if (playgroundSettings.settingsLanguageFoldout) { if (playgroundSettings.languages.Count>0) { bool setThisLoadFrom = false; int currentLanguageCount = playgroundSettings.languages.Count; for (int i = 0; i(); if (presetParticles!=null) { if (PlaygroundC.reference==null) PlaygroundC.ResourceInstantiate("Playground Manager"); if (PlaygroundC.reference) { if (PlaygroundC.reference.autoGroup && presetParticles.particleSystemTransform.parent==null) presetParticles.particleSystemTransform.parent = PlaygroundC.referenceTransform; PlaygroundC.particlesQuantity++; presetParticles.particleSystemId = PlaygroundC.particlesQuantity; } presetGo.name = presetObject.name; return presetParticles; } else return null; } public void CreateManager () { GameObject pmGo = PlaygroundC.ResourceInstantiate("Playground Manager"); PlaygroundC pm = pmGo.GetComponent(); PlaygroundC.reference = pm; } } public class PresetCategory { public string categoryName = ""; public bool foldout = false; public List presetObjects = new List(); public PresetCategory (string categoryName) { this.categoryName = categoryName; } } public class PresetObjectC { public Object presetObject; public Texture2D presetImage; public string presetPath; public bool unfiltered = true; public bool example = false; public PresetObjectC () {} public PresetObjectC (Object presetObject) { this.presetObject = presetObject; } } [System.Serializable] public class ExtensionObjectC { public string title; public string id; public string publisher; public string publisherUrl; public string category; public string iconUrl; public string iconId; public Texture2D icon; public WWW iconDownload; public bool unfiltered = true; public string searchMeta = ""; bool shouldDownloadIcon; bool downloadStarted; bool downloadFinished; public void PrepareDownload () { shouldDownloadIcon = true; } public bool IsExpectingDownload () { return shouldDownloadIcon; } public bool IconDownloadRoutine () { if (!shouldDownloadIcon) { return true; } if (!downloadStarted) StartIconDownload(); if (iconDownload.error!=null) { shouldDownloadIcon = false; downloadFinished = true; iconDownload.Dispose(); return true; } if (!downloadFinished && iconDownload.isDone) { icon = new Texture2D(75,75,TextureFormat.ARGB32,false); icon = iconDownload.texture; SaveIconToCache(); shouldDownloadIcon = false; downloadFinished = true; iconDownload.Dispose(); } return downloadFinished; } public void StartIconDownload () { downloadStarted = true; iconDownload = new WWW(iconUrl); } public void SaveIconToCache () { byte[] bytes = icon.EncodeToPNG(); FileStream file = File.Create("PlaygroundCache/"+iconId+".png"); BinaryWriter binary = new BinaryWriter(file); binary.Write(bytes); file.Close(); } public bool CheckAvailability () { if (icon==null && iconId!=null) { if (File.Exists ("PlaygroundCache/"+iconId+".png")) { byte[] fileData = File.ReadAllBytes("PlaygroundCache/"+iconId+".png"); icon = new Texture2D(2, 2); icon.LoadImage(fileData); return true; } return false; } return false; } }