---
tags: 2021-UnityRL
---
# Unity Python API
> Credit by Chien-Feng
## Documentations
1. 路徑設定(pip)
Inside the ml-agents directory
```
cd gym_unity/
pip3 install -e .
cd ml-agents-envs/
pip3 install -e .
```
2. 實作細節
在實作上,如果要直接跟 editor 互動的話,要把 UnityEnvironment 中的 file_name 設為 None:
```
UnityEnvironment(file_name=None)
```
UnityToGymWrapper 中的 uint8_visual 如果是 True 代表說吃到的影像範圍為 0-255,defualt 的話為 False。另外這邊和原本的 doc 有些出入,原本的 doc 上寫的是:
```
UnityToGymWrapper(unity_env, 0, uint8_visual=True)
```
然而後來看了一下 code 發現這個 0 不知所云,可能是版本問題,總之要拿掉才跑得動:
```
UnityToGymWrapper(unity_env, uint8_visual=True)
```
整體而言用法如:reset、step 這些都與與 gym 相同,有些更詳細的用法可能可以自己去 code 裡面翻翻看。
3. 範例
OpenAI baselines 的安裝:https://github.com/openai/baselines
Sample code with OpenAI baselines
```
import gym
from baselines import deepq
from baselines import logger
from mlagents_envs.environment import UnityEnvironment
from gym_unity.envs import UnityToGymWrapper
def main():
unity_env = UnityEnvironment(file_name=None)
env = UnityToGymWrapper(unity_env, uint8_visual=True)
logger.configure('./logs') # Change to log in a different directory
act = deepq.learn(
env,
"cnn", # conv_only is also a good choice for GridWorld
lr=2.5e-4,
total_timesteps=1000000,
buffer_size=50000,
exploration_fraction=0.05,
exploration_final_eps=0.1,
print_freq=20,
train_freq=5,
learning_starts=20000,
target_network_update_freq=50,
gamma=0.99,
prioritized_replay=False,
checkpoint_freq=1000,
checkpoint_path='./logs', # Change to save model in a different directory
dueling=True
)
print("Saving model to unity_model.pkl")
act.save("unity_model.pkl")
if __name__ == '__main__':
main()
```
執行以上程式碼後會看到 Terminal 印出一行字:Start training by pressing the Play button in the Unity Editor,此時再按下 Editor 上的 Play 鍵就開始訓練了。
## Reference
1. UnityEnvironment: https://github.com/Unity-Technologies/ml-agents/blob/main/docs/Python-API.md
2. UnityToGymWrapper: https://github.com/Unity-Technologies/ml-agents/blob/main/gym-unity/README.md