Skip to content

Instantly share code, notes, and snippets.

@seiroise
Created June 29, 2016 03:32
Show Gist options
  • Select an option

  • Save seiroise/ad74786e4621fe7ccf27ab42158d6400 to your computer and use it in GitHub Desktop.

Select an option

Save seiroise/ad74786e4621fe7ccf27ab42158d6400 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// 調節用パラメータを表示
/// </summary>
public class Indicator : MonoBehaviour {
[SerializeField] bool indicate = true; //表示するか
IParameterIndicator[] paramIndicator;
Vector2 scrollPos;
void Start() {
//全てのIParameterIndicatorを取ってくる
paramIndicator = GetAllInterfaces<IParameterIndicator>();
}
void Update() {
//表示/非表示切り替え
if(Input.GetKeyDown(KeyCode.Escape)) indicate = !indicate;
}
void OnGUI() {
if(!indicate) return;
Rect scrollArea = new Rect(0f, 0f, Screen.width / 2, Screen.height);
//背景
GUI.Box(scrollArea, "");
//ScrollViewの開始
GUILayout.BeginScrollView(scrollPos, GUILayout.Width(scrollArea.width), GUILayout.Height(scrollArea.height));
foreach(IParameterIndicator e in paramIndicator) {
e.IndicateParameter();
}
GUILayout.EndScrollView();
}
/// <summary>
/// 全てのGameObjectから特定のインタフェースを取得する
/// </summary>
private T[] GetAllInterfaces<T>() {
List<T> list = new List<T>();
foreach (GameObject obj in UnityEngine.Object.FindObjectsOfType(typeof(GameObject))) {
T[] interfaces = GetInterfaces<T>(obj);
if(interfaces.Length > 0) list.AddRange(interfaces);
}
return list.ToArray();
}
/// <summary>
/// GameObjectから特定のインタフェースを取得する
/// </summary>
private T[] GetInterfaces<T>(GameObject gObj) {
MonoBehaviour[] mObjs = gObj.GetComponents<MonoBehaviour>();
return (from a in mObjs where a.GetType().GetInterfaces().Any(k => k == typeof(T)) select (T)(object)a).ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment