using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.SceneManagement; using LitJson; //LisJson支持vector3等 https://www.cnblogs.com/msxh/p/12541159.html using System.Runtime.InteropServices; #if UNITY_UI_FAIRYGUI using FairyGUI; #endif namespace WeChatWASM { public class UnityDumper : MonoBehaviour { public class CObjectData { public string url; public string format; public string name; public long cpuMemory; } #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void DumpUICallback(string str); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void GetScreenSizeCallback(int width, int height); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void GetUnityVersionCallback(string version); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void GetUnityFrameRateCallback(int rate); #if UNITY_WEBGL [DllImport("__Internal")] #endif private static extern void GetUnityCacheDetailCallback(string str); public static int index = 1; private static string sceneName = ""; private static Camera cam; private static long allMemory = 0; #if UNITY_UI_UGUI public static string platform = "ugui"; #elif UNITY_UI_NGUI public static string platform = "ngui"; #elif UNITY_UI_FAIRYGUI public static string platform = "fairygui"; #else public static string platform = "undefined"; #endif public void getNowScenesComponents() { if(platform == "undefined") { DumpUICallback(""); } Debug.Log("getNowScenesComponents"); cam = Camera.main; List forest = new List(); UnityEngine.SceneManagement.Scene scene = SceneManager.GetActiveScene(); sceneName = scene.name; if(sceneName.Length == 0) { sceneName = "default"; } index = 1; foreach (GameObject rootObj in scene.GetRootGameObjects()) { if (rootObj.transform.parent == null) { // Debug.Log(rootObj.name + ":" + rootObj.GetType().Name); Node node = new Node(rootObj, "0"); forest.Add(node); } } Node rootNode = new Node("root", forest); // WriteFileByLine (Application.persistentDataPath,String.Format("tree_{0}.txt", 0), JsonMapper.ToJson(rootNode)); // var countI = 0; // Debug.Log(Application.persistentDataPath); // foreach(string str in forest) { // WriteFileByLine (Application.persistentDataPath,String.Format("tree_{0}.txt", countI), str); // countI++; // } // Debug.Log(JsonMapper.ToJson(rootNode)); DumpUICallback(JsonMapper.ToJson(rootNode)); } private static Canvas GetRootCanvas(GameObject gameObject){ Canvas canvas = gameObject.GetComponentInParent(); // 如果unity版本小于unity5.5,就用递归的方式取吧,没法直接取rootCanvas // 如果有用到4.6以下版本的话就自己手动在这里添加条件吧 #if UNITY_4_6 || UNITY_4_7 || UNITY_4_8 || UNITY_4_9 || UNITY_5_0 || UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4 if (canvas && canvas.isRootCanvas) { return canvas; } else { if (gameObject.transform.parent.gameObject != null) { return GetRootCanvas(gameObject.transform.parent.gameObject); } else { return null; } } #else if (canvas && canvas.isRootCanvas){ return canvas; } else if (canvas) { return canvas.rootCanvas; } else { return null; } #endif } private void WriteFileByLine(string file_path,string file_name,string str_info) { StreamWriter sw; if(!File.Exists(file_path+"//"+file_name)) { sw=File.CreateText(file_path+"//"+file_name);//创建一个用于写入 UTF-8 编码的文本 Debug.Log("文件创建成功!"); } else { sw=File.AppendText(file_path+"//"+file_name);//打开现有 UTF-8 编码文本文件以进行读取 } sw.WriteLine(str_info);//以行为单位写入字符串 sw.Close (); sw.Dispose ();//文件流释放 } private class Node { public string id; public string name; public Feature feature; public Rect originalPosition; public int childrenCount; public Rect position; public int z; public string zPath = ""; public List children = new List(); public bool show = false; public bool enable = false; public bool useless = true; public bool visible = false; public List components = new List(); //根节点的构造函数 public Node(string name, List children){ this.name = name; this.zPath = "0"; this.children = children; this.childrenCount = children.Count; this.z = 0; this.id = "root"; } #if UNITY_UI_FAIRYGUI public Dictionary> actionInfo = new Dictionary>(); #else public string action_info = "#todo"; #endif #if UNITY_UI_FAIRYGUI public Node(GObject obj, string zPath) { List GComponentList = new List{"GComponent", "GButton", "GComboBox", "GLabel", "GList", "GProgressBar", "GRoot", "GScrollBar", "GSlider", "Window"}; this.feature = new Feature(obj); this.z = index; index++; this.zPath = zPath.Length > 0 ? zPath + "_" + this.z.ToString(): this.z.ToString(); this.id = sceneName + ":" + feature.className + ":" + feature.tag + ":" + obj.id; name = obj.name; childrenCount = 0; enable = obj.enabled; visible = obj.visible; originalPosition = feature.rect; actionInfo = new Dictionary>(); var eventList = new Dictionary(); bool hasEvent = false; if(!obj.onClick.isEmpty) { eventList.Add("onClick", true); hasEvent = true; } else { eventList.Add("onClick", false); } if(!obj.onDragMove.isEmpty){ eventList.Add("onDragMove", true); hasEvent = true; } else { eventList.Add("onDragMove", false); } if(!obj.onDragEnd.isEmpty){ eventList.Add("onDragEnd", true); hasEvent = true; } else { eventList.Add("onDragEnd", false); } if(!obj.onDragStart.isEmpty){ eventList.Add("onDragStart", true); hasEvent = true; } else { eventList.Add("onDragStart", false); } if(!obj.onKeyDown.isEmpty){ eventList.Add("onKeyDown", true); hasEvent = true; } else { eventList.Add("onKeyDown", false); } if(!obj.onRightClick.isEmpty){ eventList.Add("onRightClick", true); hasEvent = true; } else { eventList.Add("onRightClick", false); } if(!obj.onTouchBegin.isEmpty){ eventList.Add("onTouchBegin", true); hasEvent = true; } else { eventList.Add("onTouchBegin", false); } if(!obj.onTouchEnd.isEmpty){ eventList.Add("onTouchEnd", true); hasEvent = true; } else { eventList.Add("onTouchEnd", false); } if(!obj.onTouchMove.isEmpty){ eventList.Add("onTouchMove", true); hasEvent = true; } else { eventList.Add("onTouchMove", false); } actionInfo.Add("eventList", eventList); if(hasEvent && visible && enable){ enable = true; }else{ enable = false; } useless = !enable; //fairygui坐标系以左上角为(0,0),往右为x正方向,往下位y正方向 position = new Rect(feature.rect.x, feature.rect.y, feature.rect.width, feature.rect.height); //只有特定几类的ui才有children,其他的没有 if(GComponentList.Contains(feature.originalClassName)) { GComponent tmpObj = (GComponent)obj; childrenCount = tmpObj.numChildren; foreach(GObject child in tmpObj.GetChildren()) { Node childNode = new Node(child, this.zPath); children.Add(childNode); } } } #endif public Node(GameObject obj, string zPath) { if(platform == "undefined") { Debug.Log("unsupport undefined platform"); } this.feature = new Feature(obj); this.z = index; index++; this.zPath = zPath.Length > 0 ? zPath + "_" + this.z.ToString(): this.z.ToString(); id = sceneName + ":" + feature.className + ":" + feature.tag; name = obj.name; childrenCount = obj.transform.childCount; // unity坐标系转换为手机屏幕的坐标系 position = new Rect(feature.rect.x, UnityEngine.Screen.height - feature.rect.y, feature.rect.width, feature.rect.height); show = (position.width > 0 && position.height > 0); #if UNITY_UI_UGUI Renderer render = obj.GetComponent(); if (render != null) { visible = render.enabled; } else { visible = false; } Button button = obj.GetComponent