最近在项目中需要开发一套地图数据生成编辑器,记录自己在这个过程中使用的一些好用的创建和替换Prefab的方法。
PrefabUtility.CreatePrefab(localPath,obj) 这个方法可以将场景中的GameObject对象创建成Prefab,官方文档中有给出例子:
using UnityEngine;
using UnityEditor;
public class Example : EditorWindow
{
//Creates a new menu (Examples) with a menu item (Create Prefab)
[MenuItem("Examples/Create Prefab")]
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/" + gameObject.name + ".prefab";
//Check if the Prefab and/or name already exists at the path
if (Ass