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; }
// inner recursion method, thus each if-statement must have @return in the end of the following block. private static JSONObject innerHandleField(Type type, object value, WXHierarchyContext context, List <string> requireList) { // 因为下方有递归尝试转换基类的逻辑,这里需要有一个终结点 if (type == typeof(System.Object)) { return(null); } if (WXMonoBehaviourExportHelper.IsInBlackList(type)) { return(null); } foreach (ConditionPropertiesHandler handler in conditionPropertiesHandlerList) { if (handler.condition.Invoke(type)) { JSONObject result = handler.handler.Invoke(type, value, context, requireList); if (result != null) { return(result); } } } // 尝试转换它的基类 return(innerHandleField(type.BaseType, value, context, requireList)); }
public override string GetExportPath() { var scriptPath = Path.Combine("bindings", WXMonoBehaviourExportHelper.GetValidTypeNameUnescapeNamespace(script.GetClass()) + ".binding.js"); // scriptPath = Path.ChangeExtension(scriptPath, ".binding.js"); var path = scriptPath.Replace('\\', '/'); // Path.Combine("Assets/", scriptPath); return(path); }
public override string getTypeName() { string result; if (audio) { result = this.audio.GetType().ToString(); } else { result = "UnityEngine.AudioSource"; } return(WXMonoBehaviourExportHelper.EscapeNamespace(result)); }
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); }
public override string getTypeName() { var result = rigidbody ? rigidbody.GetType().ToString() : "UnityEngine.Rigidbody"; return(WXMonoBehaviourExportHelper.EscapeNamespace(result)); }
public override string getTypeName() { var result = comp ? comp.GetType().ToString() : "UnityRefComponent"; return(WXMonoBehaviourExportHelper.EscapeNamespace(result)); }
private static void RegisterUnityProperties() { AddTypePropertyHandler(typeof(Vector2), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(Vector3), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(Vector4), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(Quaternion), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(Matrix4x4), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(Color), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(TextAsset), (obj, context, requireList) => { 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 = t.text.Replace("\r\n", "\\n").Replace("\\", "\\\\").Replace("\"", "\\\""); data.AddField("text", text); data.AddField("path", path); json.AddField("type", "UnityEngine.TextAsset".EscapeNamespaceSimple()); json.AddField("value", data); return(json); }); AddTypePropertyHandler(typeof(Material), (obj, context, requireList) => { 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); }); AddTypePropertyHandler(typeof(UnityEngine.LayerMask), (obj, context, requireList) => { LayerMask mask = (LayerMask)obj; return(JSONObject.Create(mask.value)); }); AddTypePropertyHandler(typeof(List <>), (obj, context, requireList) => { 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(); JSONObject itemResult = innerHandleField(type, itemObj, context, requireList); if (itemResult != null) { result.Add(itemResult); } } } return(result); }); AddTypePropertyHandler(typeof(PuertsBeefBallSDK.RemoteResource), (obj, context, requireList) => { var m = (PuertsBeefBallSDK.RemoteResource)obj; return(JSONObject.CreateStringObject(m.resourcePath)); }); // disgusting code logic :( // refactor should be needed AddTypePropertyHandler(typeof(PuertsBeefBallBehaviour), (obj, context, requireList) => { var m = (PuertsBeefBallBehaviour)obj; if (!m) { return(null); } JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT); var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(m.GetType().FullName); innerData.AddField("type", escapedTypeName); innerData.AddField("value", context.AddComponentInProperty(new PuertsBeefBallBehaviourConverter(m), (Component)obj)); return(innerData); }); AddTypePropertyHandler(typeof(Component), (obj, context, requireList) => { Component c = (Component)obj; if (!c) { return(null); } JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT); var escapedTypeName = WXMonoBehaviourExportHelper.EscapeNamespace(c.GetType().FullName); innerData.AddField("type", escapedTypeName); innerData.AddField("value", context.AddComponentInProperty(new WXUnityComponent(c), c)); return(innerData); }); // 在前面condition逻辑里,理论上已经把所有可能为prefab的逻辑走完 AddTypePropertyHandler(typeof(GameObject), (obj, context, requireList) => { throw new Exception("不支持节点属性指向GameObject,请改为指向Transform或是具体逻辑组件"); // var go = (GameObject)obj; // if (!go) return null; // JSONObject innerData = new JSONObject(JSONObject.Type.OBJECT); // return GetPrefabOnSerializedField(context, go, innerData); }); }
public void AfterOutput(ITranslator translator, string outputPath, bool nocore) { IEnumerable <TranslatorOutputItem> outputForHtml = translator.Outputs.GetOutputs(); var scriptTemplate = "import './{0}';"; var firstJs = true; var jsBuffer = new StringBuilder(); ILogger outputLogger = this.Logger; foreach (var output in outputForHtml) { if (output.OutputType == TranslatorOutputType.JavaScript) { var path = output.GetOutputPath(outputPath, true, outputLogger); // blacklist if (WXBridge.isWXBridgePlugin) { if (path.IndexOf("minigame-adaptor-lib.js") >= 0 || path.IndexOf("minigame-adaptor-lib.meta.js") >= 0 || path.IndexOf("minigame-adaptor-lib-patch.js") >= 0) { continue; } } if (!firstJs) { jsBuffer.Append(NEW_LINE); } firstJs = false; path = Uri.UnescapeDataString(path); path = path.Replace("'", "\\\'"); jsBuffer.Append(string.Format(scriptTemplate, path)); } } File.WriteAllText(Path.Combine(outputPath, pluginFullName + ".js"), jsBuffer.ToString(), Encoding.UTF8); Logger.Trace(String.Format("location: {0}", translator.Location)); var bindingPath = Path.Combine(outputPath, "bindings"); if (!Directory.Exists(bindingPath)) { Directory.CreateDirectory(bindingPath); } foreach (var bindingTypePair in bindingTypes) { var jsFilePath = convertToOutputPath(translator, bindingPath, bindingTypePair.Key); if (File.Exists(jsFilePath)) { File.Delete(jsFilePath); } var output = new StringBuilder(); var importRelPath = GetRelativePath(Path.Combine(outputPath, "weapp-adapter"), Path.GetDirectoryName(jsFilePath)); Logger.Trace(String.Format("generate binding js {0}", jsFilePath)); // if (WXBridge.isWXBridgePlugin) { // output.Append(String.Format("import {{ requireBridge }} from '{0}'\r\n", getBindingFileImportPath("bridge_util", outputPath, jsFilePath))); // output.Append("requireBridge()\n"); // // output.Append("requireBridge({\n pluginAlias: 'WXBridge',\n useLocalFile: false,\n useLocalBridgeAdaptor: false\n})})"); // } else { // output.Append(String.Format("import '{0}'\r\n", getBindingFileImportPath("bridge", outputPath, jsFilePath))); // output.Append(String.Format("import '{0}'\r\n", getBindingFileImportPath("bridge.meta", outputPath, jsFilePath))); // output.Append(String.Format("import '{0}'\r\n", getBindingFileImportPath("weapp-adapter", outputPath, jsFilePath))); // output.Append(String.Format("import '{0}'\r\n", getBindingFileImportPath("bridge_adaptor", outputPath, jsFilePath))); // } output.Append(String.Format("import '{0}'\r\n", getBindingFileImportPath("minigame-adaptor-util", outputPath, jsFilePath))); output.Append(String.Format("import '{0}'\r\n", getBindingFileImportPath(pluginFullName, outputPath, jsFilePath))); foreach (var bindingType in bindingTypePair.Value) { generateBindingFile(output, translator, bindingPath, jsFilePath, bindingType); } // EscapeNamespace var textToWrite = WXMonoBehaviourExportHelper.EscapeNamespace(output.ToString()); File.WriteAllText(jsFilePath, textToWrite); } }
private static void IterateBridgeComponent(GameObject go, WXEntity obj, WXHierarchyContext context, ExportPreset preset) { if (preset.presetKey == "ngui-prefab" || preset.presetKey == "ngui-asset" || preset.presetKey == "ngui-prefabfolder" || preset.presetKey == "ngui-rootScene") { WXBridge.isNGUIPreset = true; } if (preset.presetKey == "ugui-prefab" || preset.presetKey == "ugui-asset" || preset.presetKey == "ugui-prefabfolder" || preset.presetKey == "ugui-rootScene") { WXBridge.isUGUIPreset = true; } var monoBehaviours = go.GetComponents <MonoBehaviour>(); foreach (var behaviour in monoBehaviours) { // 处理用户脚本丢失的情况 if (behaviour == null) { continue; } // user's MonoBehaviour or not? if (!BridgeExport.GetDevTypesSet().Contains(behaviour.GetType())) { continue; } if (WXMonoBehaviourExportHelper.IsInBlackList(behaviour.GetType())) { continue; } obj.components.Add(context.AddComponent(new WXEngineMonoBehaviour(behaviour), behaviour)); } // for a ngui-prefab, we don't need to export any other components if (WXBridge.isNGUIPreset) { return; } if (WXBridge.isUGUIPreset) { return; } /** * 下面的Component为adaptor对齐unity独有的组件 * 需要根据对应类型新建一个对应的Component的class */ // Colliders Collider[] colliders = go.GetComponents <Collider>(); if (colliders != null && colliders.Length > 0) { foreach (var collider in colliders) { if (collider is BoxCollider) { //obj.components.Add(context.AddComponent(new WXBoxCollider((BoxCollider)collider), collider)); obj.components.Add(context.AddComponent(new WXUnityComponent(collider), collider)); } else if (collider is SphereCollider) { //obj.components.Add(context.AddComponent(new WXSphereCollider((SphereCollider)collider), collider)); obj.components.Add(context.AddComponent(new WXUnityComponent(collider), collider)); } else if (collider is CapsuleCollider) { //obj.components.Add(context.AddComponent(new WXCapsuleCollider((CapsuleCollider)collider), collider)); obj.components.Add(context.AddComponent(new WXUnityComponent(collider), collider)); } else if (collider is MeshCollider) { //obj.components.Add(context.AddComponent(new WXMeshCollider((MeshCollider)collider), collider)); obj.components.Add(context.AddComponent(new WXUnityComponent(collider), collider)); } } } // AudioSource(s) AudioSource[] audioSources = go.GetComponents <AudioSource>(); if (audioSources != null && audioSources.Length > 0) { foreach (var audioSource in audioSources) { obj.components.Add(context.AddComponent(new WXEngineAudioSource(audioSource), audioSource)); } } Rigidbody rigidbody = go.GetComponent <Rigidbody>(); if (rigidbody != null) { //obj.components.Add(context.AddComponent(new WXRigidbody(rigidbody), rigidbody)); obj.components.Add(context.AddComponent(new WXUnityComponent(rigidbody), rigidbody)); } /** * 下面的Component为adaptor对齐unity,但引擎也有类似Component的组件 * 需要在WXUnityComponent中对应做处理,增加ref对象指向对应引擎对象 */ obj.components.Add(context.AddComponent(new WXUnityComponent(go.transform), go.transform)); // Particle System ParticleSystem particle = go.GetComponent <ParticleSystem>(); if (particle != null) { //Debug.Log("addComponentParticleSystem"); obj.components.Add(context.AddComponent(new WXUnityComponent(particle), particle)); } // Animator Animator animator = go.GetComponent <Animator>(); if (animator != null) { obj.components.Add(context.AddComponent(new WXUnityComponent(animator), animator)); } // Animation Animation animation = go.GetComponent <Animation>(); if (animation != null) { obj.components.Add(context.AddComponent(new WXUnityComponent(animation), animation)); } // Renderers Renderer renderer = go.GetComponent <Renderer>(); if (renderer != null) { if (renderer is MeshRenderer) { obj.components.Add(context.AddComponent(new WXUnityComponent((MeshRenderer)renderer), renderer)); // 由于引擎没有MeshFilter组件,这里强制在导出MeshRenderer的时候带上一个MeshFilter obj.components.Add(context.AddComponent(new WXMeshFilter(), renderer)); } else if (renderer is LineRenderer) { obj.components.Add(context.AddComponent(new WXUnityComponent((LineRenderer)renderer), renderer)); } else if (renderer is SkinnedMeshRenderer) { obj.components.Add(context.AddComponent(new WXUnityComponent((SkinnedMeshRenderer)renderer), renderer)); obj.components.Add(context.AddComponent(new WXMeshFilter(), renderer)); } } // Camera Camera camera = go.GetComponent <Camera>(); if (camera != null) { obj.components.Add(context.AddComponent(new WXUnityComponent(camera), camera)); } // Light Light light = go.GetComponent <Light>(); if (light != null) { obj.components.Add(context.AddComponent(new WXUnityComponent(light), light)); } }
public override string getTypeName() { var result = collider ? collider.GetType().ToString() : "UnityEngine.BoxCollider"; return(WXMonoBehaviourExportHelper.EscapeNamespace(result)); }