private static JSONObject GetPrefabOnSerializedField(WXHierarchyContext context, GameObject go, JSONObject innerData, bool isGameObject = true, Component comp = null) { // Prefab? var path = AssetDatabase.GetAssetPath(go); if (go.transform.IsChildOf(context.Root.transform) || path == "") { GetGameObjectReferenceIndex(context, go, ref innerData, isGameObject); return(innerData); } if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go)) { WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go); WXPrefab converter = new WXPrefab(go, path); string prefabPath = converter.Export(ExportPreset.GetExportPreset("prefab")); context.AddResource(prefabPath); } var prefabInfo = new JSONObject(JSONObject.Type.OBJECT); var typeName = comp ? comp.GetType().FullName : "UnityEngine.GameObject"; var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(typeName); prefabInfo.AddField("type", escapedTypeName); prefabInfo.AddField("path", path); innerData.AddField("type", "UnityPrefabWrapper"); innerData.AddField("value", prefabInfo); return(innerData); // GetGameObjectReferenceIndex(context, go, ref innerData, isGameObject); // return innerData; }
private void exportPrefabResource(GameObject go, WXEntity entity) { #if UNITY_2018_4_OR_NEWER GameObject sourceObj = PrefabUtility.GetCorrespondingObjectFromOriginalSource(go); if (preset.is2d) { // Debug.LogError("preset.is2d" + preset.is2d); // Debug.LogError(prefab2dType); // prefabPath WXResource prefabInstance = (WXResource)Activator.CreateInstance(prefab2dType, new object[] { sourceObj, entity.prefabPath, false, false }); AddResource(prefabInstance.Export(preset)); } else { // Debug.LogError("preset.is2d" + preset.is2d); WXPrefab prefab = new WXPrefab(sourceObj, entity.unityAssetPath); AddResource(prefab.Export(preset)); } #else GameObject sourceObj = PrefabUtility.GetPrefabParent(go) as GameObject; if (preset.is2d) { // Debug.LogError("preset.is2d" + preset.is2d); // Debug.LogError(prefab2dType); WXResource prefabInstance = (WXResource)Activator.CreateInstance(prefab2dType, new object[] { sourceObj, entity.prefabPath, false, false }); AddResource(prefabInstance.Export(preset)); // WXNGUITree prefab = new WXNGUITree(sourceObj, entity.prefabPath, false); // AddResource(prefab.Export(preset)); } else { // Debug.LogError("preset.is2d" + preset.is2d); WXPrefab prefab = new WXPrefab(sourceObj, entity.unityAssetPath); AddResource(prefab.Export(preset)); } #endif }
private static void RegisterUnityProperties() { AddPropertyHandler(typeof(Vector2), (obj, context) => { Vector2 v = (Vector2)obj; if (v == null) { return(null); } JSONObject vec2 = new JSONObject(JSONObject.Type.ARRAY); vec2.Add(v.x); vec2.Add(v.y); return(vec2); }); AddPropertyHandler(typeof(Vector3), (obj, context) => { Vector3 v = (Vector3)obj; if (v == null) { return(null); } JSONObject vec3 = new JSONObject(JSONObject.Type.ARRAY); vec3.Add(v.x); vec3.Add(v.y); vec3.Add(v.z); return(vec3); }); AddPropertyHandler(typeof(Vector4), (obj, context) => { Vector4 v = (Vector4)obj; if (v == null) { return(null); } JSONObject vec4 = new JSONObject(JSONObject.Type.ARRAY); vec4.Add(v.x); vec4.Add(v.y); vec4.Add(v.z); vec4.Add(v.w); return(vec4); }); AddPropertyHandler(typeof(Quaternion), (obj, context) => { Quaternion v = (Quaternion)obj; if (v == null) { return(null); } JSONObject array4 = new JSONObject(JSONObject.Type.ARRAY); array4.Add(v.x); array4.Add(v.y); array4.Add(v.z); array4.Add(v.w); return(array4); }); AddPropertyHandler(typeof(Matrix4x4), (obj, context) => { Matrix4x4 v = (Matrix4x4)obj; if (v == null) { return(null); } JSONObject array16 = new JSONObject(JSONObject.Type.ARRAY); array16.Add(v.m00); array16.Add(v.m01); array16.Add(v.m02); array16.Add(v.m03); array16.Add(v.m10); array16.Add(v.m11); array16.Add(v.m12); array16.Add(v.m13); array16.Add(v.m20); array16.Add(v.m21); array16.Add(v.m22); array16.Add(v.m23); array16.Add(v.m30); array16.Add(v.m31); array16.Add(v.m32); array16.Add(v.m33); return(array16); }); AddPropertyHandler(typeof(Color), (obj, context) => { Color c = (Color)obj; if (c == null) { return(null); } JSONObject vec4 = new JSONObject(JSONObject.Type.ARRAY); vec4.Add((int)(c.r * 255)); vec4.Add((int)(c.g * 255)); vec4.Add((int)(c.b * 255)); vec4.Add((int)(c.a * 255)); return(vec4); }); AddPropertyHandler(typeof(TextAsset), (obj, context) => { TextAsset t = (TextAsset)obj; if (!t) { return(null); } string path = AssetDatabase.GetAssetPath(t); // string copyToPath = Path.Combine(WXResourceStore.storagePath, path); // Debug.Log("WXResourceStore.storagePath:" + WXResourceStore.storagePath); // Debug.Log("path:" + path); // Debug.Log("copyToPath:" + copyToPath); // Regex regex = new Regex(".txt$"); // copyToPath = regex.Replace(copyToPath, ".json"); // if (!Directory.Exists(WXResourceStore.storagePath + "Assets/")) { // Directory.CreateDirectory(WXResourceStore.storagePath + "Assets/"); // } // FileStream fs = new FileStream(copyToPath, FileMode.Create, FileAccess.Write); // wxFileUtil.WriteData(fs, JsonConvert.SerializeObject(new { text = t.text })); // fs.Close(); JSONObject json = new JSONObject(JSONObject.Type.OBJECT); JSONObject data = new JSONObject(JSONObject.Type.OBJECT); // Debug.Log("JsonConvert.SerializeObject(t.text): " + JsonConvert.SerializeObject(t.text)); string text = JsonConvert.SerializeObject(t.text); // 去掉首尾双引号 text = text.Remove(0, 1); text = text.Substring(0, text.Length - 1); data.AddField("text", text); data.AddField("path", path); json.AddField("type", "UnityEngine.TextAsset".EscapeNamespaceSimple()); json.AddField("value", data); return(json); }); AddPropertyHandler(typeof(Material), (obj, context) => { Material material = (Material)obj; if (material == null) { return(null); } WXMaterial materialConverter = new WXMaterial(material, null); string materialPath = materialConverter.Export(context.preset); context.AddResource(materialPath); JSONObject json = new JSONObject(JSONObject.Type.OBJECT); JSONObject data = new JSONObject(JSONObject.Type.OBJECT); json.AddField("type", "UnityEngine.Material".EscapeNamespaceSimple()); json.AddField("value", data); data.AddField("path", materialPath); return(json); }); AddPropertyHandler(typeof(List <>), (obj, context) => { IEnumerable list = (IEnumerable)obj; if (list == null) { return(null); } JSONObject result = new JSONObject(JSONObject.Type.ARRAY); var enumerator = ((IEnumerable)list).GetEnumerator(); while (enumerator.MoveNext()) { object itemObj = enumerator.Current; if (itemObj == null) { continue; } if (itemObj.GetType() == typeof(List <>)) { throw new Exception("List不支持嵌套"); } else { Type type = itemObj.GetType(); if (type.IsSubclassOf(typeof(UnityEngine.Component)) || type == typeof(UnityEngine.GameObject)) { var o = (UnityEngine.Object)itemObj; GameObject go = null; if (o == null) { continue; } if (type.IsSubclassOf(typeof(UnityEngine.Component))) { go = ((Component)o).gameObject; } else if (type == typeof(UnityEngine.GameObject)) { go = (GameObject)o; } var path = AssetDatabase.GetAssetPath(o); // Prefab? if (go.transform.IsChildOf(context.Root.transform) || path == "") { if (!propertiesHandlerDictionary.ContainsKey(type) && !type.IsArray && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(List <>)) && type.IsSerializable) { var sobj = new JSONObject(JSONObject.Type.OBJECT); result.Add(sobj); SerializableHandler(type, sobj, itemObj, context); } else if (type != typeof(System.Object) && propertiesHandlerDictionary.ContainsKey(type)) { var res = propertiesHandlerDictionary[type](itemObj, context); result.Add(res); } continue; } if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go)) { WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go); WXPrefab converter = new WXPrefab(go, path); string prefabPath = converter.Export(ExportPreset.GetExportPreset("prefab")); context.AddResource(prefabPath); } var prefabInfo = new JSONObject(JSONObject.Type.OBJECT); prefabInfo.AddField("type", type.FullName.EscapeNamespaceSimple()); prefabInfo.AddField("path", path); var innerData = new JSONObject(JSONObject.Type.OBJECT); innerData.AddField("type", "UnityPrefabWrapper"); innerData.AddField("value", prefabInfo); // data.AddField(field.Name, innerData); result.Add(innerData); } else if (!propertiesHandlerDictionary.ContainsKey(type) && !type.IsArray && (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(List <>)) && type.IsSerializable) { var sobj = new JSONObject(JSONObject.Type.OBJECT); result.Add(sobj); SerializableHandler(type, sobj, itemObj, context); } else if (type != typeof(System.Object) && propertiesHandlerDictionary.ContainsKey(type)) { var res = propertiesHandlerDictionary[type](itemObj, context); result.Add(res); } } } return(result); }); // disgusting code logic :( // refactor should be needed AddPropertyHandler(typeof(MonoBehaviour), (obj, context) => { var m = (MonoBehaviour)obj; if (!m) { return(null); } if (ExportLogger.LOGGING) { ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Property, "Object: " + obj + "\nType: " + obj.GetType())); } var go = m.gameObject; JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT); var path = AssetDatabase.GetAssetPath(go); // Prefab? if (go.transform.IsChildOf(context.Root.transform) || path == "") { var typeName = m.GetType().FullName; var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(typeName); innerData.AddField("type", escapedTypeName); innerData.AddField("value", context.AddComponentInProperty(new WXEngineMonoBehaviour(m), (Component)obj)); return(innerData); } if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go)) { WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go); WXPrefab converter = new WXPrefab(go, path); string prefabPath = converter.Export(ExportPreset.GetExportPreset("prefab")); context.AddResource(prefabPath); } var prefabInfo = new JSONObject(JSONObject.Type.OBJECT); { var typeName = m ? m.GetType().FullName : "UnityEngine.GameObject"; var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(typeName); prefabInfo.AddField("type", escapedTypeName); } prefabInfo.AddField("path", path); innerData.AddField("type", "UnityPrefabWrapper"); innerData.AddField("value", prefabInfo); return(innerData); }); AddPropertyHandler(typeof(Component), (obj, context) => { Component c = (Component)obj; if (!c) { return(null); } if (ExportLogger.LOGGING) { ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Property, "Object: " + obj + "\nType: " + obj.GetType())); } JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT); var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(c.GetType().FullName); innerData.AddField("type", escapedTypeName); if (c is Transform) { innerData = GetPrefabOnSerializedField(context, c.gameObject, innerData, false); } // 下面是adaptor独有的类才需要单独写 // Physics //else if (c is BoxCollider) { // innerData.AddField("value", context.AddComponent(new WXBoxCollider((BoxCollider)c), (BoxCollider)c)); //} else if (c is SphereCollider) { // innerData.AddField("value", context.AddComponent(new WXSphereCollider((SphereCollider)c), (SphereCollider)c)); //} else if (c is CapsuleCollider) { // innerData.AddField("value", context.AddComponent(new WXCapsuleCollider((CapsuleCollider)c), (CapsuleCollider)c)); //} else if (c is MeshCollider) { // innerData.AddField("value", context.AddComponent(new WXMeshCollider((MeshCollider)c), (MeshCollider)c)); //} else if (c is Rigidbody) { // innerData.AddField("value", context.AddComponent(new WXRigidbody((Rigidbody)c), (Rigidbody)c)); //} else if (c is AudioSource) { innerData.AddField("value", context.AddComponentInProperty(new WXEngineAudioSource((AudioSource)c), (AudioSource)c)); } // 有ref的类 else { // if (context.componentDictionary.ContainsKey(c)) { innerData.AddField("value", context.AddComponentInProperty(new WXUnityComponent(c), c)); // } } return(innerData); }); AddPropertyHandler(typeof(GameObject), (obj, context) => { var go = (GameObject)obj; if (!go) { return(null); } if (ExportLogger.LOGGING) { ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Property, "Object: " + obj + "\nType: " + obj.GetType())); } JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT); return(GetPrefabOnSerializedField(context, go, innerData)); }); }
// inner recursion method, thus each if-statement must have @return in the end of the following block. private static void innerHandleProperty(Type type, FieldInfo field, object obj, JSONObject data, WXHierarchyContext context) { if (ExportLogger.LOGGING) { ExportLogger.AddLog(new ExportLogger.Log(ExportLogger.Log.Type.Inner, "Type: " + type + "\nObject: " + obj + "\nJson: " + data)); } if (obj == null || type == null || type == typeof(System.Object) || field.IsDefined(typeof(NonSerializedAttribute)) || field.IsDefined(typeof(HideInInspector))) { return; } if (WXBridge.isNGUIPreset) { Type wxnguiType = WXMonoBehaviourExportHelper.GetWXNGUIComponentType(type); if (wxnguiType != null) { var nativeComp = (Component)field.GetValue(obj); if (nativeComp != null) { var go = nativeComp.gameObject; Debug.Log(nativeComp); var entity = context.MakeEntity(go); // 特殊处理 if (wxnguiType.FullName == "WXEngine.WXTransform2DComponent") { data.AddField(field.Name, context.AddComponentInProperty( (WXComponent)Activator.CreateInstance(wxnguiType, (object)nativeComp.transform), nativeComp.transform )); } else { data.AddField(field.Name, context.AddComponentInProperty( (WXComponent)Activator.CreateInstance(wxnguiType, nativeComp, go, entity), nativeComp )); } } } } if (WXBridge.isUGUIPreset) { Type wxuguiType = WXMonoBehaviourExportHelper.GetWXUGUIComponentType(type); if (wxuguiType != null) { var nativeComp = (Component)field.GetValue(obj); if (nativeComp != null) { var go = nativeComp.gameObject; Debug.Log(nativeComp); var entity = context.MakeEntity(go); // 特殊处理 if (wxuguiType.FullName == "WeChat.WXUGUITransform2DComponent") { data.AddField(field.Name, context.AddComponent( (WXComponent)Activator.CreateInstance(wxuguiType, (object)nativeComp.transform), nativeComp.transform )); } else { data.AddField(field.Name, context.AddComponent( (WXComponent)Activator.CreateInstance(wxuguiType, nativeComp, go, entity), nativeComp )); } } } } if (WXBridge.isUGUIPreset) { Type wxuguiType = WXMonoBehaviourExportHelper.GetWXUGUIComponentType(type); if (wxuguiType != null) { var nativeComp = (Component)field.GetValue(obj); if (nativeComp != null) { var go = nativeComp.gameObject; Debug.Log(nativeComp); var entity = context.MakeEntity(go); // 特殊处理 if (wxuguiType.FullName == "WeChat.WXUGUITransform2DComponent") { data.AddField(field.Name, context.AddComponent( (WXComponent)Activator.CreateInstance(wxuguiType, (object)nativeComp.transform), nativeComp.transform )); } else { data.AddField(field.Name, context.AddComponent( (WXComponent)Activator.CreateInstance(wxuguiType, nativeComp, go, entity), nativeComp )); } } } } if (WXMonoBehaviourExportHelper.IsInBlackList(type)) { return; } if (propertiesHandlerDictionary.ContainsKey(type)) { var res = InvokePropertyHandler(type, field.GetValue(obj), context); if (res) { data.AddField(field.Name, res); } return; } else if (type.IsArray || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))) { var res = InvokePropertyHandler(typeof(List <>), field.GetValue(obj), context); if (res) { data.AddField(field.Name, res); } return; } else if (type.IsEnum) { var res = JSONObject.Create((int)field.GetValue(obj)); if (res) { data.AddField(field.Name, res); } return; } else if (type.IsSubclassOf(typeof(UnityEngine.Component)) || type == typeof(UnityEngine.GameObject)) { // Prefab or not var o = (UnityEngine.Object)field.GetValue(obj); GameObject go = null; if (o == null) { return; } if (type.IsSubclassOf(typeof(UnityEngine.Component))) { go = ((Component)o).gameObject; } else if (type == typeof(UnityEngine.GameObject)) { go = (GameObject)o; } var path = AssetDatabase.GetAssetPath(o); if (go.transform.IsChildOf(context.Root.transform) || path == "") { if (type.IsSerializable) { var _innerData = new JSONObject(JSONObject.Type.OBJECT); data.AddField(field.Name, _innerData); SerializableHandler(type, _innerData, field.GetValue(obj), context); return; } innerHandleProperty(type.BaseType, field, obj, data, context); return; } if (!WXMonoBehaviourExportHelper.exportedResourcesSet.Contains(go)) { WXMonoBehaviourExportHelper.exportedResourcesSet.Add(go); WXPrefab converter = new WXPrefab(go, path); string prefabPath = converter.Export(ExportPreset.GetExportPreset("prefab")); context.AddResource(prefabPath); } var prefabInfo = new JSONObject(JSONObject.Type.OBJECT); prefabInfo.AddField("type", type.FullName.EscapeNamespaceSimple()); prefabInfo.AddField("path", path); var innerData = new JSONObject(JSONObject.Type.OBJECT); innerData.AddField("type", "UnityPrefabWrapper"); innerData.AddField("value", prefabInfo); data.AddField(field.Name, innerData); return; } else if (type.IsSerializable) { // data of serializable object as a new JSONObject to be added var innerData = new JSONObject(JSONObject.Type.OBJECT); data.AddField(field.Name, innerData); SerializableHandler(type, innerData, field.GetValue(obj), context); return; } innerHandleProperty(type.BaseType, field, obj, data, context); }
private static void RegisterConditionProperties() { AddConditionPropertyHandler( (Type type) => { return(type.IsArray || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))); }, (type, value, context, requireList) => { return(typePropertiesHandlerDictionary[typeof(List <>)](value, context, requireList)); } ); AddConditionPropertyHandler( (Type type) => { return(type.IsEnum); }, (type, value, context, requireList) => { return(JSONObject.Create((int)value)); } ); AddConditionPropertyHandler( (Type type) => { return(type.IsSubclassOf(typeof(UnityEngine.Component)) || type == typeof(UnityEngine.GameObject)); }, (type, value, context, requireList) => { // 取值 var o = (UnityEngine.Object)value; GameObject go = null; if (o == null) { return(null); } // 尝试获得资源路径 var path = AssetDatabase.GetAssetPath(o); // 如果是组件 if (type.IsSubclassOf(typeof(UnityEngine.Component))) { go = ((Component)o).gameObject; } // 如果直接是一个GameObject else if (type == typeof(UnityEngine.GameObject)) { go = (GameObject)o; } // 如果拿得到路径,且发现transform不属于当前场景,则当prefab处理 if (path != "" && !go.transform.IsChildOf(context.Root.transform)) { // 按prefab导出 // 排重? WXPrefab converter = new WXPrefab(go, path); string prefabPath = converter.Export(ExportPreset.GetExportPreset("prefab")); context.AddResource(prefabPath); var prefabInfo = new JSONObject(JSONObject.Type.OBJECT); prefabInfo.AddField("type", type.FullName.EscapeNamespaceSimple()); prefabInfo.AddField("path", path); // prefab数据 var innerData = new JSONObject(JSONObject.Type.OBJECT); innerData.AddField("type", "UnityPrefabWrapper"); innerData.AddField("value", prefabInfo); return(innerData); } return(null); } ); AddConditionPropertyHandler( (Type type) => { return(typePropertiesHandlerDictionary.ContainsKey(type)); }, (type, value, context, requireList) => { return(typePropertiesHandlerDictionary[type](value, context, requireList)); } ); AddConditionPropertyHandler( (Type type) => { // 基础类型都是isSerializable return(type.IsSerializable); }, (type, value, context, requireList) => { var innerData = new JSONObject(JSONObject.Type.OBJECT); SerializableHandler(type, innerData, value, context, requireList); return(innerData); } ); }