Is there a way to select region?

Lethn
Lethn
I'm looking through a tutorial on how to do a room browser and that part is all very straight forward but I'd like to see if it's possible to do a drop down menu that gives the players freedom to select which region they want to make a room in. I did some research on this already and apparently there are restrictions on doing this type of thing? Could somebody correct me if I'm wrong or if there's been some kind of change I'd just like to know.

Just to be clear, I'm not talking about the server settings, I'm talking about implementing a drop down menu within the actual project.

Comments

  • Gage_IAG
    Gage_IAG ✭✭
    edited January 2019
    You can do something like this, let me know if theres any issues:
    Dropdown regionDropdown;
    
    void Start() {
        GetRegions();
    }
    
    void OnEnable() {
        regionListDropdown.onValueChanged.AddListener(delegate { RegionListChanged(); });
    }
    
    void GetRegions() {
        foreach (CloudRegionCode region in System.Enum.GetValues(typeof(CloudRegionCode))) {
            if (region != CloudRegionCode.none) {
                regionDropdown.options.Clear();
                regionDropdown.options.Add(new Dropdown.OptionData(region.ToString()));
            }
        }
        regionDropdown.value = 0;
        regionDropdown.captionText.text =  regionDropdown.options[0].text;
    }
    
    public void RegionListChanged() {
        PhotonNetwork.Disconnect();
        CloudRegionCode regionSwitchCode = (CloudRegionCode)System.Enum.Parse(typeof(CloudRegionCode), regionListDropdown.options[regionListDropdown.value].text);
        PhotonNetwork.ConnectToRegion(regionSwitchCode, PhotonNetwork.gameVersion);
    }
  • Lethn
    Lethn
    edited January 2019
    Oh thanks! I'll check this out, that all looks very straightforward, I seriously need to take the time to study indexes and lists.
  • Theres many ways to go about doing this, just know that. This is just a way i generally use if a project requires region selection.
  • I'm not looking for anything too fancy to begin with but I do want to make a semi-decent server browser which is why I was asking around.
  • Lethn
    Lethn
    edited February 2019
    I'm just bumping this topic because I tried the code and it came up with a bunch of assembly reference errors and so on for some reason, if somebody could help me out that would be great. I just realised is this code in PUN 1 or PUN 2?

    I think I'm just running into so many problems partly because of PUN 2 that it's just not worth it for me to update yet, I may have to go back to PUN 1.

    Edit: Just as a note PUN 1 has immediately helped things however I get the error: error CS0103: The name 'regionListDropdown' does not exist in the current context
  • After chatting over on the Unity forums I sort of found a solution to this, for whatever reason, the line

    regionDropdown.options.Clear();

    Deletes all of the options except for rue, by removing it it seemed to make things functional at least.
  • wasd
    wasd
    edited August 2021
    Hello Lethn, I am using PUN2 and I put the code in and I am getting the same error: "error CS0103: The name 'regionListDropdown' does not exist in the current context" How did you fix this?
    Thanks,
    wasd
  • Gage_IAG wrote: »
    Theres many ways to go about doing this, just know that. This is just a way i generally use if a project requires region selection.

    Hey Gage,
    I am wondering is there a way to change server region by change the region value in the photon server settings in the recourses folder? Because I know you can change region there so I was wondering.
  • how to make that using PUN2