浏览代码

Updated control panel to not need labels to add new controls

/aisv647_visualizations
Steven Borkman 4 年前
当前提交
88566a4e
共有 1 个文件被更改,包括 31 次插入39 次删除
  1. 70
      com.unity.perception/Runtime/GroundTruth/Labelers/Visualization/ControlPanel.cs

70
com.unity.perception/Runtime/GroundTruth/Labelers/Visualization/ControlPanel.cs


using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

/// </summary>
public class ControlPanel : MonoBehaviour
{
Dictionary<string, GameObject> controlMap = new Dictionary<string, GameObject>();
HashSet<GameObject> m_Controls = new HashSet<GameObject>();
/// Adds a new UI control to the control panel. If the panel already contains an elment with the
/// passed in name, the UI element will not be added and the method will return false. Also, all
/// UI elements must have a LayoutElement component and if they do not, this method will reject
/// the new element, and return false.
/// Retrieves a list of the current controls in the control panel.
/// </summary>
public List<GameObject> controls
{
get
{
return m_Controls.ToList<GameObject>();
}
}
/// <summary>
/// Adds a new UI control to the control panel. If the control cannot be added and the method will
/// return false. Also, all UI elements must have a LayoutElement component and if they do not,
/// this method will reject the new element, and return false.
public bool AddNewControl(string name, GameObject uiControl)
public bool AddNewControl(GameObject uiControl)
{
if (uiControl.GetComponent<RectTransform>() == null)
{

return false;
}
if (controlMap.ContainsKey(name))
if (m_Controls.Add(uiControl))
Debug.LogWarning("A control with the name: " + name + " has already been registered with the control panel.");
return false;
uiControl.transform.SetParent(this.transform, false);
return true;
controlMap[name] = uiControl;
uiControl.transform.SetParent(this.transform, false);
return true;
return false;
/// Removes the component with the passed in name from the control panel. Returns
/// Removes the passed in component from the control panel. Returns
/// The caller is responsible for destroying the control.
public bool RemoveControl(string name)
public bool RemoveControl(GameObject uiControl)
if (!controlMap.ContainsKey(name))
if (m_Controls.Remove(uiControl))
Debug.LogWarning(name + " does not exist in control panel, cannot remove.");
return false;
}
uiControl.transform.SetParent(null);
var control = controlMap[name];
controlMap.Remove(name);
control.transform.SetParent(null);
GameObject.Destroy(control.gameObject);
return true;
return true;
}
return false;
}
/// <summary>

/// </summary>
public GameObject AddToggleControl(string name, UnityAction<bool> listener)
{
if (controlMap.ContainsKey(name))
{
Debug.LogWarning("A control with the name: " + name + " has already been registered with the control panel.");
return null;
}
if (listener == null)
{
Debug.LogWarning("Adding toggle to control panel without a listener, nothing will respond to user's clicks");

toggle.GetComponentInChildren<Text>().text = name;
toggle.GetComponent<Toggle>().onValueChanged.AddListener(listener);
controlMap[name] = toggle;
m_Controls.Add(toggle);
return toggle;
}

/// </summary>
public GameObject AddSliderControl(string name, float defaultValue, UnityAction<float> listener)
{
if (controlMap.ContainsKey(name))
{
Debug.LogWarning("A control with the name: " + name + " has already been registered with the control panel.");
return null;
}
if (listener == null)
{
Debug.LogWarning("Adding slider to control panel without a listener, nothing will respond to user's interactions");

slider.value = defaultValue;
slider.onValueChanged.AddListener(listener);
controlMap[name] = gameObject;
m_Controls.Add(gameObject);
return gameObject;
}
正在加载...
取消
保存