public void ButtonClick(UIButton button, UIButton.ButtonType buttonType) { bool success = false; switch (buttonType) { //Accept/Cancel current actions (submit stuff to the server for authentication and propegation to other players) case UIButton.ButtonType.AcceptActionButton: success = ButtonAcceptAction(); break; case UIButton.ButtonType.CancelActionButton: success = ButtonCancelAction(); break; //Used to switch between character and build mode case UIButton.ButtonType.BuildButton: success = ButtonBuildAction(); break; case UIButton.ButtonType.CharacterButton: success = ButtonCharacterAction(); break; //Creation actions. Highly dependent on several states case UIButton.ButtonType.CreateDemolishButton: success = ButtonDemolishAction(); break; case UIButton.ButtonType.CreateFloorButton: success = ButtonFloorAction(); break; case UIButton.ButtonType.CreateObjectButton: success = ButtonObjectAction(); break; case UIButton.ButtonType.CreateRoomButton: success = ButtonRoomAction(); break; case UIButton.ButtonType.CreateWallButton: success = ButtonWallAction(); break; case UIButton.ButtonType.CreateDoorButton: success = ButtonDoorAction(); break; default: Debug.LogError("Unrecognized button type " + buttonType.ToString()); break; } //TODO based on success, can play a pass/fail sound or do other nice stuff }
//Regular Button public UIButton CreateButton(UIButton.ButtonType type, float posX, float posY, Transform parent, string name = "!LEFTBLANK!") { GameObject uiParent = CreateButtonPrimitive(type, posX, posY, parent); switch (type) { case UIButton.ButtonType.Default: if (name == "!LEFTBLANK!") { name = "Default"; } break; case UIButton.ButtonType.Confirm: if (name == "!LEFTBLANK!") { name = "Confirm"; } break; case UIButton.ButtonType.Back: if (name == "!LEFTBLANK!") { name = "Back"; } break; default: return(null); } //Assign component UIButton output = uiParent.AddComponent <UIButton>(); output.Initialize(); output.UIName = name; return(output); }
//Base Button GameObject CreateButtonPrimitive(UIButton.ButtonType type, float posX, float posY, Transform parent) { //Select button type to spawn GameObject objectToCopy; switch (type) { case UIButton.ButtonType.Default: if (defaultButtonOriginal == null) { defaultButtonOriginal = SearchGO("Default", "KeyConfig"); if (defaultButtonOriginal == null) { return(null); } } objectToCopy = defaultButtonOriginal; break; case UIButton.ButtonType.Confirm: if (confirmButtonOriginal == null) { confirmButtonOriginal = SearchGO("Confirm", "KeyConfig"); if (confirmButtonOriginal == null) { return(null); } } objectToCopy = confirmButtonOriginal; break; case UIButton.ButtonType.Back: if (backButtonOriginal == null) { backButtonOriginal = SearchGO("Cancel", "Gamesettings"); if (backButtonOriginal == null) { return(null); } } objectToCopy = backButtonOriginal; break; default: return(null); } //Create parent GO and set it up GameObject uiParent = new GameObject("ModButton"); uiParent.transform.SetParent(parent, false); uiParent.transform.localScale = Vector3.one; uiParent.transform.localPosition = new Vector3(posX, posY, 0f); //Instantiate GO GameObject elementCreated = Instantiate(objectToCopy, uiParent.transform); elementCreated.name = "ModUIElement"; elementCreated.transform.localPosition = ZFixVector; //Shifts the UIelement to the front, to avoid z level clashing //For Default button, disable selection change on click if (type == UIButton.ButtonType.Default) { Destroy(elementCreated.GetComponent <GuiClickChangeSelection>()); } //For Confirm button, disable warning sign if (type == UIButton.ButtonType.Confirm) { Destroy(elementCreated.transform.Find("CloudWarn").gameObject); } return(uiParent); }