Unity运行时保存prefab的方法一则
unity编辑器在运行状态时,prefab的apply按钮就消失了,其实此时代码访问的话是有效的。
代码如下,将会给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); } }