List <DescriptorValue> ParseLuaTarget_GetValues(RealStatePtr L, LuaTable argsTable, List <string> keylist) { List <DescriptorValue> valuelist = new List <DescriptorValue>(); foreach (var key in keylist) { Descriptor[] descriptors = null; object value = null; LuaTypes valueType = LuaTypes.LUA_TNIL; argsTable.Get <string, object>(key, out value, out valueType); DescriptorValue dv = new DescriptorValue(); dv.v = ParseLuaTarget_LuaValue(L, value, valueType, out descriptors); dv.desc = descriptors; valuelist.Add(dv); } return(valuelist); }
public override void OnInspectorGUI() { LuaTarget currTarget = target as LuaTarget; if (EditorApplication.isPlaying) { EditorGUI.BeginDisabledGroup(true); } EditorGUILayout.Space(); EditorGUILayout.BeginVertical("GroupBox"); EditorGUILayout.PropertyField(serializedObject.FindProperty("luaFilename")); EditorGUILayout.BeginHorizontal(); string targetPath = currTarget.luaFilename == null ? string.Empty : LuaEnv.LuaRelativeDir + "/" + currTarget.luaFilename; string targetLuaPath = targetPath.EndsWith(".lua") ? targetPath : targetPath + ".lua"; if (targetLuaPath.StartsWith(LuaEnv.LuaRelativeDir) && targetLuaPath.Length > LuaEnv.LuaRelativeDir.Length + 1) { targetLuaPath = targetLuaPath.Substring(LuaEnv.LuaRelativeDir.Length + 1); } int prevIndex = System.Array.FindIndex(fileOptions, luaFile => luaFile == targetLuaPath); int currIndex = EditorGUILayout.Popup(prevIndex, fileOptions); if (prevIndex != currIndex) { if ((uint)currIndex < fileOptions.Length) { currTarget.luaFilename = fileOptions[currIndex]; } GUI.changed = true; } string apath = Path.Combine("Assets", targetPath); if (!apath.EndsWith(".lua")) { apath += ".lua"; } var prevAsset = AssetDatabase.LoadAssetAtPath <UnityEngine.Object>(apath); var currAsset = EditorGUILayout.ObjectField(prevAsset, typeof(UnityEngine.Object), false); if (currAsset != prevAsset) { currTarget.luaFilename = currAsset != null?AssetDatabase.GetAssetPath(currAsset).Substring(("Assets/" + LuaEnv.LuaRelativeDir + "/").Length) : string.Empty; targetPath = currTarget.luaFilename == null ? string.Empty : LuaEnv.LuaRelativeDir + "/" + currTarget.luaFilename; GUI.changed = true; } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); if (EditorApplication.isPlaying) { EditorGUI.EndDisabledGroup(); OnInspectorGUI_Playing(); return; } string path = Path.Combine(Application.dataPath, targetPath); if (!path.EndsWith(".lua")) { path += ".lua"; } if (!File.Exists(path)) { serializedObject.ApplyModifiedProperties(); if (lastPath != "" && PrefabUtility.GetPrefabType(serializedObject.targetObject) == PrefabType.Prefab) { Debug.Log("Auto save in project view 1"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } lastPath = ""; lastKeyList = null; return; } List <string> keylist = new List <string>(); List <DescriptorValue> valuelist = new List <DescriptorValue>(); var prevValues = currTarget.savedValues; var currValues = new Dictionary <string, object>(); if (lastTarget == currTarget && lastKeyList != null && lastPath != path && lastModifyTime != File.GetLastWriteTime(path)) { lastPath = path; keylist = lastKeyList; valuelist = lastValueList; } else { int originTop = 0; try { var content = File.ReadAllText(path); LuaTable argsTable = null; keylist = ParseLuaBehaviour_Begin(Utils.LuaL, path, ref originTop, ref argsTable); if (keylist == null) { ParseLuaTarget_End(Utils.LuaL, originTop); return; } var argsFieldIndex = GetFieldSourceIndex(content, LuaTarget.ArgsName); var argsFieldContent = content.Substring(argsFieldIndex.Key, argsFieldIndex.Value - argsFieldIndex.Key - 1); var orderResult = (argsFieldIndex.Key == -1 || argsFieldIndex.Value == -1) ? null : GetOrderedKey(keylist, argsFieldContent); string region = "--region"; string endRegion = "--endregion"; if (orderResult != null) { keylist = keylist.OrderBy(key => { int index; orderResult.TryGetValue(key, out index); return(index); }).ToList(); List <int> lastRegionIndexList = null; List <string> regionNameList = null; int i = 0; int len = argsFieldContent.Length; while (i < len) { int start = argsFieldContent.IndexOf(region, i); if (start < 0) { break; } int regionNameEndIndex = argsFieldContent.IndexOf('\n', start); if (regionNameEndIndex < start + region.Length + 1) { break; } int end = argsFieldContent.IndexOf(endRegion, start); if (end < 0) { break; } if (lastRegionIndexList == null) { lastRegionIndexList = new List <int>(); regionNameList = new List <string>(); } lastRegionIndexList.Add(start); lastRegionIndexList.Add(end); regionNameList.Add(argsFieldContent.Substring(start + region.Length + 1, regionNameEndIndex - start - region.Length - 1)); i = end + endRegion.Length; } if (lastRegionIndexList != null) { var keyIndexList = keylist.ConvertAll(key => { int index; orderResult.TryGetValue(key, out index); return(index); }); int currKeyIndexListIndex = 0; lastRegionList = new List <Region>(); for (int j = 0; j < lastRegionIndexList.Count; j += 2) { int start = lastRegionIndexList[j]; int end = lastRegionIndexList[j + 1]; int curr = keyIndexList[currKeyIndexListIndex]; while (curr < start) { currKeyIndexListIndex++; if (currKeyIndexListIndex >= keyIndexList.Count) { curr = -1; break; } else { curr = keyIndexList[currKeyIndexListIndex]; } } if (curr == -1) { break; } int startKeyListIndex = currKeyIndexListIndex; while (curr < end) { currKeyIndexListIndex++; if (currKeyIndexListIndex >= keyIndexList.Count) { curr = -1; break; } else { curr = keyIndexList[currKeyIndexListIndex]; } } lastRegionList.Add(new Region() { start = startKeyListIndex, end = currKeyIndexListIndex, name = regionNameList[j >> 1], fadeValue = 1f, }); if (curr == -1) { break; } } } } else { keylist.Sort(); } valuelist = ParseLuaTarget_GetValues(Utils.LuaL, argsTable, keylist); ParseLuaTarget_End(Utils.LuaL, originTop); lastKeyList = keylist; lastValueList = valuelist; lastTarget = currTarget; lastPath = path; lastModifyTime = File.GetLastWriteTime(path); regionFoldout = new Dictionary <string, bool>(); } catch (System.Exception e) { ParseLuaTarget_End(Utils.LuaL, originTop); Debug.LogError(e.Message); } } int currRegionIndex = 0; int regionNextIndex = (lastRegionList != null && lastRegionList.Count > currRegionIndex) ? lastRegionList[currRegionIndex].start : -1; bool inRegion = false; bool regionShow = true; for (int i = 0; i < keylist.Count; i++) { var key = keylist[i]; DescriptorValue value = valuelist[i]; object pv; prevValues.TryGetValue(key, out pv); if (pv != null) { if (value.v is System.Type) { if (!(pv is System.Type) && (pv.GetType() == value.v || pv.GetType().IsSubclassOf(value.v as System.Type))) { value.v = pv; } } else { if (pv.GetType() == value.v.GetType()) { value.v = pv; } } } if (regionNextIndex != -1 && i >= regionNextIndex && inRegion) { EditorGUILayout.EndFadeGroup(); EditorGUILayout.EndVertical(); currRegionIndex++; regionNextIndex = (lastRegionList != null && lastRegionList.Count > currRegionIndex) ? lastRegionList[currRegionIndex].start : -1; inRegion = false; regionShow = true; } if (regionNextIndex != -1 && i >= regionNextIndex && !inRegion) { EditorGUILayout.BeginVertical("GroupBox"); var region = lastRegionList[currRegionIndex]; regionNextIndex = region.end; inRegion = true; if (!regionFoldout.TryGetValue(region.name, out regionShow)) { regionShow = true; } EditorGUI.indentLevel++; bool currRegionShow = EditorGUILayout.Foldout(regionShow, region.name, RegionStyle); EditorGUI.indentLevel--; regionFoldout[region.name] = currRegionShow; if (currRegionShow != regionShow) { if (region.fadeAnim != null) { region.fadeAnim.valueChanged = null; } region.fadeAnim = new AnimFloat(region.fadeValue) { target = currRegionShow ? 1f : 0f, speed = 2.5f, }; var tRegion = region; var unityEvent = new UnityEvent(); unityEvent.AddListener(() => { Repaint(); if (tRegion.fadeAnim != null) { tRegion.fadeValue = tRegion.fadeAnim.value; } }); region.fadeAnim.valueChanged = unityEvent; } EditorGUILayout.BeginFadeGroup(region.fadeValue); regionShow = region.fadeValue > 0f; } if (regionShow) { currValues[key] = ShowItem(key, value.v, value.desc); } else { var type = value.v as System.Type; if (type != null) { object ret = type.IsArray ? System.Array.CreateInstance(type.GetElementType(), 0) : type.IsValueType ? System.Activator.CreateInstance(type) : null; if (ret != null && !(ret is System.Type)) { GUI.changed = true; } currValues[key] = ret; } else { currValues[key] = value.v; } } } if (inRegion) { EditorGUILayout.EndFadeGroup(); EditorGUILayout.EndVertical(); } if (GUI.changed) { currTarget.savedValues = currValues; serializedObject.ApplyModifiedProperties(); Undo.RecordObject(currTarget, "LuaTarget Value Updated"); EditorUtility.SetDirty(currTarget); if (PrefabUtility.GetPrefabType(serializedObject.targetObject) == PrefabType.Prefab) { Debug.Log("Auto save in project view 2"); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } } }