Skip to content

Instantly share code, notes, and snippets.

@h1ddengames
Last active July 18, 2020 22:52
Show Gist options
  • Save h1ddengames/5a78674391e5b50dad8cdd638a46b706 to your computer and use it in GitHub Desktop.
Save h1ddengames/5a78674391e5b50dad8cdd638a46b706 to your computer and use it in GitHub Desktop.
Unity Textmeshpro Dropdown: Disable certain options from the dropdown list
// MODIFIED FROM https://stackoverflow.com/questions/55297626/disable-an-options-in-a-dropdown-unity
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(TMP_Dropdown))]
[DisallowMultipleComponent]
public class DropDownController : MonoBehaviour, IPointerClickHandler {
[Tooltip("Indexes that should be ignored. Indexes are 0 based.")]
public List<int> indexesToDisable = new List<int>();
private TMP_Dropdown _dropdown;
private void Awake() {
_dropdown = GetComponent<TMP_Dropdown>();
}
public void OnPointerClick(PointerEventData eventData) {
var dropDownList = GetComponentInChildren<Canvas>();
if(!dropDownList) return;
// If the dropdown was opened find the options toggles
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);
for(var i = 0; i < toogles.Length; i++) {
toogles[i].interactable = !indexesToDisable.Contains(i - 1);
}
}
// Anytime change a value by index
public void EnableOption(int index, bool enable) {
if(enable) {
// remove index from disabled list
if(indexesToDisable.Contains(index)) {
indexesToDisable.Remove(index);
}
} else {
// add index to disabled list
if(!indexesToDisable.Contains(index)) {
indexesToDisable.Add(index);
}
}
var dropDownList = GetComponentInChildren<Canvas>();
// If this returns null than the Dropdown was closed
if(!dropDownList) return;
// If the dropdown was opened find the options toggles
var toogles = dropDownList.GetComponentsInChildren<Toggle>(true);
toogles[index].interactable = enable;
}
// Anytime change a value by string label
public void EnableOption(string label, bool enable) {
var index = _dropdown.options.FindIndex(o => string.Equals(o.text, label));
// We need a 1-based index
EnableOption(index + 1, enable);
}
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class DropDownSetter : MonoBehaviour {
public List<string> countryOptions = new List<string>() {
"-- Region: Africa --",
"Algeria - DZ",
"Egypt - EG",
"Mauritius - MU",
"Mayotte - YT",
"Morocco - MA",
"South Africa - ZA",
"Tunisia - TN",
"-- Region: North America --",
"Canada - CA",
"Costa Rica - CR",
"Mexico - MX",
"Panama - PA",
"Puerto Rico - PR",
"United States - US",
"-- Region: South America --",
"Argentina - AR",
"Bolivia - BO",
"Brazil - BR",
"Chile - CL",
"Colombia - CO",
"Ecuador - EC",
"Peru - PE",
"Uruguay - UY",
"Venezuela - VE",
"-- Region: Asia --",
"Armenia - AM",
"Bahrain - BH",
"Hong Kong - HK",
"India - IN",
"Indonesia - ID",
"Israel - IL",
"Japan - JP",
"Kuwait - KW",
"Lebanon - LB",
"Malaysia - MY",
"Maldives - MV",
"Oman - OM",
"Pakistan - PK",
"Philippines - PH",
"Qatar - QA",
"Saudi Arabia - SA",
"Singapore - SG",
"South Korea - KR",
"Taiwan - TW",
"Thailand - TH",
"Turkey - TR",
"United Arab Emirates - AE",
"Vietnam - VN",
"-- Region: Europe --",
"Albania - AL",
"Austria - AT",
"Belgium - BE",
"Bosnia and Herzegovina - BA",
"Bulgaria - BG",
"Croatia - HR",
"Cyprus - CY",
"Czech Republic - CZ",
"Denmark - DK",
"Estonia - EE",
"Finland - FI",
"France - FR",
"Georgia - GE",
"Germany - DE",
"Greece - GR",
"Hungary - HU",
"Iceland - IS",
"Ireland - IE",
"Italy - IT",
"Kosovo - XK",
"Latvia - LV",
"Lithuania - LT",
"Luxembourg - LU",
"Macedonia - MK",
"Malta - MT",
"Montenegro - ME",
"Netherlands - NL",
"Norway - NO",
"Poland - PL",
"Portugal - PT",
"Romania - RO",
"Russia - RU",
"Serbia - RS",
"Slovakia - SK",
"Slovenia - SI",
"Spain - ES",
"Sweden - SE",
"Switzerland - CH",
"Ukraine - UA",
"United Kingdom - GB",
"-- Region: Oceania --",
"Australia - AU",
"New Caledonia - NC",
"New Zealand - NZ"
};
// Start is called before the first frame update
void Start() {
DropDownController dropDownController = GetComponent<DropDownController>();
TMP_Dropdown dropdown = GetComponent<TMP_Dropdown>();
dropdown.options.Clear();
foreach(var item in countryOptions) {
dropdown.options.Add(new TMP_Dropdown.OptionData(item));
}
dropDownController.EnableOption("-- Region: Africa --", false);
dropDownController.EnableOption("-- Region: North America --", false);
dropDownController.EnableOption("-- Region: South America --", false);
dropDownController.EnableOption("-- Region: Asia --", false);
dropDownController.EnableOption("-- Region: Europe --", false);
dropDownController.EnableOption("-- Region: Oceania --", false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment