Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
burning-ape committed Oct 11, 2022
1 parent f468735 commit abbbff8
Show file tree
Hide file tree
Showing 20 changed files with 1,153 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions Editor/BakePreffab.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "BakePreffab.Editor",
"rootNamespace": "",
"references": [
"GUID:17a712dc2fa6134478bc48023aee3be7",
"GUID:0b89bab5e208b814887d6d7da6189abe",
"GUID:0acc523941302664db1f4e527237feb3",
"GUID:35557276d5ecfd84fbc67608e9afce5b"
],
"includePlatforms": [],
"excludePlatforms": [
"Android",
"CloudRendering",
"EmbeddedLinux",
"GameCoreScarlett",
"GameCoreXboxOne",
"iOS",
"LinuxStandalone64",
"Lumin",
"macOSStandalone",
"PS4",
"PS5",
"Stadia",
"Switch",
"tvOS",
"WSA",
"WebGL",
"WindowsStandalone32",
"WindowsStandalone64",
"XboxOne"
],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/BakePreffab.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

236 changes: 236 additions & 0 deletions Editor/PrefabsBakerEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
using System.Text;
using UnityEditor;
using UnityEngine;
using System.IO;


[CustomEditor(typeof(PrefabsBaker))]
public class PrefabsBakerEditor : Editor
{
private Texture2D _closeButtonTexture;
private PrefabsBaker _prefabsBaker;


#if UNITY_EDITOR
public void OnEnable()
{
// Get prefabs baker monoBeh
_prefabsBaker = (PrefabsBaker)target;

// Get path of the package
var packagePath = GetPackagePath(this);


// Load assets
_closeButtonTexture = (Texture2D)AssetDatabase
.LoadAssetAtPath($"{packagePath}/Sprites/crossTexture.png", typeof(Texture2D));


// Load paths
var pathToCopiesFolder = PlayerPrefs.GetString("CopiesFolder");
_prefabsBaker.PathToCopiesFolder.Clear();
_prefabsBaker.PathToCopiesFolder.Append(pathToCopiesFolder);

var pathToMatsFolder = PlayerPrefs.GetString("MatFolder");
_prefabsBaker.PathToMatsFolder.Clear();
_prefabsBaker.PathToMatsFolder.Append(pathToMatsFolder);
}
#endif

public override void OnInspectorGUI()
{
// Draw inspector
base.DrawDefaultInspector();


#region GUIStyles

// Bold white text
var boldWhiteText = new GUIStyle();
boldWhiteText.fontStyle = FontStyle.Bold;
boldWhiteText.normal.textColor = Color.white;


// Cross button texture
var buttonTexture = new GUIStyle();
buttonTexture.normal.background = _closeButtonTexture;

#endregion


#region Select copies folder

GUILayout.Space(25);
GUILayout.BeginHorizontal();

var contentPref = new SFEContent();
contentPref.Style = boldWhiteText;
contentPref.DeletePathTexture = buttonTexture;
contentPref.Name = "CopiesFolder";
contentPref.LabelText = "Folder to save prefab copies to:";
contentPref.TooltipText = "Folder in which all the newly generated prefabs will be saved to. Recommended to create a new one.";
contentPref.PathToFolder = _prefabsBaker.PathToCopiesFolder;

SelectFolderElement.SelectFolder(contentPref);

GUILayout.EndHorizontal();

#endregion


#region Select material folder

GUILayout.Space(10);
GUILayout.BeginHorizontal();

var contentMat = new SFEContent();
contentMat.Style = boldWhiteText;
contentMat.DeletePathTexture = buttonTexture;
contentMat.Name = "MatFolder";
contentMat.LabelText = "Folder to save materials to:";
contentMat.TooltipText = "Folder in which all the newly generated materials will be saved to. Recommended to create a new one.";
contentMat.PathToFolder = _prefabsBaker.PathToMatsFolder;

SelectFolderElement.SelectFolder(contentMat);

GUILayout.EndHorizontal();


#endregion


#region Bake lighting elements

GUILayout.Space(25);
GUILayout.BeginHorizontal();

// Bake prefabs
if (GUILayout.Button("Bake Lights On Prefabs"))
{
// Check if there is no errors to proceed baking lightmaps
var canProceedBaking = _prefabsBaker.PreBaking();

if (canProceedBaking)
{
Lightmapping.Bake();
_prefabsBaker.PostBaking();
}
}

GUILayout.EndHorizontal();

#endregion


#region Clear lighting elements

GUILayout.Space(5);
GUILayout.BeginHorizontal();

var clearButton = new GUIContent("Clear all data", "Warning! Clears all the lightmap data on the scene " +
"and deletes all assets in selected folders.");

// Delete all the generated data
GUI.backgroundColor = Color.red;
if (GUILayout.Button(clearButton))
{
Lightmapping.Clear();
Lightmapping.ClearLightingDataAsset();
Lightmapping.ClearDiskCache();

_prefabsBaker.ClearData();
}
GUILayout.EndHorizontal();
#endregion

}



#region Menu Item

[MenuItem("GameObject/Prefabs Baker", priority = 0)]
public static void CreateEmpty()
{
GameObject prefabsBakerGO = new GameObject("Prefabs Baker");
Undo.RegisterCreatedObjectUndo(prefabsBakerGO, "Create prefabs baker");

prefabsBakerGO.AddComponent<PrefabsBaker>();
}
#endregion


#region Static Extensions

public static string GetPackagePath(ScriptableObject script)
{
MonoScript thisAsset = MonoScript.FromScriptableObject(script);
var filePath = AssetDatabase.GetAssetPath(thisAsset);

return filePath.Replace($"/Editor/{Path.GetFileName(filePath)}", "");
}
#endregion


#region Structs and Classes

public static class SelectFolderElement
{
public static void SelectFolder(SFEContent content)
{
bool pathSelected = false;

if (content.PathToFolder.Length > 0) pathSelected = true;
else pathSelected = false;


// Main label
var selectMatFolderContent = new GUIContent(content.LabelText, content.TooltipText);
GUILayout.Label(selectMatFolderContent, content.Style, GUILayout.Width(190));

GUILayout.Space(5);
GUILayout.BeginHorizontal();


if (!pathSelected)
{
if (GUILayout.Button("Select folder"))
{
// Select folder and save its path
// Folder was pathSelected this time
var path = content.PathToFolder.Append(EditorUtility.OpenFolderPanel("Select folder", "", ""));
path.Replace(Application.dataPath.Replace("Assets", ""), "");

PlayerPrefs.SetString(content.Name, content.PathToFolder.ToString());
GUIUtility.ExitGUI();
}
}
else
{
// Show path to the folder
GUILayout.Label(content.PathToFolder.ToString(), GUILayout.Width(200));

// Show clear button
// Pressing it will delete path to the folder, clean saved path
// Folder was not pathSelected this time
if (GUILayout.Button("", content.DeletePathTexture, GUILayout.MaxHeight(16), GUILayout.MaxWidth(16)))
{
content.PathToFolder.Clear();
PlayerPrefs.DeleteKey(content.Name);
}
}

GUILayout.EndHorizontal();
}
}


public class SFEContent
{
public GUIStyle Style, DeletePathTexture;
public string LabelText, TooltipText, Name;
public bool PathSelected;
public StringBuilder PathToFolder;
}
#endregion
}
11 changes: 11 additions & 0 deletions Editor/PrefabsBakerEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/BakePrefab.Runtime.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "BakePrefab.Runtime"
}
7 changes: 7 additions & 0 deletions Runtime/BakePrefab.Runtime.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit abbbff8

Please sign in to comment.