Unity 2018 改进了 prefab 使用方法,prefab 可以嵌套,双击 prefab 会在新场景中打开。下面是代码保存 prefab 的方法。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void SavePrefab(BaseTable table)
{
    var status = PrefabUtility.GetPrefabInstanceStatus(table);
    if (status == PrefabInstanceStatus.Connected)
    {
        PrefabUtility.ApplyPrefabInstance(table.gameObject, InteractionMode.AutomatedAction);
    }
    else if (status == PrefabInstanceStatus.NotAPrefab)
    {
        var prefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
        if (prefabStage != null)
        {
            PrefabUtility.SaveAsPrefabAsset(table.gameObject, prefabStage.prefabAssetPath);
        }
        else
        {
            PrefabUtility.SavePrefabAsset(table.gameObject);
        }
    }
    else
    {
        Debug.LogError("prefab save failed : " + table.name);
    }
}