static void setupEventHandler(DUIType uiType, GameObject gameObject, Element element) { switch (uiType) { case DUIType.Button: gameObject.GetComponent <UnityEngine.UI.Button>().onClick.AddListener( () => element.actionFrame = Time.frameCount); break; case DUIType.TextField: gameObject.GetComponent <UnityEngine.UI.InputField>().onValueChanged.AddListener( (_) => element.actionFrame = Time.frameCount); break; case DUIType.HorizontalSlider: gameObject.GetComponent <UnityEngine.UI.Slider>().onValueChanged.AddListener( (_) => element.actionFrame = Time.frameCount); break; case DUIType.Toggle: gameObject.GetComponent <UnityEngine.UI.Toggle>().onValueChanged.AddListener( (_) => element.actionFrame = Time.frameCount); break; case DUIType.ScrollView: var scrollRect = gameObject.GetComponent <UnityEngine.UI.ScrollRect>(); element.callback = (_) => element.actionFrame = Time.frameCount; scrollRect.verticalScrollbar.onValueChanged.AddListener(element.callback); scrollRect.horizontalScrollbar.onValueChanged.AddListener(element.callback); break; } }
public Element(DUIType uiType, GameObject gameObject, Rect position) { this.uiType = uiType; this.gameObject = gameObject; this.position = position; this.actionFrame = 0; }
public static void ResetDefaultPrefab(DUIType uiType) { if (prefabDict_.ContainsKey(uiType)) { GameObject.Destroy(prefabDict_[uiType]); } string prefabPath = DUISettings.PrefabPathDict[uiType]; prefabDict_[uiType] = Resources.Load <GameObject>(prefabPath); }
public Element(DUIType uiType, GameObject gameObject, Rect position, string callerFilePath, int callerLineNumber) { this.uiType = uiType; this.gameObject = gameObject; this.actionFrame = 0; this.callback = null; this.searchInfo = new SearchInfo() { position = position, callerFilePath = callerFilePath, callerLineNumber = callerLineNumber, }; }
static Element search(DUIType uiType, Rect position, string callerFilePath, int callerLineNumber, GameObject prefab) { float bestCost = float.MaxValue; uint bestKey = 0; foreach (uint key in elementDict_.Keys) { if (alreadySelected_.Contains(key)) { continue; } var elem = elementDict_[key]; // Check if the element is in the current hierarchy level if (uiStack_.Last().transform != elem.gameObject.transform.parent) { continue; } // Select element whose rect is most similar to `position` if (uiType == elem.uiType && callerFilePath == elem.searchInfo.callerFilePath && callerLineNumber == elem.searchInfo.callerLineNumber) { float cost = (position.position - elem.searchInfo.position.position).magnitude; cost += (position.size - elem.searchInfo.position.size).magnitude; if (cost < bestCost) { bestCost = cost; bestKey = key; } if (bestCost <= 0) { break; } } } // Found if (bestKey > 0) { alreadySelected_.Add(bestKey); elementDict_[bestKey].gameObject.SetActive(true); elementDict_[bestKey].searchInfo.position = position; return(elementDict_[bestKey]); } // Not found -> Create a new element. if (false == prefabDict_.ContainsKey(uiType)) { ResetDefaultPrefab(uiType); } GameObject gameObject = null; if (prefab != null) { gameObject = GameObject.Instantiate(prefab, uiStack_.Last().transform); } else { gameObject = GameObject.Instantiate(prefabDict_[uiType], uiStack_.Last().transform); } uint newKey = nextID(); elementDict_[newKey] = new Element(uiType, gameObject, position, callerFilePath, callerLineNumber); alreadySelected_.Add(newKey); // Add event listener setupEventHandler(uiType, gameObject, elementDict_[newKey]); return(elementDict_[newKey]); }
//------------------------------------------------------------------------------ public static void SetDefaultPrefab(DUIType uiType, GameObject prefab) { prefabDict_[uiType] = prefab; }
//------------------------------------------------------------------------------------------ static Element search(DUIType uiType, Rect position) { float bestCost = float.MaxValue; uint bestKey = 0; foreach (uint key in elementDict_.Keys) { if (alreadySelected_.Contains(key)) { continue; } var elem = elementDict_[key]; // Check if the element is in the current hierarchy level if (uiStack_.Last().transform != elem.gameObject.transform.parent) { continue; } // Select element whose rect is most similar to `position` if (uiType == elem.uiType) { float cost = (position.position - elem.position.position).magnitude; cost += (position.size - elem.position.size).magnitude; if (cost < bestCost) { bestCost = cost; bestKey = key; } if (bestCost <= 0) { break; } } } // Found if (bestKey > 0) { alreadySelected_.Add(bestKey); return(elementDict_[bestKey]); } // Not found -> Create a new element. if (false == prefabDict_.ContainsKey(uiType)) { string prefabPath = DUISettings.PrefabPathDict[uiType]; prefabDict_[uiType] = Resources.Load <GameObject>(prefabPath); } var gameObject = GameObject.Instantiate(prefabDict_[uiType], uiStack_.Last().transform); uint newKey = nextID(); elementDict_[newKey] = new Element(uiType, gameObject, position); alreadySelected_.Add(newKey); // Add event listener setupEventHandler(uiType, gameObject, elementDict_[newKey]); return(elementDict_[newKey]); }