private void CopyCreateMenu(UIToggle aCopyToggle) { if(m_ToggleParams != null && aCopyToggle != null) { DebugUtils.Log(aCopyToggle.uiType); switch(aCopyToggle.uiType) { case UIType.IMAGE: { UIImageParams imageParams = new UIImageParams(); m_ToggleParams = imageParams; m_ToggleParams.isSelectable = aCopyToggle.selectable; m_ToggleParams.recieveActions = aCopyToggle.receivesActionEvents; m_ToggleParams.uiSpace = aCopyToggle.uiSpace; m_ToggleParams.uiType = aCopyToggle.uiType; UIImage image = aCopyToggle.GetComponentInChildren<UIImage>(); if(image != null) { imageParams.color = image.color; imageParams.height = image.height; imageParams.width = image.width; imageParams.meshBoarder = image.meshBoarder; imageParams.innerUVBoarder = image.innerUVBoarder; imageParams.outerUVBoarder = image.innerUVBoarder; imageParams.shader = image.shader; imageParams.texture = image.texture; } else { DebugUtils.LogError(MISSING_IMAGE); } } break; case UIType.LABEL: { UILabelParams labelParams = new UILabelParams(); m_ToggleParams = labelParams; m_ToggleParams.isSelectable = aCopyToggle.selectable; m_ToggleParams.recieveActions = aCopyToggle.receivesActionEvents; m_ToggleParams.uiSpace = aCopyToggle.uiSpace; m_ToggleParams.uiType = aCopyToggle.uiType; UILabel label = aCopyToggle.GetComponentInChildren<UILabel>(); if (label != null) { labelParams.color = label.color; labelParams.font = label.font; labelParams.fontSize = label.fontSize; labelParams.fontTexture = label.fontTexture; labelParams.text = label.text; } else { DebugUtils.LogError(MISSING_LABEL); } } break; case UIType.BUTTON: { DebugUtils.Log("Create Button"); UIButtonParams buttonParams = new UIButtonParams(); m_ToggleParams = buttonParams; m_ToggleParams.isSelectable = aCopyToggle.selectable; m_ToggleParams.recieveActions = aCopyToggle.receivesActionEvents; m_ToggleParams.uiSpace = aCopyToggle.uiSpace; m_ToggleParams.uiType = aCopyToggle.uiType; UIButton button = aCopyToggle.GetComponentInChildren<UIButton>(); if (button == null) { DebugUtils.LogError(MISSING_BUTTON); break; } buttonParams.disabled = button.buttonState == UIButtonState.DISABLED; buttonParams.disabledTexture = button.disabledTexture; buttonParams.normalTexture = button.normalTexture; buttonParams.hoverTexture = button.hoverTexture; buttonParams.downTexture = button.downTexture; buttonParams.enabledTextColor = button.enabledTextColor; buttonParams.disabledTextColor = button.disabledTextColor; buttonParams.eventListener = button.eventListener; UILabel label = button.GetComponentInChildren<UILabel>(); if (label == null) { DebugUtils.LogError(MISSING_LABEL); } else { buttonParams.labelColor = label.color; buttonParams.labelFont = label.font; buttonParams.labelFontSize = label.fontSize; buttonParams.labelFontTexture = label.fontTexture; buttonParams.labelText = label.text; } UIImage image = button.GetComponentInChildren<UIImage>(); if (image == null) { DebugUtils.LogError(MISSING_IMAGE); } else { buttonParams.imageColor = image.color; buttonParams.imageHeight = image.height; buttonParams.imageWidth = image.width; buttonParams.imageMeshBoarder = image.meshBoarder; buttonParams.imageInnerUVBoarder = image.innerUVBoarder; buttonParams.imageOuterUVBoarder = image.outerUVBoarder; buttonParams.imageShader = image.shader; buttonParams.imageTexture = image.texture; } } break; case UIType.TEXTFIELD: { UITextfieldParams textfieldParams = new UITextfieldParams(); m_ToggleParams = textfieldParams; m_ToggleParams.isSelectable = aCopyToggle.selectable; m_ToggleParams.recieveActions = aCopyToggle.receivesActionEvents; m_ToggleParams.uiSpace = aCopyToggle.uiSpace; m_ToggleParams.uiType = aCopyToggle.uiType; UITextfield textfield = aCopyToggle.GetComponentInChildren<UITextfield>(); if (textfield == null) { DebugUtils.LogError(MISSING_TEXTFIELD); break; } textfieldParams.disabled = textfield.buttonState == UIButtonState.DISABLED; textfieldParams.disabledTexture = textfield.disabledTexture; textfieldParams.normalTexture = textfield.normalTexture; textfieldParams.hoverTexture = textfield.hoverTexture; textfieldParams.downTexture = textfield.downTexture; textfieldParams.enabledTextColor = textfield.enabledTextColor; textfieldParams.disabledTextColor = textfield.disabledTextColor; textfieldParams.eventListener = textfield.eventListener; textfieldParams.maxCharacter = textfield.maxCharacter; UILabel label = textfield.GetComponentInChildren<UILabel>(); if (label == null) { DebugUtils.LogError(MISSING_LABEL); } else { textfieldParams.labelColor = label.color; textfieldParams.labelFont = label.font; textfieldParams.labelFontSize = label.fontSize; textfieldParams.labelFontTexture = label.fontTexture; textfieldParams.labelText = label.text; } UIImage image = textfield.GetComponentInChildren<UIImage>(); if (image == null) { DebugUtils.LogError(MISSING_IMAGE); } else { textfieldParams.imageColor = image.color; textfieldParams.imageHeight = image.height; textfieldParams.imageWidth = image.width; textfieldParams.imageMeshBoarder = image.meshBoarder; textfieldParams.imageInnerUVBoarder = image.innerUVBoarder; textfieldParams.imageOuterUVBoarder = image.outerUVBoarder; textfieldParams.imageShader = image.shader; textfieldParams.imageTexture = image.texture; } } break; } } }
/// <summary> /// Creates a UI Image prefab at using the toggle as the root /// </summary> /// <param name="aParams">The parameters for creating the toggle</param> /// <param name="aToggle">The root of creation</param> /// <returns></returns> public static UIImage CreateUIImage(UIImageParams aParams, UIToggle aToggle) { if (aParams == null || aToggle == null) { return null; } GameObject rootGameObject = aToggle.gameObject; GameObject imageGameObject = new GameObject(aParams.name + IMAGE_POST_FIX); imageGameObject.transform.position = Vector3.zero; imageGameObject.transform.rotation = Quaternion.identity; imageGameObject.transform.parent = rootGameObject.transform; imageGameObject.layer = rootGameObject.layer; imageGameObject.AddComponent<MeshRenderer>(); imageGameObject.AddComponent<MeshFilter>(); UIImage uiImage = imageGameObject.AddComponent<UIImage>(); uiImage.meshName = MESH_NAME; uiImage.width = aParams.width; uiImage.height = aParams.height; uiImage.meshBoarder = aParams.meshBoarder; uiImage.outerUVBoarder = aParams.outerUVBoarder; uiImage.innerUVBoarder = aParams.innerUVBoarder; uiImage.texture = aParams.texture; uiImage.shader = aParams.shader; uiImage.color = aParams.color; uiImage.GenerateMaterial(); uiImage.GenerateMesh(); uiImage.SetTexture(); uiImage.SetColor(); #if UNITY_EDITOR UnityEditor.EditorUtility.SetDirty(uiImage); #endif return uiImage; }