private void OnEnable() { binding = target as LuaBinding; var luaPathProp = serializedObject.FindProperty("LuaFile"); if (string.IsNullOrEmpty(luaPathProp.stringValue) || !File.Exists(Application.dataPath + "/Resources/Lua/" + luaPathProp.stringValue + ".lua")) { return; } descriptor = LuaClassEditorFactory.GetDescriptorWithFilePath(luaPathProp.stringValue); }
public override void OnInspectorGUI() { if (binding.LuaData == null) { binding.LuaData = new LuaBindingDataBase[0]; } var luaPathProp = serializedObject.FindProperty("LuaFile"); if (string.IsNullOrEmpty(luaPathProp.stringValue)) { EditorGUILayout.PropertyField(luaPathProp); EditorGUILayout.HelpBox("需要设置Lua文件!", MessageType.Error); serializedObject.ApplyModifiedProperties(); return; } if (descriptor == null) { EditorGUILayout.PropertyField(luaPathProp); EditorGUILayout.HelpBox("需要设置Lua文件!", MessageType.Error); descriptor = LuaClassEditorFactory.GetDescriptorWithFilePath(luaPathProp.stringValue); serializedObject.ApplyModifiedProperties(); return; } isUsedBinding.Clear(); foreach (var field in descriptor.Fields.Where(field => !field.FieldName.StartsWith("_"))) { if (field.FieldType.Contains("[]")) { CheckBinding <LuaBindingUOArrayData>(field); } else if (field.FieldType.StartsWith("CS.")) { var typeName = field.FieldType.Substring(3); var type = String2TypeCache.GetType(typeName); if (type == null) { EditorGUILayout.HelpBox($"Can not find type : {typeName}", MessageType.Error); } else { if (field.FieldType == "CS.Extend.Asset.AssetReference") { var match = CheckBinding <LuaBindingAssetReferenceData>(field); if (!string.IsNullOrEmpty(field.Comment) && field.Comment.StartsWith("CS.")) { match.AssetType = String2TypeCache.GetType(field.Comment.Substring(3)); } } else { CheckBinding <LuaBindingUOData>(field); } } } else { var index = Array.IndexOf(basicTypes, field.FieldType); if (index >= 0) { var typ = basicBindingTypes[index]; CheckBinding(field, typ); } else { CheckBinding <LuaBindingUOData>(field); } } } for (var i = 0; i < binding.LuaData.Length;) { var bindingData = binding.LuaData[i]; if (isUsedBinding.Contains(bindingData)) { i++; } else { ArrayUtility.RemoveAt(ref binding.LuaData, i); } } serializedObject.UpdateIfRequiredOrScript(); var luaDataProp = serializedObject.FindProperty("LuaData"); for (var i = 0; i < binding.LuaData.Length; i++) { var arrElem = binding.LuaData[i]; var elementProp = luaDataProp.GetArrayElementAtIndex(i); var dataProp = elementProp.FindPropertyRelative("Data"); arrElem.OnPropertyDrawer(dataProp); } if (Application.isPlaying) { foreach (var methodName in descriptor.DebugMethods) { if (GUILayout.Button(methodName) && binding.LuaInstance != null) { var func = binding.LuaInstance.Get <Action <LuaTable> >(methodName); func(binding.LuaInstance); } } } serializedObject.ApplyModifiedProperties(); base.OnInspectorGUI(); GUILayout.BeginHorizontal(); if (GUILayout.Button("重新加载Lua文件")) { if (descriptor == null) { return; } descriptor = LuaClassEditorFactory.ReloadDescriptor(descriptor.ClassName.Replace('.', '/')); } if (GUILayout.Button("在编辑器中打开")) { string idePath = EditorPrefs.GetString("kScriptsDefaultApp_h2657262712"); var luaPath = $"{Application.dataPath}/../Lua/{luaPathProp.stringValue.Replace('.', '/')}.lua"; if (idePath.Contains("Rider")) { Process.Start($"\"{idePath}\"", $"--line 0 {luaPath}"); } else if (idePath.Contains("Code")) { string binPath = idePath.Replace("Code.exe", "bin/code"); Process.Start($"\"{binPath}\"", $"-r -g \"{luaPath}:0\""); } } GUILayout.EndHorizontal(); }