Unity運行時保存prefab的方法一則

unity編輯器在運行狀態時,prefab的apply按鈕就消失了,其實此時代碼訪問的話是有效的。app

代碼以下,將會給transform的右鍵增長一個save prefab的選項。編輯器

using UnityEngine;
using UnityEditor;
using System.Collections;

static public class PrefabExtendTools
{

    [MenuItem("CONTEXT/Transform/SavePrefab")]
    static public void SavePrefab()
    {
        GameObject source = PrefabUtility.GetPrefabParent (Selection.activeGameObject) as GameObject;
        if(source == null) return;
        string prefabPath = AssetDatabase.GetAssetPath (source).ToLower ();
        if(prefabPath.EndsWith(".prefab") == false) return;
        PrefabUtility.ReplacePrefab (Selection.activeGameObject, source, ReplacePrefabOptions.ConnectToPrefab | ReplacePrefabOptions.ReplaceNameBased);
    }
}

 

 

相關文章
相關標籤/搜索