public static UIVerticalSliderKnob Create(CreateArgs input) { GameObject go = new GameObject(input.widgetName); go.tag = "UICollider"; go.layer = LayerMask.NameToLayer("CameraHidden"); // Find the anchor of the parent if it is a UIElement Vector3 parentAnchor = Vector3.zero; if (input.parent) { UIElement elem = input.parent.gameObject.GetComponent <UIElement>(); if (elem) { parentAnchor = elem.Anchor; } } UIVerticalSliderKnob uiSliderKnob = go.AddComponent <UIVerticalSliderKnob>(); uiSliderKnob.transform.parent = input.parent; uiSliderKnob.transform.localPosition = parentAnchor + input.relativeLocation; uiSliderKnob.transform.localRotation = Quaternion.identity; uiSliderKnob.transform.localScale = Vector3.one; uiSliderKnob.radius = input.radius; uiSliderKnob.depth = input.depth; // Setup the Meshfilter MeshFilter meshFilter = go.GetComponent <MeshFilter>(); if (meshFilter != null) { meshFilter.sharedMesh = UIUtils.BuildRoundedBox(2.0f * input.radius, 2.0f * input.radius, input.radius, input.depth); } // Setup the MeshRenderer MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>(); if (meshRenderer != null && input.material != null) { meshRenderer.sharedMaterial = Instantiate(input.material); uiSliderKnob._color.useConstant = false; uiSliderKnob._color.reference = input.c; meshRenderer.sharedMaterial.SetColor("_BaseColor", uiSliderKnob.Color); meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; meshRenderer.renderingLayerMask = 2; // "LightLayer 1" } return(uiSliderKnob); }
public static void Create(CreateArgs input) { GameObject go = new GameObject(input.widgetName); go.tag = "UICollider"; // Find the anchor of the parent if it is a UIElement Vector3 parentAnchor = Vector3.zero; if (input.parent) { UIElement elem = input.parent.gameObject.GetComponent <UIElement>(); if (elem) { parentAnchor = elem.Anchor; } } UIVerticalSlider uiSlider = go.AddComponent <UIVerticalSlider>(); // NOTE: also creates the MeshFilter, MeshRenderer and Collider components uiSlider.relativeLocation = input.relativeLocation; uiSlider.transform.parent = input.parent; uiSlider.transform.localPosition = parentAnchor + input.relativeLocation; uiSlider.transform.localRotation = Quaternion.identity; uiSlider.transform.localScale = Vector3.one; uiSlider.width = input.width; uiSlider.height = input.height; uiSlider.margin = input.margin; uiSlider.thickness = input.thickness; uiSlider.sliderPositionBegin = input.sliderBegin; uiSlider.sliderPositionEnd = input.sliderEnd; uiSlider.railMargin = input.railMargin; uiSlider.railThickness = input.railThickness; uiSlider.knobRadius = input.knobRadius; uiSlider.knobDepth = input.knobDepth; uiSlider.dataSource = input.dataSource; uiSlider.minValue = input.minValue; uiSlider.maxValue = input.maxValue; uiSlider.currentValue = input.currentValue; uiSlider.textContent = input.caption; uiSlider.sourceMaterial = input.material; uiSlider.sourceRailMaterial = input.railMaterial; uiSlider.sourceKnobMaterial = input.knobMaterial; uiSlider.textValueAlign = input.textValueAlign; uiSlider.baseColor.useConstant = false; uiSlider.baseColor.reference = input.color; uiSlider.textColor.useConstant = false; uiSlider.textColor.reference = input.textColor; uiSlider.pushedColor.useConstant = false; uiSlider.pushedColor.reference = input.pushedColor; uiSlider.selectedColor.useConstant = false; uiSlider.selectedColor.reference = input.selectedColor; // Setup the Meshfilter MeshFilter meshFilter = go.GetComponent <MeshFilter>(); if (meshFilter != null) { meshFilter.sharedMesh = UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness); uiSlider.Anchor = Vector3.zero; BoxCollider coll = go.GetComponent <BoxCollider>(); if (coll != null) { Vector3 initColliderCenter = meshFilter.sharedMesh.bounds.center; Vector3 initColliderSize = meshFilter.sharedMesh.bounds.size; if (initColliderSize.z < UIElement.collider_min_depth_shallow) { coll.center = new Vector3(initColliderCenter.x, initColliderCenter.y, UIElement.collider_min_depth_shallow / 2.0f); coll.size = new Vector3(initColliderSize.x, initColliderSize.y, UIElement.collider_min_depth_shallow); } else { coll.center = initColliderCenter; coll.size = initColliderSize; } coll.isTrigger = true; } } // Setup the MeshRenderer MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>(); if (meshRenderer != null && input.material != null) { // Clone the material. meshRenderer.sharedMaterial = Instantiate(input.material); meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; meshRenderer.renderingLayerMask = 2; // "LightLayer 1" uiSlider.SetColor(input.color.value); } // // RAIL // float railWidth = 2 * uiSlider.railMargin; float railHeight = (input.height - 2 * input.margin) * (input.sliderEnd - input.sliderBegin); float railThickness = uiSlider.railThickness; float railMargin = uiSlider.railMargin; Vector3 railPosition = new Vector3(input.width / 2.0f - railMargin, -input.height + input.margin + (input.height - 2 * input.margin) * uiSlider.sliderPositionEnd, -railThickness); uiSlider.rail = UIVerticalSliderRail.Create( new UIVerticalSliderRail.CreateArgs { parent = go.transform, widgetName = "Rail", relativeLocation = railPosition, width = railWidth, height = railHeight, thickness = railThickness, margin = railMargin, material = input.railMaterial, c = input.railColor } ); // // KNOB // float newKnobRadius = uiSlider.knobRadius; float newKnobDepth = uiSlider.knobDepth; float pct = (uiSlider.currentValue - uiSlider.minValue) / (uiSlider.maxValue - uiSlider.minValue); float heightWithoutMargins = input.height - 2.0f * input.margin; float startY = -input.height + input.margin + heightWithoutMargins * uiSlider.sliderPositionBegin + railMargin; float endY = -input.height + input.margin + heightWithoutMargins * uiSlider.sliderPositionEnd - railMargin; float posY = startY + pct * (endY - startY); Vector3 knobPosition = new Vector3((input.width / 2.0f) - uiSlider.knobRadius, posY + uiSlider.knobRadius, -uiSlider.knobDepth); uiSlider.knob = UIVerticalSliderKnob.Create( new UIVerticalSliderKnob.CreateArgs { widgetName = "Knob", parent = go.transform, relativeLocation = knobPosition, radius = newKnobRadius, depth = newKnobDepth, material = input.knobMaterial, c = input.knobColor } ); // // CANVAS (to hold the image) // GameObject canvas = new GameObject("Canvas"); canvas.transform.parent = uiSlider.transform; Canvas c = canvas.AddComponent <Canvas>(); c.renderMode = RenderMode.WorldSpace; RectTransform rt = canvas.GetComponent <RectTransform>(); // auto added when adding Canvas rt.localScale = Vector3.one; rt.localRotation = Quaternion.identity; rt.anchorMin = new Vector2(0, 1); rt.anchorMax = new Vector2(0, 1); rt.pivot = new Vector2(0, 1); // top left rt.sizeDelta = new Vector2(uiSlider.width, uiSlider.height); rt.localPosition = Vector3.zero; // top left CanvasScaler cs = canvas.AddComponent <CanvasScaler>(); cs.dynamicPixelsPerUnit = 300; // 300 dpi, sharp font cs.referencePixelsPerUnit = 100; // default? float minSide = Mathf.Min(uiSlider.width, uiSlider.height); // Add an Image under the Canvas if (input.icon != null) { GameObject image = new GameObject("Image"); image.transform.parent = canvas.transform; Image img = image.AddComponent <Image>(); img.sprite = input.icon; img.color = input.textColor.value; RectTransform trt = image.GetComponent <RectTransform>(); trt.localScale = Vector3.one; trt.localRotation = Quaternion.identity; trt.anchorMin = new Vector2(0, 1); trt.anchorMax = new Vector2(0, 1); trt.pivot = new Vector2(0, 1); // top left // TODO: non square icons ratio... trt.sizeDelta = new Vector2(minSide - 2.0f * input.margin, minSide - 2.0f * input.margin); trt.localPosition = new Vector3(input.margin, -input.margin, -0.001f); // top-left minus margins } // Text VALUE { GameObject text = new GameObject("TextValue"); text.transform.parent = canvas.transform; TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>(); t.text = input.currentValue.ToString("#0.00"); t.enableAutoSizing = true; t.fontSizeMin = 1; t.fontSizeMax = 500; t.fontStyle = FontStyles.Normal; t.alignment = TextAlignmentOptions.Right; t.color = input.textColor.value; RectTransform trt = t.GetComponent <RectTransform>(); trt.localScale = 0.01f * Vector3.one; trt.localRotation = Quaternion.identity; trt.anchorMin = new Vector2(0, 1); trt.anchorMax = new Vector2(0, 1); trt.pivot = new Vector2(1, 1); // top right? trt.sizeDelta = new Vector2(5, uiSlider.knobRadius * 2.0f * 100.0f); // size = 5, enough to hold the 0.00 float. float textPosRight = -2.0f * uiSlider.margin; // TMP: au pif pour le moment trt.localPosition = new Vector3(textPosRight, knobPosition.y, -0.002f); } UIUtils.SetRecursiveLayer(go, "CameraHidden"); }