FBX生成Prefab

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;

public class GeneratePrefab 
{
    private static string prefabDirectory = "/Prefab/Monster";
    private static string prefabExtension = ".prefab";

    [MenuItem("Example/Generate prefab")]
    public static void Generate()
    {
        GameObject selectedGameObject = Selection.activeGameObject;
        string selectedAssetPath = AssetDatabase.GetAssetPath(selectedGameObject);
        if(string.IsNullOrEmpty(selectedAssetPath))
        {
            return;
        }

        string modelAssetPath = string.Concat(Application.dataPath,prefabDirectory);
        string modelParentPath = string.Concat("Assets/Resources", prefabDirectory);
        string modelFullPath = string.Concat(modelParentPath, "/", "model");
        if (!Directory.Exists(modelFullPath))
        {
            AssetDatabase.CreateFolder(modelParentPath , "model");
        }
        GameObject cloneObj = GameObject.Instantiate(selectedGameObject);
        cloneObj.name = cloneObj.name.Replace("(Clone)", string.Empty);
        string genPrefabFullName = string.Concat(modelFullPath, "/", cloneObj.name, prefabExtension);

        Object prefabObj = PrefabUtility.CreateEmptyPrefab(genPrefabFullName);
        GameObject prefab = PrefabUtility.ReplacePrefab(cloneObj, prefabObj);
        GameObject.DestroyImmediate(cloneObj);
    }

}

你可能感兴趣的:(Unity3D基础)