QQ聊天

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);
    }
}

 

 

posted @ 2015-07-20 16:49  SITT  阅读(10240)  评论(0编辑  收藏  举报
QQ聊天