浏览代码

Develop axis support (#829)

/develop-generalizationTraining-TrainerController
Arthur Juliani 7 年前
当前提交
6b1ba14a
共有 1 个文件被更改,包括 90 次插入45 次删除
  1. 135
      unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs

135
unity-environment/Assets/ML-Agents/Scripts/CoreBrainPlayer.cs


using System.Collections;
using System.Collections;
using UnityEngine.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class CoreBrainPlayer : ScriptableObject, CoreBrain
{
[SerializeField] private bool broadcast = true;
[System.Serializable]
private struct DiscretePlayerAction

}
[System.Serializable]
private struct ContinuousPlayerAction
private struct KeyContinuousPlayerAction
[System.Serializable]
private struct AxisContinuousPlayerAction
{
public string axis;
public int index;
public float scale;
}
[FormerlySerializedAs("continuousPlayerActions")]
private ContinuousPlayerAction[] continuousPlayerActions;
private KeyContinuousPlayerAction[] keyContinuousPlayerActions;
[SerializeField]
[Tooltip("The list of axis actions.")]
/// Contains the mapping from input to continuous actions
private AxisContinuousPlayerAction[] axisContinuousPlayerActions;
[SerializeField] private int defaultAction = 0;
[SerializeField]
private int defaultAction = 0;
/// Reference to the brain that uses this CoreBrainPlayer
public Brain brain;

foreach (Agent agent in agentInfo.Keys)
{
var action = new float[brain.brainParameters.vectorActionSize];
foreach (ContinuousPlayerAction cha in continuousPlayerActions)
foreach (KeyContinuousPlayerAction cha in keyContinuousPlayerActions)
{
if (Input.GetKey(cha.key))
{
action[cha.index] = cha.value;
}
}
foreach (AxisContinuousPlayerAction axisAction in axisContinuousPlayerActions)
if (Input.GetKey(cha.key))
var axisValue = Input.GetAxis(axisAction.axis);
axisValue *= axisAction.scale;
if (Mathf.Abs(axisValue) > 0.0001)
action[cha.index] = cha.value;
action[axisAction.index] = axisValue;
}
}
}
else
{

public void OnInspector()
{
#if UNITY_EDITOR
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle(new GUIContent("Broadcast",
EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
broadcast = EditorGUILayout.Toggle(
new GUIContent("Broadcast",
var serializedBrain = new SerializedObject(this);
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
var serializedBrain = new SerializedObject(this);
if (brain.brainParameters.vectorActionSpaceType == SpaceType.continuous)
{
GUILayout.Label("Edit the continuous inputs for your actions", EditorStyles.boldLabel);
var keyActionsProp = serializedBrain.FindProperty("keyContinuousPlayerActions");
var axisActionsProp = serializedBrain.FindProperty("axisContinuousPlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(keyActionsProp , true);
EditorGUILayout.PropertyField(axisActionsProp, true);
serializedBrain.ApplyModifiedProperties();
if (keyContinuousPlayerActions == null)
{
keyContinuousPlayerActions = new KeyContinuousPlayerAction[0];
}
if (axisContinuousPlayerActions == null)
{
axisContinuousPlayerActions = new AxisContinuousPlayerAction[0];
}
foreach (KeyContinuousPlayerAction action in keyContinuousPlayerActions)
GUILayout.Label("Edit the continuous inputs for your actions",
EditorStyles.boldLabel);
var chas = serializedBrain.FindProperty("continuousPlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(chas, true);
serializedBrain.ApplyModifiedProperties();
if (continuousPlayerActions == null)
if (action.index >= brain.brainParameters.vectorActionSize)
continuousPlayerActions = new ContinuousPlayerAction[0];
EditorGUILayout.HelpBox(
string.Format(
"Key {0} is assigned to index {1} " +
"but the action size is only of size {2}"
, action.key.ToString(), action.index.ToString(),
brain.brainParameters.vectorActionSize.ToString()),
MessageType.Error);
foreach (ContinuousPlayerAction cha in continuousPlayerActions)
}
foreach (AxisContinuousPlayerAction action in axisContinuousPlayerActions)
{
if (action .index >= brain.brainParameters.vectorActionSize)
if (cha.index >= brain.brainParameters.vectorActionSize)
{
EditorGUILayout.HelpBox(string.Format(
"Key {0} is assigned to index {1} but the action size is only of size {2}"
, cha.key.ToString(), cha.index.ToString(),
brain.brainParameters.vectorActionSize.ToString()), MessageType.Error);
}
EditorGUILayout.HelpBox(
string.Format(
"Axis {0} is assigned to index {1} " +
"but the action size is only of size {2}"
, action.axis, action.index.ToString(),
brain.brainParameters.vectorActionSize.ToString()),
MessageType.Error);
else
{
GUILayout.Label("Edit the discrete inputs for your actions",
EditorStyles.boldLabel);
defaultAction = EditorGUILayout.IntField("Default Action", defaultAction);
var dhas = serializedBrain.FindProperty("discretePlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(dhas, true);
serializedBrain.ApplyModifiedProperties();
}
GUILayout.Label("You can change axis settings from Edit->Project Settings->Input",
EditorStyles.helpBox );
}
else
{
GUILayout.Label("Edit the discrete inputs for your actions", EditorStyles.boldLabel);
defaultAction = EditorGUILayout.IntField("Default Action", defaultAction);
var dhas = serializedBrain.FindProperty("discretePlayerActions");
serializedBrain.Update();
EditorGUILayout.PropertyField(dhas, true);
serializedBrain.ApplyModifiedProperties();
}
#endif
}
}
正在加载...
取消
保存