using UnityEditor;
using UnityEngine;
public class ExportObjects : EditorWindow
{
//Creates a new menu (Examples) with a menu item (Create Prefab)
[MenuItem("GameObject/Create Prefab", false, -1)]
static void CreatePrefab()
{
//Keep track of the currently selected GameObject(s)
GameObject[] objectArray = Selection.gameObjects;
//Loop through every GameObject in the array above
foreach (GameObject gameObject in objectArray)
{
//Set the path as within the Assets folder, and name it as the GameObject's name with the .prefab format
string localPath = "Assets/Prefabs to export/" + gameObject.name + ".prefab";
//Check if the Prefab and/or name already exists at the path
if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)))
{
//Create dialog to ask if User is sure they want to overwrite existing Prefab
if (EditorUtility.DisplayDialog("Are you sure?",
"The Prefab already exists. Do you want to overwrite it?",
"Yes",
"No"))
//If the user presses the yes button, create the Prefab
{
CreateNew(gameObject, localPath);
Export(gameObject, localPath);
}
}
//If the name doesn't exist, create the new Prefab
else
{
Debug.Log(gameObject.name + " is not a Prefab, will convert");
CreateNew(gameObject, localPath);
Export(gameObject, localPath);
}
}
}
// Disable the menu item if no selection is in place
[MenuItem("GameObject/Create Prefab", true, -1)]
static bool ValidateCreatePrefab()
{
return Selection.activeGameObject != null;
}
static void CreateNew(GameObject obj, string localPath)
{
//Create a new Prefab at the path given
Object prefab = PrefabUtility.SaveAsPrefabAsset(obj, localPath);
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
}
static void Export(GameObject obj, string localPath)
{
AssetDatabase.ExportPackage(localPath, obj.name);
}
}
Первая проблема заключается в методе экспорта.Он добирается и делает строку:
AssetDatabase.ExportPackage(localPath, obj.name);
Но он не создает никакого файла пакета на жестком диске в localPath.
Вторая проблема заключается в том, что я не уверен, что делаю ложь, -1 правильно (но это работает).
В-третьих, как изменить строку:
PrefabUtility.ReplacePrefab(obj, prefab, ReplacePrefabOptions.ConnectToPrefab);
ReplacePrefab устарел и должен быть заменен, как в предыдущей строке: SaveAsPrefabAsset и Iпробовал, но в этих строках параметры не такие, как указано выше: obj, prefab, ReplacePrefabOptions.ConnectToPrefab Так что я не уверен, как это сделать.