static public void load(GameObject goRootp, SceneRootData sceneRootData) { GraphRoot GraphRoot = goRootp.AddComponent <GraphRoot>(); goRootp.transform.position = new Vector3(0, 0, 0); GraphRoot.initBySceneRootData(sceneRootData); }
static public void create(GameObject goRootp, int xNum, int zNum, LayerMask layer, float tilex = 25f, float tilez = 25f) { GraphRoot GraphRoot = goRootp.AddComponent <GraphRoot>(); goRootp.transform.position = new Vector3(0, 0, 0); GraphRoot.layer = layer; GraphRoot.init(xNum, zNum, tilex, tilez); }
public static void Save() { if (!EditorApplication.isPlaying) { EditorUtility.DisplayDialog("error", "场景必须在运行状态 ", "ok"); return; } GraphRoot.save(); }
public static void Load() { //获取当前打开场景(path) string currSceneName = EditorApplication.currentScene; //获取当前打开场景名称 currSceneName = currSceneName.Substring(currSceneName.LastIndexOf("/") + 1); currSceneName = currSceneName.Replace(".unity", ""); SceneRootData sceneRootData = (SceneRootData)AssetDatabase.LoadAssetAtPath(string.Format("Assets/Resources/SceneRectData/SceneRectData_{0}.asset", currSceneName), typeof(SceneRootData)); GraphRoot.load(new GameObject("GraphLoadRoot"), sceneRootData); }
public static void SetPrefab() { /* * if (EditorApplication.isPlaying) * { * EditorUtility.DisplayDialog("error", "场景不能在运行状态 ", "ok"); * return; * }*/ GraphSetting graphSetting = FindObjectOfType <GraphSetting>(); if (graphSetting == null) { EditorUtility.DisplayDialog("error", "找不到 GraphSetting 组件", "ok"); return; } GraphRoot.setPrefabByDataAsset(graphSetting.sceneRectDataAssetPath, graphSetting.prefabSearchPathArr); }
public static void Build() { if (!EditorApplication.isPlaying) { //EditorUtility.DisplayDialog("error", "场景必须在运行状态 ", "ok"); //return; } GraphSetting graphSetting = FindObjectOfType <GraphSetting>(); if (graphSetting == null) { EditorUtility.DisplayDialog("error", "找不到 GraphSetting 组件", "ok"); return; } //Tree8.isDebug = false; //GraphRoot.create(new GameObject("GraphRoot"), 4, 4, LayerMask.NameToLayer("Tree")); GraphRoot.create(new GameObject("GraphRoot"), graphSetting.xNum, graphSetting.zNum, graphSetting.layerMask, graphSetting.tilex, graphSetting.tilez); }
//加载 Scene IEnumerator LoadGameSceneAsync(string assetBundleName) { WWW download = new WWW(AppContentPath() + assetBundleName); yield return(download); assetBundle = download.assetBundle; string[] assetPaths = assetBundle.GetAllAssetNames(); SceneRootData sceneRootData = assetBundle.LoadAsset <SceneRootData>(assetPaths[0]); string goName = assetBundlesPath; if (assetBundlesPath.LastIndexOf("/") > -1) { goName = assetBundlesPath.Substring(assetBundlesPath.LastIndexOf("/") + 1); } GraphRoot.load(new GameObject("GraphRoot_" + goName), sceneRootData); //perfabAssetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + "map001.prefab"); }
void OnGUI() { graphSetting = FindObjectOfType <GraphSetting>(); if (graphSetting == null) { graphSetting = new GameObject("GraphSetting").AddComponent <GraphSetting>(); } GUILayout.BeginVertical(); EditorGUILayout.Space(); EditorGUILayout.Space(); GUILayout.BeginHorizontal(); //获取当前打开场景(path) string currSceneName = EditorApplication.currentScene; //获取当前打开场景名称 currSceneName = currSceneName.Substring(currSceneName.LastIndexOf("/") + 1); currSceneName = currSceneName.Replace(".unity", ""); GUILayout.Label("SceneName"); GUILayout.Label(currSceneName); GUILayout.EndHorizontal(); EditorGUILayout.Space(); //EditorGUILayout.Space(); //EditorGUILayout.Space(); GUILayout.BeginArea(new Rect(10, 90, 268, 480)); GUILayout.BeginVertical(); GUILayout.BeginHorizontal(); GUILayout.Label("xNum"); string xNumStr = (GUILayout.TextField(graphSetting.xNum.ToString())); int.TryParse(xNumStr, out graphSetting.xNum); GUILayout.Label("zNum"); string zNumStr = (GUILayout.TextField(graphSetting.zNum.ToString())); int.TryParse(zNumStr, out graphSetting.zNum); GUILayout.EndHorizontal(); GUILayout.BeginHorizontal(); GUILayout.Label("tilex "); string tilexStr = GUILayout.TextField(graphSetting.tilex.ToString()); int.TryParse(tilexStr, out graphSetting.tilex); //int.Parse(GUILayout.TextField("20")); GUILayout.Label("tilez "); string tilezStr = (GUILayout.TextField(graphSetting.tilez.ToString())); int.TryParse(tilezStr, out graphSetting.tilez); GUILayout.EndHorizontal(); GUILayout.EndVertical(); GUILayout.EndArea(); GUILayout.BeginHorizontal(); //GUILayout.Label("Layer"); //layerName = GUILayout.TextField(layerName); string[] options = new string[32];//{"CanJump", "CanShoot", "CanSwim"}; for (int i = 0; i < 32; i++) { options[i] = LayerMask.LayerToName(i); } graphSetting.layerMask = EditorGUILayout.MaskField(" 采集 layer ", graphSetting.layerMask, options); //EditorGUIUtility. //EditorGUILayout.MaskField(layer, if (GUILayout.Button("1 Build", GUILayout.Width(80))) { if (!EditorApplication.isPlaying) { EditorUtility.DisplayDialog("error", "场景必须在运行状态 ", "ok"); return; } //Tree8.isDebug = false; GraphRoot.create(new GameObject("GraphRoot"), graphSetting.xNum, graphSetting.zNum, graphSetting.layerMask, graphSetting.tilex, graphSetting.tilez); } GUILayout.EndHorizontal(); EditorGUILayout.Space(); EditorGUILayout.Space(); GUILayout.BeginHorizontal(); GUILayout.BeginArea(new Rect(10, 90 + 100, 568, 280)); GUILayout.Label("2.保存场景捕获数据SceneRectData到Resources"); if (GUILayout.Button("2 Save Assets ", GUILayout.Width(218))) { if (!EditorApplication.isPlaying) { EditorUtility.DisplayDialog("error", "场景必须在运行状态 ", "ok"); return; } GraphRoot.save(); } GUILayout.EndArea(); GUILayout.BeginArea(new Rect(10, 90 + 100 + 48, 568, 280)); GUILayout.Label("3.设置SceneRectData中的Prefab路径 "); if (GUILayout.Button("3 Set SceneRectData PrefabPath ", GUILayout.Width(218))) { if (EditorApplication.isPlaying) { EditorUtility.DisplayDialog("error", "场景不能在运行状态 ", "ok"); return; } //GraphRoot.setPrefab(); } GUILayout.EndArea(); GUILayout.BeginArea(new Rect(10, 90 + 100 + 48 * 2, 568, 280)); GUILayout.Label("4.打包 SceneRectData 到 AssetBundles "); if (GUILayout.Button("4 AssetBundles SceneRectData ", GUILayout.Width(218))) { GraphEditor.AssetBundles(); } GUILayout.EndArea(); GUILayout.EndHorizontal(); /* * GUILayout.BeginHorizontal(); * //获取当前打开场景(path) * string currSceneName = EditorApplication.currentScene; * //获取当前打开场景名称 * currSceneName = currSceneName.Substring(currSceneName.LastIndexOf("/") + 1); * currSceneName = currSceneName.Replace(".unity", ""); * GUILayout.Label("SceneName"); * GUILayout.Label(currSceneName); * * if (GUILayout.Button("Export", GUILayout.Width(80))) * { * ExportSceneLightMap.Export(); * } * GUILayout.EndHorizontal(); * * // Lightmap 列表 * LightmapData[] currSceneLightMaps = LightmapSettings.lightmaps; * int length = currSceneLightMaps.Length; * * foreach (LightmapData lightmapData in currSceneLightMaps) * { * GUILayout.BeginHorizontal(); * * //GUILayout.Label(lightmapData.lightmapLight, typeof(Texture2D)) as Texture2D); * EditorGUILayout.ObjectField(lightmapData.lightmapLight, typeof(Texture2D), true, GUILayout.Width(220)); * * GUILayout.EndHorizontal(); * } * //end Lightmap 列表 * //输出 Lightmap 列表 * for (int i = 0; i < length; i++) * { * GUILayout.BeginHorizontal(); * //获取 lm Texture2D * Texture2D currLightMapColor = currSceneLightMaps[i].lightmapColor; * Texture2D currLightMapDir = currSceneLightMaps[i].lightmapDir; * * //格式化名称 * string currLightMapColorName = string.Format("Assets/Resources/Lightmap_{1}/LightmapColor_{0}_{1}", i, currSceneName); * string currLightMapDirName = string.Format("Assets/Resources/Lightmap_{1}/LightmapDir_{0}_{1}", i, currSceneName); * * GUILayout.Label("output >>>>>>> index = " + i); * * if (File.Exists(Application.dataPath + currLightMapDirName.Replace("Assets", "") + ".exr")) * { * Texture2D currAssetLightMapDir = (Texture2D)AssetDatabase.LoadAssetAtPath(currLightMapDirName, typeof(Texture2D)); * EditorGUILayout.ObjectField(currAssetLightMapDir, typeof(Texture2D), true, GUILayout.Width(160)); * } * if (File.Exists(Application.dataPath + currLightMapColorName.Replace("Assets", "") + ".exr")) * { * Texture2D currAssetLightMapColor = (Texture2D)AssetDatabase.LoadAssetAtPath(currLightMapColorName + ".exr", typeof(Texture2D)); * EditorGUILayout.ObjectField(currAssetLightMapColor, typeof(Texture2D), true, GUILayout.Width(160)); * } * GUILayout.EndHorizontal(); * }*/ //end 输出 Lightmap 列表 GUILayout.EndVertical(); }
/// <summary> /// 重新绑定Prefab /// </summary> /* * static public void setPrefab() * { #if UNITY_EDITOR * //获取当前打开场景(path) * string currSceneName = EditorApplication.currentScene; * //获取当前打开场景名称 * currSceneName = currSceneName.Substring(currSceneName.LastIndexOf("/") + 1); * currSceneName = currSceneName.Replace(".unity", ""); * * string sceneRectDataAssetPath = string.Format("Assets/Resources/SceneRectData/SceneRectData_{0}.asset", currSceneName); * * string[] files = System.IO.Directory.GetFiles(Application.dataPath + "/Resources/SceneRectData/"); * * for (int i = 0; i < files.Length; i++) * { * string file = files[i]; * if (file.EndsWith(".meta")) * { * continue; * } * if (file.IndexOf(currSceneName) < 0) * { * continue; * } * * file = file.Substring(file.LastIndexOf("/Assets/Resources/") + 1); * sceneRectDataAssetPath = file; * * SceneRootData sceneRootData = (SceneRootData)AssetDatabase.LoadAssetAtPath(sceneRectDataAssetPath, * typeof(SceneRootData)); * * Dictionary<string, UnityEngine.Object> prefabNameDic = new Dictionary<string, UnityEngine.Object>(); * * foreach (SceneRectData sceneRectData in sceneRootData.sceneRectDataArr) * { * foreach (SceneObjectData sceneObjectData in sceneRectData.sceneObjectDataArr) * { * GameObject gameObject = GameObject.Find(sceneObjectData.gameObjectPath); * if (gameObject == null) * { * EditorUtility.DisplayDialog("error", "无效路径=>" + sceneObjectData.gameObjectPath, "ok"); * return; * } * if (PrefabUtility.GetPrefabType(gameObject) == PrefabType.PrefabInstance) * { * UnityEngine.Object parentObject = EditorUtility.GetPrefabParent(gameObject); * string assetPath = AssetDatabase.GetAssetPath(parentObject); * //直接设置数据会更新到 Assets 文件 * sceneObjectData.prefabName = assetPath; * * if (!prefabNameDic.ContainsKey(assetPath)) * { * //GameObject prefabGo = null; * //prefabNameDic.TryGetValue(assetPath, out prefabGo); * prefabNameDic.Add(assetPath, parentObject); * } * Debug.LogWarning(gameObject + " = " + assetPath + "," + gameObject.GetInstanceID()); * } * else * { * * } * } * } * * sceneRootData.prefabPathArr = new string[prefabNameDic.Keys.Count]; * prefabNameDic.Keys.CopyTo(sceneRootData.prefabPathArr, 0); * * sceneRootData.prefabAssetBundleNameArr = new string[prefabNameDic.Keys.Count]; * //设置依赖 prefab 打包 * for (int j = 0; j < sceneRootData.prefabPathArr.Length; j++) * { * //设置打包路径 * AssetImporter assetImporter = AssetImporter.GetAtPath(sceneRootData.prefabPathArr[j]); * assetImporter.assetBundleName = currSceneName + ".prefab"; * * sceneRootData.prefabAssetBundleNameArr[j] = assetImporter.assetBundleName; * * //GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(sceneRootData.prefabPathArr[i], typeof(GameObject)); * //GameObject prefabGameObject = GameObject.Instantiate<GameObject>(prefab); * //prefabGameObject.transform.parent = prefabRoot.transform; * //prefabNameDic.Add(sceneRootData.prefabPathArr[i], prefabGameObject); * * } * } * //不需要删除后重新创建!!! LoadAssetAtPath 加载数据后【赋值】【直接更新】到 asset 文件!! * //AssetDatabase.DeleteAsset(sceneRectDataAssetPath); * //AssetDatabase.CreateAsset(sceneRootData, string.Format("Assets/Resources/SceneRectData/SceneRectData_{0}.asset", currSceneName)); * } #endif * * } */ /// <summary> /// 保存场景数据到 序列化对象 SceneRootData 中 /// 由工具链调用 /// </summary> static public void save() { GraphRoot graphRoot = GameObject.FindObjectOfType <GraphRoot>(); if (graphRoot == null) { #if UNITY_EDITOR EditorUtility.DisplayDialog("error", " 场景没有 Root 请先执行 build 操作 ", "ok!"); #endif return; } SceneRootData sceneRootData = new SceneRootData(); int xNum = graphRoot.x_Num; int zNum = graphRoot.z_Num; sceneRootData.xNum = xNum; sceneRootData.zNum = zNum; sceneRootData.tilex = graphRoot.tile_w; sceneRootData.tilez = graphRoot.tile_h; sceneRootData.sceneRectDataArr = new SceneRectData[graphRoot.transform.GetChildCount()]; int index = 0; for (int x = 0; x < xNum; x++) { for (int z = 0; z < zNum; z++) { Graph t = graphRoot.arr[x, z]; SceneRectData sceneRectData = new SceneRectData(); sceneRectData.xIndex = x; sceneRectData.zIndex = z; sceneRectData.sceneObjectDataArr = t.list.ToArray(); sceneRootData.sceneRectDataArr[index++] = sceneRectData; } } sceneRootData.prefabPathArr = new string[graphRoot.prefabNameDic.Keys.Count]; graphRoot.prefabNameDic.Keys.CopyTo(sceneRootData.prefabPathArr, 0); #if UNITY_EDITOR //获取当前打开场景(path) string currSceneName = EditorApplication.currentScene; //获取当前打开场景名称 currSceneName = currSceneName.Substring(currSceneName.LastIndexOf("/") + 1); currSceneName = currSceneName.Replace(".unity", ""); //string resourcesPath = Application.dataPath + string.Format("/Resources/SceneRectData_{0}", currSceneName); string resourcesPath = Application.dataPath + "/Resources/SceneRectData"; if (!Directory.Exists(resourcesPath)) { System.IO.Directory.CreateDirectory(resourcesPath); } //AssetDatabase.CreateAsset(sceneRootData, string.Format("Assets/Resources/SceneRectData/SceneRectData_{0}.asset", currSceneName)); string colliderLayerStr = Convert.ToString(graphRoot.layer.value, 2); string layerStr = string.Empty; char[] charArr = colliderLayerStr.ToCharArray(); Array.Reverse(charArr); for (int i = 0; i < colliderLayerStr.Length; i++) { char c = charArr[i]; int layerInt = int.Parse(c.ToString()); if (layerInt == 1) { layerStr += LayerMask.LayerToName(i) + "_"; } } AssetDatabase.CreateAsset(sceneRootData, string.Format("Assets/Resources/SceneRectData/SceneRectData_{0}_{1}.asset", currSceneName, layerStr)); //AssetDatabase.CreateAsset(sceneRootData, string.Format("Assets/Resources/SceneRectData/SceneRectData_{0}_{1}.asset" , currSceneName, colliderLayerStr)); #endif }