本文为B站系列教学视频《使用 UnrealEnginPython 插件进行UE4中的Python开发》——《Day 03 使用和风天气打印天气信息》的学习笔记,UP主为腾讯游戏策划、虚幻社区贡献者 世欺子。
本节课将使用 UnrealEnginePython 脚本以及和风天气 API,在 UE5.0.3 中获取并呈现天气信息。
3.1 创建打印天气信息的 Actor 类
-
在内容浏览器中 “
Content
” 目录下新建文件夹,命名为 “GettingWeatherTutorial
”,用来保存后面用到的打印天气信息的 Actor 类。
-
新建一个 Actor 类,命名为 “
BP_GettingWeather
”,用于打印天气信息。
-
双击 “
BP_GettingWeather
”,弹出 “BP_GettingWeather
” 资产编辑窗口,在 “组件” 面板添加 一个Python
组件,设置 “Python Module
” 为 “test_getting_weather_in_ue
”,将 “Python Class
” 设置为 “WeatherComponent
”。
-
创建一个 “字符串”(String)类型的变量,命名为 “
CityName
” ,用于传入城市信息。变量创建后可以看到默认值无法设置,出现提示信息 “请编译蓝图”,解决方法是点击工具栏的 “编译”,即可设置默认值为 “北京”。
-
在 “
CityName
” 变量右侧点击 “眼睛”按钮,会发现 “眼睛” “睁开了”,即 “CityName
” 变量实现公有化,这是为了能够在场景中直接设置此变量的值。
-
在左下方 “我的蓝图” 面板 “事件调度器”选项卡右边点击 “+” 按钮,添加 “
OnWeatherNeedUpdate
” 事件。
3.2 获取和风天气 API KEY
-
访问和风天气官网,注册账户。
-
注册完成后,访问 “控制台-项目管理”,点击 “创建项目” 按钮。
订阅方式选择 “免费订阅”。
-
在项目管理中查看 API KEY。
3.3 编辑获取天气信息事件蓝图程序
-
在内容浏览器中双击 “
~\Content\ThirdPerson\Blueprints
” 下的 “BP_ThirdPersonCharacter
” ,在“事件图表”选项卡下的蓝图编辑器中编辑打印天气信息事件蓝图程序。
-
打开
Python Editor
新建脚本test_getting_weather_in_ue
,参考UP主在视频中和http://zhy.world/?p=102 提供的代码,然后根据和风天气官网文档《API配置》和《GeoAPI - 热门城市查询》进行部分修改。import unreal_engine as ue import urllib.request import gzip class WeatherComponent: '''和风天气开发服务的API均使用Gzip进行了压缩,这将极大的减少网络流量,加快请求。 因此,在开发过程中需要对返回的数据进行解压,不然就会导致数据编码溢出的问题''' def ungzip(self, page): try: data = gzip.decompress(page) except: pass return data '''通过和风天气 Geo API 获取城市/地区代码''' def getting_city_code(self, city_name): url = "https://geoapi.qweather.com/v2/city/lookup?location=" + urllib.parse.quote(city_name) + "&key=这里替换成你的key" city_search_result = eval(self.ungzip(urllib.request.urlopen(url).read()).decode('utf-8')) for city_info in city_search_result["location"]: if city_info["name"] == city_name: return str(city_info["id"]) def begin_play(self): self.uobject.get_owner().bind_event("OnWeatherNeedUpdate", self.getting_weather) # 将打印天气信息函数和事件分发器 OnWeatherNeedUpdate 进行绑定 '''通过和风天气 API 以及 城市/地区代码 获取城市/地区天气信息''' def getting_weather(self): city_name = self.uobject.get_owner().CityName city_code = urllib.parse.quote(self.getting_city_code(city_name)) url = "https://devapi.qweather.com/v7/weather/3d?location=" + city_code + "&key=这里替换成你的key" weather_data = self.ungzip(urllib.request.urlopen(url).read()).decode('utf-8') weather_dict = eval(weather_data) weather_dict_daily = eval(str(weather_dict['daily'][0])) '''打印天气信息,并设置信息停留在视口的时间为 10s''' ue.print_string( 'Pressure: ' + weather_dict_daily['pressure'], 10.0) ue.print_string( 'Precipitation: ' + weather_dict_daily['precip'], 10.0) ue.print_string( 'Humidity: ' + weather_dict_daily['humidity'], 10.0) ue.print_string( 'Wind speed at the night: ' + weather_dict_daily['windSpeedNight'], 10.0) ue.print_string( 'Wind speed at the night: ' + weather_dict_daily['windSpeedNight'], 10.0) ue.print_string( 'Wind scale at the night: ' + weather_dict_daily['windScaleNight'], 10.0) ue.print_string( 'Wind direction at the night: ' + weather_dict_daily['windDirNight'], 10.0) ue.print_string( 'Wind speed in the day: ' + weather_dict_daily['windSpeedDay'], 10.0) ue.print_string( 'Wind scale in the day: ' + weather_dict_daily['windScaleDay'], 10.0) ue.print_string( 'Wind direction in the day: ' + weather_dict_daily['windDirDay'], 10.0) ue.print_string( 'Description of actual weather conditions at the night: ' + weather_dict_daily['textNight'], 10.0) ue.print_string( 'Description of actual weather conditions in the day: ' + weather_dict_daily['textDay'], 10.0) ue.print_string( 'Temperature(celcius): ' + weather_dict_daily['tempMin'] + ' ~ ' + weather_dict_daily['tempMax'], 10.0) ue.print_string( 'Moonphase: ' + weather_dict_daily['moonPhase'], 10.0) ue.print_string( 'Moonset: ' + weather_dict_daily['moonset'], 10.0) ue.print_string( 'Moonrise: ' + weather_dict_daily['moonrise'], 10.0) ue.print_string( 'Sunset: ' + weather_dict_daily["sunset"], 10.0) ue.print_string( 'Sunrise: ' + weather_dict_daily['sunrise'], 10.0) ue.print_string('-----Live Weather-----', 10.0) ue.print_string( 'UTC Time: ' + weather_dict['updateTime'], 10.0) ue.print_string('-----Updated Time-----', 10.0) ue.print_string( 'City / District Code: ' + city_code, 10.0) ue.print_string( 'City: ' + city_name, 10.0) ue.print_string('-----Basic Information-----', 10.0)
-
返回 “
ThirdPesonMap
” 主编辑器,在场景中放置 “BP_GettingWeather
”。
-
点击工具栏上的 “播放”(▶)按钮进行测试,每次按下数字键“1”,都可以看到城市天气信息被打印在视口左侧。