class_docstr: A MultiAgentEnv representing a single Unity3D game instance.
  
  For an example on how to use this Env with a running Unity3D editor
  or with a compiled game, see:
  `rllib/examples/unity3d_env_local.py`
  For an example on how to use it inside a Unity game client, which
  connects to an RLlib Policy server, see:
  `rllib/examples/serving/unity3d_[client|server].py`
  
  Supports all Unity3D (MLAgents) examples, multi- or single-agent and
  gets converted automatically into an ExternalMultiAgentEnv, when used
  inside an RLlib PolicyClient for cloud/distributed training of Unity games.
class_docstr_tok: ["A","MultiAgentEnv","representing","a","single","Unity3D","game","instance",".","For","an","example","on","how","to","use","this","Env","with","a","running","Unity3D","editor","or","with","a","compiled","game",",","see",":","`","rllib\/examples\/unity3d_env_local.py","`","For","an","example","on","how","to","use","it","inside","a","Unity","game","client",",","which","connects","to","an","RLlib","Policy","server",",","see",":","`","rllib\/examples\/serving\/unity3d_","[","client|server","]",".py","`","Supports","all","Unity3D","(","MLAgents",")","examples",",","multi-","or","single-agent","and","gets","converted","automatically","into","an","ExternalMultiAgentEnv",",","when","used","inside","an","RLlib","PolicyClient","for","cloud\/distributed","training","of","Unity","games","."]
docstr: Initializes a Unity3DEnv object.
  
  Args:
      file_name (Optional[str]): Name of the Unity game binary.
          If None, will assume a locally running Unity3D editor
          to be used, instead.
      port (Optional[int]): Port number to connect to Unity environment.
      seed: A random seed value to use for the Unity3D game.
      no_graphics: Whether to run the Unity3D simulator in
          no-graphics mode. Default: False.
      timeout_wait: Time (in seconds) to wait for connection from
          the Unity3D instance.
      episode_horizon: A hard horizon to abide to. After at most
          this many steps (per-agent episode `step()` calls), the
          Unity3D game is reset and will start again (finishing the
          multi-agent episode that the game represents).
          Note: The game itself may contain its own episode length
          limits, which are always obeyed (on top of this value here).
docstr_tok: ["Initializes","a","Unity3DEnv","object",".","Args",":","file_name","(","Optional","[","str","]",")",":","Name","of","the","Unity","game","binary",".","If","None",",","will","assume","a","locally","running","Unity3D","editor","to","be","used",",","instead",".","port","(","Optional","[","int","]",")",":","Port","number","to","connect","to","Unity","environment",".","seed",":","A","random","seed","value","to","use","for","the","Unity3D","game",".","no_graphics",":","Whether","to","run","the","Unity3D","simulator","in","no-graphics","mode",".","Default",":","False",".","timeout_wait",":","Time","(","in","seconds",")","to","wait","for","connection","from","the","Unity3D","instance",".","episode_horizon",":","A","hard","horizon","to","abide","to",".","After","at","most","this","many","steps","(","per-agent","episode","`","step","(",")","`","calls",")",",","the","Unity3D","game","is","reset","and","will","start","again","(","finishing","the","multi-agent","episode","that","the","game","represents",")",".","Note",":","The","game","itself","may","contain","its","own","episode","length","limits",",","which","are","always","obeyed","(","on","top","of","this","value","here",")","."]
code: def __init__(
      self,
      file_name: str = None,
      port: Optional[int] = None,
      seed: int = 0,
      no_graphics: bool = False,
      timeout_wait: int = 300,
      episode_horizon: int = 1000,
  ):
      """Initializes a Unity3DEnv object.
  
      Args:
          file_name (Optional[str]): Name of the Unity game binary.
              If None, will assume a locally running Unity3D editor
              to be used, instead.
          port (Optional[int]): Port number to connect to Unity environment.
          seed: A random seed value to use for the Unity3D game.
          no_graphics: Whether to run the Unity3D simulator in
              no-graphics mode. Default: False.
          timeout_wait: Time (in seconds) to wait for connection from
              the Unity3D instance.
          episode_horizon: A hard horizon to abide to. After at most
              this many steps (per-agent episode `step()` calls), the
              Unity3D game is reset and will start again (finishing the
              multi-agent episode that the game represents).
              Note: The game itself may contain its own episode length
              limits, which are always obeyed (on top of this value here).
      """
      # Skip env checking as the nature of the agent IDs depends on the game
      # running in the connected Unity editor.
      self._skip_env_checking = True
  
      super().__init__()
  
      if file_name is None:
          print(
              "No game binary provided, will use a running Unity editor "
              "instead.\nMake sure you are pressing the Play (|>) button in "
              "your editor to start."
          )
  
      import mlagents_envs
      from mlagents_envs.environment import UnityEnvironment
  
      # Try connecting to the Unity3D game instance. If a port is blocked
      port_ = None
      while True:
          # Sleep for random time to allow for concurrent startup of many
          # environments (num_workers >> 1). Otherwise, would lead to port
          # conflicts sometimes.
          if port_ is not None:
              time.sleep(random.randint(1, 10))
          port_ = port or (
              self._BASE_PORT_ENVIRONMENT
              if file_name
              else self._BASE_PORT_EDITOR
          )
          # cache the worker_id and
          # increase it for the next environment
          worker_id_ = Unity3DEnv._WORKER_ID if file_name else 0
          Unity3DEnv._WORKER_ID += 1
          try:
              self.unity_env = UnityEnvironment(
                  file_name=file_name,
                  worker_id=worker_id_,
                  base_port=port_,
                  seed=seed,
                  no_graphics=no_graphics,
                  timeout_wait=timeout_wait,
              )
              print(
                  "Created UnityEnvironment for port {}".format(
                      port_ + worker_id_
                  )
              )
          except mlagents_envs.exception.UnityWorkerInUseException:
              pass
          else:
              break
  
      # ML-Agents API version.
      self.api_version = self.unity_env.API_VERSION.split(".")
      self.api_version = [int(s) for s in self.api_version]
  
      # Reset entire env every this number of step calls.
      self.episode_horizon = episode_horizon
      # Keep track of how many times we have called `step` so far.
      self.episode_timesteps = 0
  
file_path: ray/rllib/env/wrappers/unity3d_env.py
imports: from gymnasium.spaces import Box, MultiDiscrete, Tuple
  import logging
  import numpy
  import random
  import time
  from typing import Callable, Optional, Tuple
  from ray.rllib.env.multi_agent_env import MultiAgentEnv
  from ray.rllib.policy.policy import PolicySpec
  from ray.rllib.utils.annotations import PublicAPI
  from ray.rllib.utils.typing import MultiAgentDict, PolicyID, AgentID
label_desc: Use image node_id 1 to create a new Unity3DEnv object from inherited base classes: MultiAgentEnv with example: obj = Unity3DEnv(file_name, port, seed, no_graphics, timeout_wait, episode_horizon)