PropertyModification

作者:追风剑情 发布于:2018-1-12 14:05 分类:Unity3d

利用PropertyModification,在Prefab中保存额外属性信息。

示例

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

public class MenuEditor {

    [MenuItem("Tool/PrefabUtility.SetPropertyModifications")]
    static void SetPropertyModifications()
    {
        GameObject tar = Selection.activeObject as GameObject;
        TestPrefab test = tar.GetComponent<TestPrefab>();
        GameObject subPrefab = test.subPrefab;

        PropertyModification[] pms = new PropertyModification[1];
        PropertyModification pm = new PropertyModification();
        pm.target = test;
        pm.objectReference = subPrefab;
        pm.propertyPath = "propertyName";
        pm.value = "propertyValue";
        pms[0] = pm;

        PrefabUtility.RecordPrefabInstancePropertyModifications(tar);//设置撤销点
        PrefabUtility.SetPropertyModifications(tar, pms);//保存额外属性修改信息

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
    }

    [MenuItem("Tool/PrefabUtility.GetPropertyModifications")]
    static void GetPropertyModifications()
    {
        GameObject tar = Selection.activeObject as GameObject;
        PropertyModification[] pms = PrefabUtility.GetPropertyModifications(tar);
    }
}

用Notepad++打开修改后的prefab文件可以看到以下信息

Prefab:
  m_ObjectHideFlags: 1
  serializedVersion: 2
  m_Modification:
    m_TransformParent: {fileID: 0}
    m_Modifications:
    - target: {fileID: 11492202}
      propertyPath: propertyName
      value: propertyValue
      objectReference: {fileID: 127220, guid: 4d3b8ae75960389438056bc346026994, type: 2}
    m_RemovedComponents: []
  m_ParentPrefab: {fileID: 0}
  m_RootGameObject: {fileID: 122404}
  m_IsPrefabParent: 1
  m_IsExploded: 1

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号