2022-01-16 02:52:59 +00:00
|
|
|
|
using SanAndreasUnity.Behaviours.World;
|
2022-01-16 04:00:29 +00:00
|
|
|
|
using SanAndreasUnity.Utilities;
|
2022-01-16 02:52:59 +00:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2022-01-16 04:00:29 +00:00
|
|
|
|
using System.Linq;
|
2022-01-16 02:52:59 +00:00
|
|
|
|
using UnityEditor;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Editor
|
|
|
|
|
{
|
|
|
|
|
public class AssetExporter : EditorWindowBase
|
|
|
|
|
{
|
|
|
|
|
private const string DefaultFolderName = "ExportedAssets";
|
|
|
|
|
private string m_selectedFolder = "Assets/" + DefaultFolderName;
|
|
|
|
|
|
|
|
|
|
string ModelsPath => m_selectedFolder + "/Models";
|
|
|
|
|
string CollisionModelsPath => m_selectedFolder + "/CollisionModels";
|
|
|
|
|
string MaterialsPath => m_selectedFolder + "/Materials";
|
|
|
|
|
string TexturesPath => m_selectedFolder + "/Textures";
|
|
|
|
|
string PrefabsPath => m_selectedFolder + "/Prefabs";
|
|
|
|
|
|
|
|
|
|
private CoroutineInfo m_coroutineInfo;
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
private bool m_exportFromSelection = false;
|
|
|
|
|
|
2022-01-16 02:52:59 +00:00
|
|
|
|
|
|
|
|
|
[MenuItem(EditorCore.MenuName + "/" + "Asset exporter")]
|
|
|
|
|
static void Init()
|
|
|
|
|
{
|
|
|
|
|
var window = GetWindow<AssetExporter>();
|
|
|
|
|
window.Show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AssetExporter()
|
|
|
|
|
{
|
|
|
|
|
this.titleContent = new GUIContent("Asset exporter");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnGUI()
|
|
|
|
|
{
|
|
|
|
|
EditorGUILayout.HelpBox(
|
|
|
|
|
"This tool can export all currenty loaded world objects as assets and prefabs.\n" +
|
|
|
|
|
"It will store them in a separate folder, and will only export those objects that were not already exported.",
|
|
|
|
|
MessageType.Info,
|
|
|
|
|
true);
|
|
|
|
|
|
|
|
|
|
GUILayout.Space(20);
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
if (GUILayout.Button("Export from world"))
|
|
|
|
|
this.Export(false);
|
|
|
|
|
|
|
|
|
|
if (GUILayout.Button("Export from selection"))
|
|
|
|
|
this.Export(true);
|
2022-01-16 02:52:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
void Export(bool fromSelection)
|
2022-01-16 02:52:59 +00:00
|
|
|
|
{
|
|
|
|
|
if (this.IsCoroutineRunning(m_coroutineInfo))
|
|
|
|
|
return;
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
m_exportFromSelection = fromSelection;
|
|
|
|
|
|
2022-01-16 02:52:59 +00:00
|
|
|
|
m_coroutineInfo = this.StartCoroutine(this.ExportCoroutine(), this.Cleanup, ex => this.Cleanup());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Cleanup()
|
|
|
|
|
{
|
|
|
|
|
EditorUtility.ClearProgressBar();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator ExportCoroutine()
|
|
|
|
|
{
|
|
|
|
|
yield return null;
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
if (m_exportFromSelection)
|
|
|
|
|
{
|
|
|
|
|
if (Selection.transforms.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
EditorUtility.DisplayDialog("", "No object selected.", "Ok");
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-16 02:52:59 +00:00
|
|
|
|
var cell = Cell.Instance;
|
2022-01-16 04:00:29 +00:00
|
|
|
|
if (null == cell && !m_exportFromSelection)
|
2022-01-16 02:52:59 +00:00
|
|
|
|
{
|
|
|
|
|
EditorUtility.DisplayDialog("", $"{nameof(Cell)} script not found in scene. Make sure that you started the game with the correct scene.", "Ok");
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EditorUtility.DisplayProgressBar("", "Gathering info...", 0f);
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
Transform[] objectsToExport = m_exportFromSelection
|
|
|
|
|
? Selection.transforms
|
|
|
|
|
: cell.transform.GetFirstLevelChildren().ToArray();
|
|
|
|
|
|
2022-01-16 02:52:59 +00:00
|
|
|
|
int numObjectsActive = 0;
|
2022-01-16 04:00:29 +00:00
|
|
|
|
for (int i = 0; i < objectsToExport.Length; i++)
|
2022-01-16 02:52:59 +00:00
|
|
|
|
{
|
2022-01-16 04:00:29 +00:00
|
|
|
|
var child = objectsToExport[i];
|
2022-01-16 02:52:59 +00:00
|
|
|
|
|
|
|
|
|
if (child.gameObject.activeInHierarchy)
|
|
|
|
|
numObjectsActive++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EditorUtility.ClearProgressBar();
|
|
|
|
|
|
|
|
|
|
if (!EditorUtility.DisplayDialog(
|
|
|
|
|
"",
|
2022-01-16 04:00:29 +00:00
|
|
|
|
$"There are {objectsToExport.Length} objects, with {numObjectsActive} active ones.\nProceed ?",
|
2022-01-16 02:52:59 +00:00
|
|
|
|
"Ok",
|
|
|
|
|
"Cancel"))
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_selectedFolder = EditorUtility.SaveFolderPanel(
|
|
|
|
|
"Select folder where to export files",
|
|
|
|
|
m_selectedFolder,
|
|
|
|
|
"");
|
|
|
|
|
if (string.IsNullOrWhiteSpace(m_selectedFolder))
|
|
|
|
|
{
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_selectedFolder = FileUtil.GetProjectRelativePath(m_selectedFolder);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(m_selectedFolder))
|
|
|
|
|
{
|
|
|
|
|
EditorUtility.ClearProgressBar();
|
|
|
|
|
EditorUtility.DisplayDialog("", "Folder must be inside project.", "Ok");
|
|
|
|
|
yield break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (EditorApplication.isPlaying)
|
|
|
|
|
EditorApplication.isPaused = true;
|
|
|
|
|
|
|
|
|
|
EditorUtility.DisplayProgressBar("", "Creating folders...", 0f);
|
|
|
|
|
|
|
|
|
|
this.CreateFolders();
|
|
|
|
|
|
|
|
|
|
EditorUtility.DisplayProgressBar("", "Creating assets...", 0f);
|
|
|
|
|
|
|
|
|
|
int numExported = 0;
|
2022-01-16 04:00:29 +00:00
|
|
|
|
for (int i = 0; i < objectsToExport.Length; i++)
|
2022-01-16 02:52:59 +00:00
|
|
|
|
{
|
2022-01-16 04:00:29 +00:00
|
|
|
|
var child = objectsToExport[i];
|
2022-01-16 02:52:59 +00:00
|
|
|
|
|
|
|
|
|
if (!child.gameObject.activeInHierarchy)
|
|
|
|
|
continue;
|
|
|
|
|
|
2022-01-16 04:00:29 +00:00
|
|
|
|
if (EditorUtility.DisplayCancelableProgressBar("", $"Creating assets... {child.name}", numExported / (float)numObjectsActive))
|
2022-01-16 02:52:59 +00:00
|
|
|
|
yield break;
|
|
|
|
|
|
|
|
|
|
this.ExportAssets(child.gameObject);
|
|
|
|
|
|
|
|
|
|
numExported++;
|
2022-01-16 04:00:29 +00:00
|
|
|
|
|
|
|
|
|
yield return null;
|
2022-01-16 02:52:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EditorUtility.DisplayProgressBar("", "Creating prefab...", 1f);
|
2022-01-16 04:00:29 +00:00
|
|
|
|
if (!m_exportFromSelection)
|
|
|
|
|
PrefabUtility.SaveAsPrefabAsset(cell.gameObject, $"{PrefabsPath}/{cell.gameObject.name}.prefab");
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
foreach (var obj in objectsToExport)
|
|
|
|
|
{
|
|
|
|
|
PrefabUtility.SaveAsPrefabAsset(obj.gameObject, $"{PrefabsPath}/{obj.gameObject.name}.prefab");
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-16 02:52:59 +00:00
|
|
|
|
|
|
|
|
|
EditorUtility.DisplayProgressBar("", "Refreshing asset database...", 1f);
|
|
|
|
|
AssetDatabase.Refresh();
|
|
|
|
|
|
|
|
|
|
EditorUtility.ClearProgressBar();
|
|
|
|
|
EditorUtility.DisplayDialog("", "Finished !", "Ok");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CreateFolders()
|
|
|
|
|
{
|
|
|
|
|
string[] folders = new string[]
|
|
|
|
|
{
|
|
|
|
|
"Models",
|
|
|
|
|
"Materials",
|
|
|
|
|
"Textures",
|
|
|
|
|
"Prefabs",
|
|
|
|
|
"CollisionModels",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach (string folder in folders)
|
|
|
|
|
{
|
|
|
|
|
if (!AssetDatabase.IsValidFolder(Path.Combine(m_selectedFolder, folder)))
|
|
|
|
|
AssetDatabase.CreateFolder(m_selectedFolder, folder);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ExportAssets(GameObject go)
|
|
|
|
|
{
|
|
|
|
|
string assetName = go.name;
|
|
|
|
|
|
|
|
|
|
var meshFilters = go.GetComponentsInChildren<MeshFilter>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < meshFilters.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
MeshFilter meshFilter = meshFilters[i];
|
|
|
|
|
string indexPath = meshFilters.Length == 1 ? "" : "-" + i;
|
|
|
|
|
CreateAssetIfNotExists(meshFilter.sharedMesh, $"{ModelsPath}/{assetName}{indexPath}.asset");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var meshRenderers = go.GetComponentsInChildren<MeshRenderer>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < meshRenderers.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
ExportMeshRenderer(go, meshRenderers[i], meshRenderers.Length == 1 ? (int?)null : i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var meshColliders = go.GetComponentsInChildren<MeshCollider>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < meshColliders.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
string indexPath = meshColliders.Length == 1 ? "" : "-" + i;
|
|
|
|
|
CreateAssetIfNotExists(meshColliders[i].sharedMesh, $"{CollisionModelsPath}/{assetName}{indexPath}.asset");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//PrefabUtility.SaveAsPrefabAsset(go, $"{PrefabsPath}/{assetName}.prefab");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ExportMeshRenderer(GameObject rootGo, MeshRenderer meshRenderer, int? index)
|
|
|
|
|
{
|
|
|
|
|
string indexPath = index.HasValue ? "-" + index.Value : "";
|
|
|
|
|
string assetName = rootGo.name + indexPath;
|
|
|
|
|
|
|
|
|
|
var mats = meshRenderer.sharedMaterials;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < mats.Length; i++)
|
|
|
|
|
{
|
2022-01-16 04:01:46 +00:00
|
|
|
|
var tex = mats[i].mainTexture;
|
|
|
|
|
if (tex != null)
|
|
|
|
|
CreateAssetIfNotExists(tex, $"{TexturesPath}/{assetName}-{i}.asset");
|
2022-01-16 02:52:59 +00:00
|
|
|
|
CreateAssetIfNotExists(mats[i], $"{MaterialsPath}/{assetName}-{i}.mat");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool CreateAssetIfNotExists(Object asset, string path)
|
|
|
|
|
{
|
|
|
|
|
if (AssetDatabase.Contains(asset))
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-01-16 03:26:03 +00:00
|
|
|
|
if (File.Exists(Path.Combine(Application.dataPath + "/../", path)))
|
|
|
|
|
return false;
|
2022-01-16 02:52:59 +00:00
|
|
|
|
|
2022-01-16 03:26:03 +00:00
|
|
|
|
AssetDatabase.CreateAsset(asset, path);
|
|
|
|
|
return true;
|
2022-01-16 02:52:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|