一般在做编辑器的时候会给策划做一些脚本或者ScriptableObject,让策划进行或拽赋值等操作。举个例子假如开始策划说我只需要拖放一个GameObject,但是N天以后策划说这里想拖多个GameObject. 那么如果开始序列化的数据不是List<GameObject>那么就悲剧了,数据结构一变策划之前拖拽过的工作都玩白做了。。有些人为了做兼容不得不在写一个新的数据结构让策划来填写,但是这样就得是多个变量了,代码看起来比较丑了。
其实Unity也意识到这个问题了。他们提供了一个方案
FormerlySerializedAs(name)
public string a1;
[FormerlySerializedAs("a1")]
public string a2;
这样可以把a1删除了,然后 a1序列化的数据就保存在a2里。但是它这个有局限性,比如这里我想把a1的数据放到一个新的对象里就不行了。比如这样
public class NewBehaviourScript : MonoBehaviour {
public Hero hero;
}
[System.Serializable]
public class Hero
{
[FormerlySerializedAs("a1")]
public string a2;
}
而且它这个只能替换相同数据结构,假如我想GameObject放到List<GameObject>里也不行了。。。所以我写了一个批量赋值的工具。把MonoBehaviour和ScriptableObject以泛型的形式传进去,让旧的数据等于新的数据、然后在类里把就把旧的变量直接删除掉就好了。
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class TestEditor : Editor {
[MenuItem("1/ModifyPrefab")]
static void Test()
{
ModifyAsset.ModifyPrefab<NewBehaviourScript>(delegate(Component obj) {
NewBehaviourScript script = obj as NewBehaviourScript;
script.hero.objects = new List<GameObject>(){script.obj};
});
}
[MenuItem("1/ModifyScene")]
static void Test1()
{
ModifyAsset.ModifyScene<NewBehaviourScript>(delegate(Component obj){
NewBehaviourScript script = obj as NewBehaviourScript;
script.hero.objects = new List<GameObject>(){script.obj};
});
}
[MenuItem("1/ModifyScriptableObject")]
static void Test2()
{
ModifyAsset.ModifyScriptableObject<MyAsset>(delegate(ScriptableObject obj) {
MyAsset script = obj as MyAsset;
script.hero.objects = new List<GameObject>(){script.obj};
});
}
}
可能出现这个问题的只有三处
1.prefab里的数据
2.scene里的数据
3.ScriptableObject
核心代码就是遍历、找到以后回调出去。
#if UNITY_EDITOR
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine.SceneManagement;
using System.Reflection;
using System;
using UnityEditor.SceneManagement;
public class ModifyAsset
{
static public void ModifyPrefab<T> (Action<Component>callBack) where T: MonoBehaviour
{
string[] guids = AssetDatabase.FindAssets ("t:Prefab");
foreach (string guid in guids) {
string path = AssetDatabase.GUIDToAssetPath (guid);
GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject> (path);
T[] scripts = obj.GetComponentsInChildren<T> (true);
for (int i = 0; i < scripts.Length; i++)
{
callBack (scripts[i]);
}
EditorUtility.SetDirty (obj);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
}
static public void ModifyScene<T> (Action<Component>callBack) where T: MonoBehaviour
{
EditorSceneManager.SaveOpenScenes ();
foreach (EditorBuildSettingsScene editorBuildSettingsScene in EditorBuildSettings.scenes)
{
Scene scene=EditorSceneManager.OpenScene (editorBuildSettingsScene.path);
T[] scripts = Resources.FindObjectsOfTypeAll<T> ();
for (int i = 0; i < scripts.Length; i++)
{
callBack (scripts[i]);
}
EditorSceneManager.SaveScene(scene);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
}
static public void ModifyScriptableObject<T> (Action<ScriptableObject>callBack) where T: ScriptableObject
{
string[] guids = AssetDatabase.FindAssets ("t:ScriptableObject");
foreach (string guid in guids) {
string path = AssetDatabase.GUIDToAssetPath (guid);
if (path.EndsWith (".asset")) {
T script = AssetDatabase.LoadAssetAtPath<T> (path);
if (script != null) {
callBack (script);
EditorUtility.SetDirty (script);
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
}
}
}
}
#endif
序列化对象 ,把obj放到hero.objects[0]里面,然后在把obj删除就行了
using UnityEngine;
using System.Collections;
using UnityEngine.Serialization;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject obj;
public Hero hero;
}
[System.Serializable]
public class Hero
{
public List<GameObject> objects;
}
ScriptableObject
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[CreateAssetMenu]
public class MyAsset : ScriptableObject {
public GameObject obj;
public Hero hero;
}
OK这样数据就拷贝过去了。然后就可以把旧的数据结构删除了。
Unity3D研究院编辑器之序列化的对象修改格式(十九) - 雨松MOMO程序研究院 - 1做到这里其实还没有完,因为这里就算在脚本里删除了变量名, 那这个变量名之前序列化的数据还在prefab里序列化这。除非修改保存一下才行,所以最好在批量执行一下
EditorUtility.SetDirty (obj);
网友评论