/// <summary> /// constructs the dropdown /// </summary> void activate_dropdown() { //handle spammed commands if (Time.time - last_activation < delay) { last_activation = Time.time; return; } if (activated == false && interactable_) { last_activation = Time.time; //handle the listeners /*It is assumed every interaction with the dropdown is a single button interaction, for multiple actions the * onPress listener must have another function handle input and only use deactivate_dropdown when the dropdown * is closed.*/ DropButton.onPress.RemoveListener(activate_dropdown); DropButton.onPress.AddListener(deactivate_dropdown); activated = true; //makes boundry if (boundingBox != null) { GameObject BoundingBox = Instantiate(boundingBox, this.transform.position, this.transform.rotation); BoundingBox.transform.SetParent(this.transform); } bool scrolling = false; scrollingOptions = new GameObject[options.Length]; //for scrolling support, not currently supported, currently maxOptionsShown < options.Length is always false if (maxOptionsShown < options.Length) { scrolling = true; } //considering changing i to position or index, somehting more descriptive. for (int i = 0; i < options.Length; i++) { //makes each item in options string op = options[i].label; //make template GameObject inst = Instantiate(template, this.transform.position + this.transform.up * ((-i * changeInHeight) - firstOffset), this.transform.rotation); inst.transform.parent = this.transform; Text label = inst.AddComponent <Text>(); label.text = op; GameObject extra = null; Option opt = options[i]; //spawns extras where appropriate if (opt.Disabled) { if (opt.extrasDisabled != null) { extra = Instantiate(opt.extrasDisabled, this.transform.position + this.transform.up * -i * changeInHeight, this.transform.rotation); } } else { if (opt.Hovered) { if (opt.extrasHovered != null) { extra = Instantiate(opt.extrasHovered, this.transform.position + this.transform.up * -i * changeInHeight, this.transform.rotation); } } else { if (opt.Pressed) { if (opt.extrasPressed != null) { extra = Instantiate(opt.extrasPressed, this.transform.position + this.transform.up * -i * changeInHeight, this.transform.rotation); } } else { if (opt.extras != null) { extra = Instantiate(opt.extras, this.transform.position + this.transform.up * -i * changeInHeight, this.transform.rotation); } } } } //gets the created gameobject with the collider GameObject DDinst = inst.gameObject.GetComponentInChildren <Collider>().gameObject; //DDi stands for Drop Down Item a deprecated script but a useful name. It is actually a new button script ButtonScript DDI = DDinst.AddComponent <ButtonScript>() as ButtonScript; //may be incorrect if (extra != null) { extra.transform.parent = inst.transform; } //set values from options DDI.extras = options[i].extras; DDI.extrasHovered = options[i].extrasHovered; DDI.extrasPressed = options[i].extrasPressed; DDI.extrasDisabled = options[i].extrasDisabled; DDI.ItemFade = ItemFade; DDI.setActivatinglayer(activating); DDI.setHoveringlayer(hovering); DDI.extras = extra; int index = i; //create custom listener that has the number of the item picked preset UnityAction UA = () => closeAndPick(index); DDI.onPress.AddListener(UA); //set text TextMesh text = inst.GetComponentInChildren <TextMesh>(); text.text = op; //determines which gradient the item will use DDI.useGradient = this.UseGradient; DDI.useInstantTransition = this.UseInstantTransition; if (UseInstantTransition) { DDI.useInstantTransition = true; DDI.startColor = this.startColor; DDI.endColor = this.endColor; } else { if (UseGradient) { DDI.gradient = this.DropDownItemGradient; DDI.useGradient = true; } else { DDI.startColor = options[i].StartColor; DDI.endColor = options[i].EndColor; DDI.gradient = options[i].buttonGrad; DDI.useGradient = options[i].UseGradient; DDI.useInstantTransition = options[i].UseInstantTransition; } } if (i > maxOptionsShown - 1) { inst.SetActive(false); } //set the array scrollingOptions[i] = inst; } //not used if (scrolling) { //create new slider oldSlider.gameObject.SetActive(true); //oldSlider.transform.position = this.transform.position + /*this.transform.up*/new Vector3(1, 0, 0); //add listener on the slider float event oldSlider.onValueChanged.AddListener(delegate { SliderUpdate(); }); } } }
/// <summary> /// set up of the object that happens before start /// </summary> void Awake() { //find and set button DropButton = this.GetComponent <ButtonScript>() as ButtonScript; if (DropButton == null) { DropButton = this.gameObject.AddComponent <ButtonScript>(); } if (template == null) { Debug.Log("You need a template for your dropdown buttons, even if that is an empty object with text"); } if (delay <= 0.03) { delay = 0.03f; Debug.Log("Putting the delay to low may cuase multiple inputs for one action. Also, it may be a seizure hazard"); } //will produce errors if no button is set change to give readible error DropButton.onPress.AddListener(activate_dropdown); //set the layers DropButton.setActivatinglayer(activating); DropButton.setHoveringlayer(hovering); maxOptionsShown = options.Length; //handle the text this.label = this.GetComponentInChildren <TextMesh>(); label.text = defaultString; //fixing testing problems with slider if (oldSlider != null) { oldSlider.gameObject.active = false; } //handles starting default string //negative means start at default string if (defaultnumber < 0) { label.text = defaultString; } else { //if there is no default string pick the item at the number if (defaultString.Length == 0) { label.text = options[defaultnumber].label; } else { //resize to include the default string[] temp = new string[options.Length + 1]; int counter = 0; for (int i = 0; i < options.Length; i++) { string op = options[i].label; if (counter == defaultnumber) { temp[counter] = defaultString; counter++; } temp[counter] = op; counter++; } if (counter == defaultnumber) { temp[counter] = defaultString; } } } }