private void OnSceneGUI() { bool hasUIElementParent = HasUIElemParent(); UISpinner uiSpinner = target as UISpinner; Transform T = uiSpinner.transform; Vector3 posRight = T.TransformPoint(new Vector3(+uiSpinner.width, -uiSpinner.height / 2.0f, 0)); Vector3 posBottom = T.TransformPoint(new Vector3(uiSpinner.width / 2.0f, -uiSpinner.height, 0)); Vector3 posAnchor = T.TransformPoint(uiSpinner.Anchor); Vector3 posSeparation = T.TransformPoint(new Vector3(uiSpinner.margin + (uiSpinner.width - 2 * uiSpinner.margin) * uiSpinner.separationPositionPct, -uiSpinner.height / 2.0f, 0)); float handleSize = .3f * HandleUtility.GetHandleSize(posAnchor); Vector3 snap = Vector3.one * 0.01f; EditorGUI.BeginChangeCheck(); Handles.color = Handles.xAxisColor; Vector3 newTargetPosition_right = Handles.FreeMoveHandle(posRight, Quaternion.identity, handleSize, snap, Handles.SphereHandleCap); Handles.color = Handles.yAxisColor; Vector3 newTargetPosition_bottom = Handles.FreeMoveHandle(posBottom, Quaternion.identity, handleSize, snap, Handles.SphereHandleCap); Handles.color = Handles.zAxisColor; Vector3 newTargetPosition_anchor = Handles.FreeMoveHandle(posAnchor, Quaternion.identity, handleSize, snap, Handles.SphereHandleCap); Handles.color = new Color(0.8f, 0.4f, 0.1f); Vector3 newTargetPosition_separation = Handles.FreeMoveHandle(posSeparation, Quaternion.identity, handleSize, snap, Handles.CubeHandleCap); if (EditorGUI.EndChangeCheck()) { Undo.RecordObject(target, "Change Dimensions"); Vector3 deltaRight = newTargetPosition_right - posRight; Vector3 deltaBottom = newTargetPosition_bottom - posBottom; Vector3 deltaAnchor = newTargetPosition_anchor - posAnchor; Vector3 deltaSeparation = newTargetPosition_separation - posSeparation; if (Vector3.SqrMagnitude(deltaRight) > Mathf.Epsilon) { uiSpinner.Width += deltaRight.x; } else if (Vector3.SqrMagnitude(deltaBottom) > Mathf.Epsilon) { uiSpinner.Height += -deltaBottom.y; } else if (Vector3.SqrMagnitude(deltaSeparation) > Mathf.Epsilon) { uiSpinner.separationPositionPct += deltaSeparation.x; } else if (Vector3.SqrMagnitude(deltaAnchor) > Mathf.Epsilon) { Vector3 localDeltaAnchor = T.InverseTransformVector(deltaAnchor); uiSpinner.RelativeLocation += new Vector3(localDeltaAnchor.x, localDeltaAnchor.y, 0.0f); } } }
public static void OnCreateFromHierarchy() { Transform parent = null; Transform T = UnityEditor.Selection.activeTransform; if (T != null) { parent = T; } UISpinner.Create(new UISpinner.CreateArgs { parent = parent }); }
public static UISpinner 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; } } UISpinner uiSpinner = go.AddComponent <UISpinner>(); // NOTE: also creates the MeshFilter, MeshRenderer and Collider components uiSpinner.relativeLocation = input.relativeLocation; uiSpinner.transform.parent = input.parent; uiSpinner.transform.localPosition = parentAnchor + input.relativeLocation; uiSpinner.transform.localRotation = Quaternion.identity; uiSpinner.transform.localScale = Vector3.one; uiSpinner.width = input.width; uiSpinner.height = input.height; uiSpinner.margin = input.margin; uiSpinner.thickness = input.thickness; uiSpinner.separationPositionPct = input.spinner_separation_pct; uiSpinner.textAndValueVisibilityType = input.visibility_type; uiSpinner.spinnerValueType = input.value_type; uiSpinner.minValue = input.min_spinner_value; uiSpinner.maxValue = input.max_spinner_value; uiSpinner.currentValue = input.cur_spinner_value; uiSpinner.valueRate = input.spinner_value_rate; uiSpinner.valueRateRay = input.spinner_value_rate_ray; uiSpinner.textContent = input.caption; uiSpinner.baseColor.useConstant = false; uiSpinner.baseColor.reference = input.background_color; uiSpinner.textColor.useConstant = false; uiSpinner.textColor.reference = input.textColor; uiSpinner.pushedColor.useConstant = false; uiSpinner.pushedColor.reference = input.pushedColor; uiSpinner.selectedColor.useConstant = false; uiSpinner.selectedColor.reference = input.selectedColor; uiSpinner.sourceMaterial = input.background_material; // Setup the Meshfilter MeshFilter meshFilter = go.GetComponent <MeshFilter>(); if (meshFilter != null) { meshFilter.sharedMesh = UIUtils.BuildRoundedBox(input.width, input.height, input.margin, input.thickness); uiSpinner.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.background_material != null) { // Clone the material. meshRenderer.sharedMaterial = Instantiate(input.background_material); meshRenderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; meshRenderer.renderingLayerMask = 2; // "LightLayer 1" uiSpinner.SetColor(input.background_color.value); } // // CANVAS (to hold the 2 texts) // GameObject canvas = new GameObject("Canvas"); canvas.transform.parent = uiSpinner.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(uiSpinner.width, uiSpinner.height); rt.localPosition = Vector3.zero; CanvasScaler cs = canvas.AddComponent <CanvasScaler>(); cs.dynamicPixelsPerUnit = 300; // 300 dpi, sharp font cs.referencePixelsPerUnit = 100; // default? bool hasText = (input.visibility_type == TextAndValueVisibilityType.ShowTextAndValue); // Add a Text under the Canvas { GameObject text = new GameObject("Text"); text.transform.parent = canvas.transform; TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>(); t.text = input.caption; t.enableAutoSizing = true; t.fontSizeMin = 1; t.fontSizeMax = 500; t.fontStyle = FontStyles.Normal; t.alignment = TextAlignmentOptions.Left; 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(0, 1); // top left trt.sizeDelta = new Vector2( (uiSpinner.width - 2 * uiSpinner.margin) * uiSpinner.separationPositionPct * 100.0f, (uiSpinner.height - 2 * uiSpinner.margin) * 100.0f); float textPosLeft = uiSpinner.margin; trt.localPosition = new Vector3(textPosLeft, -uiSpinner.margin, -0.002f); // hide if ValueOnly text.SetActive(hasText); } // Text VALUE { GameObject text = new GameObject("TextValue"); text.transform.parent = canvas.transform; TextMeshProUGUI t = text.AddComponent <TextMeshProUGUI>(); t.text = (input.value_type == SpinnerValueType.Float) ? input.cur_spinner_value.ToString(default_value_format) : Mathf.RoundToInt(input.cur_spinner_value).ToString(); t.enableAutoSizing = true; t.fontSizeMin = 1; t.fontSizeMax = 500; t.fontStyle = FontStyles.Normal; t.alignment = hasText ? TextAlignmentOptions.Right : TextAlignmentOptions.Center; 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 = hasText ? new Vector2( (uiSpinner.width - 2 * uiSpinner.margin) * (1 - uiSpinner.separationPositionPct) * 100.0f, (uiSpinner.height - 2 * uiSpinner.margin) * 100.0f) : new Vector2( (uiSpinner.width - 2 * uiSpinner.margin) * 100.0f, (uiSpinner.height - 2 * uiSpinner.margin) * 100.0f); float textPos = hasText ? uiSpinner.width - uiSpinner.margin // right : uiSpinner.width - uiSpinner.margin; // or middle trt.localPosition = new Vector3(textPos, -uiSpinner.margin, -0.002f); } UIUtils.SetRecursiveLayer(go, "CameraHidden"); return(uiSpinner); }
public static ShotItem GenerateShotItem(Shot shot) { GameObject root = new GameObject("shotItem"); ShotItem shotItem = root.AddComponent <ShotItem>(); root.layer = LayerMask.NameToLayer("CameraHidden"); // Set the item invisible in order to hide it while it is not added into // a list. We will activate it after it is added root.transform.localScale = Vector3.zero; float cx = 0.0f; // // ACTIVE CAMERA Button // UILabel currentShotLabel = UILabel.Create(new UILabel.CreateLabelParams { parent = root.transform, widgetName = "CurrentShotLabel", relativeLocation = new Vector3(0, 0, -UIButton.default_thickness), width = 0.01f, height = 0.03f, margin = 0.001f, material = UIUtils.LoadMaterial("UIBase"), selectedColor = UIOptions.FocusColorVar, caption = "", }); currentShotLabel.SetLightLayer(3); cx += 0.01f; // // ENABLE Checkbox // UICheckbox shotEnabledCheckbox = UICheckbox.Create(new UICheckbox.CreateParams { parent = root.transform, widgetName = "ShotEnabledCheckbox", relativeLocation = new Vector3(cx, 0, -UICheckbox.default_thickness), width = 0.03f, height = 0.03f, content = UICheckbox.CheckboxContent.CheckboxOnly, margin = 0.001f, material = UIUtils.LoadMaterial("UIBase"), }); shotEnabledCheckbox.SetLightLayer(3); cx += 0.03f; // // SHOT NAME Label // UILabel shotNameLabel = UILabel.Create(new UILabel.CreateLabelParams { parent = root.transform, widgetName = "ShotNameLabel", relativeLocation = new Vector3(cx, 0, -UIButton.default_thickness), width = 0.15f, height = 0.020f, margin = 0.001f, material = UIUtils.LoadMaterial("UIBase"), }); shotNameLabel.SetLightLayer(3); UIUtils.SetTMProStyle(shotNameLabel.gameObject); // // CAMERA NAME Label // UILabel cameraNameLabel = UILabel.Create(new UILabel.CreateLabelParams { parent = root.transform, widgetName = "CameraNameLabel", relativeLocation = new Vector3(cx, -0.020f, -UIButton.default_thickness), width = 0.15f, height = 0.01f, margin = 0.001f, fgcolor = UIOptions.Instance.attenuatedTextColor, material = UIUtils.LoadMaterial("UIBase"), }); cameraNameLabel.SetLightLayer(3); UIUtils.SetTMProStyle(cameraNameLabel.gameObject, alignment: TextAlignmentOptions.BottomRight); cx += 0.15f; // Start frame button UIButton startFrameButton = UIButton.Create(new UIButton.CreateButtonParams { parent = root.transform, widgetName = "StartFrameButton", relativeLocation = new Vector3(cx, 0, -UIButton.default_thickness), width = 0.02f, height = 0.03f, icon = UIUtils.LoadIcon("next"), buttonContent = UIButton.ButtonContent.ImageOnly, margin = 0.001f, }); startFrameButton.SetLightLayer(3); cx += 0.02f; // START: Add UISpinner UISpinner startFrameSpinner = UISpinner.Create(new UISpinner.CreateArgs { parent = root.transform, widgetName = "StartFrame", relativeLocation = new Vector3(cx, 0, -UISpinner.default_thickness), width = 0.055f, height = 0.03f, visibility_type = UISpinner.TextAndValueVisibilityType.ShowValueOnly, value_type = UISpinner.SpinnerValueType.Int, min_spinner_value = 0, max_spinner_value = 10000, cur_spinner_value = shot.start, spinner_value_rate = 30, spinner_value_rate_ray = 30, margin = 0.001f, }); startFrameSpinner.baseColor.useConstant = true; startFrameSpinner.baseColor.constant = UIOptions.BackgroundColor; startFrameSpinner.selectedColor.useConstant = true; startFrameSpinner.selectedColor.constant = UIOptions.SelectedColor; startFrameSpinner.SetLightLayer(3); UIUtils.SetTMProStyle(startFrameSpinner.gameObject); cx += 0.055f; // END: Add UISpinner UISpinner endFrameSpinner = UISpinner.Create(new UISpinner.CreateArgs { parent = root.transform, widgetName = "EndFrame", relativeLocation = new Vector3(cx, 0, -UISpinner.default_thickness), width = 0.055f, height = 0.03f, visibility_type = UISpinner.TextAndValueVisibilityType.ShowValueOnly, value_type = UISpinner.SpinnerValueType.Int, min_spinner_value = 0, max_spinner_value = 10000, cur_spinner_value = shot.end, spinner_value_rate = 30, spinner_value_rate_ray = 30, margin = 0.001f, }); endFrameSpinner.baseColor.useConstant = true; endFrameSpinner.baseColor.constant = UIOptions.BackgroundColor; endFrameSpinner.selectedColor.useConstant = true; endFrameSpinner.selectedColor.constant = UIOptions.SelectedColor; endFrameSpinner.SetLightLayer(3); UIUtils.SetTMProStyle(endFrameSpinner.gameObject); cx += 0.055f; // End frame button UIButton endFrameButton = UIButton.Create(new UIButton.CreateButtonParams { parent = root.transform, widgetName = "EndFrameButton", relativeLocation = new Vector3(cx, 0, -UIButton.default_thickness), width = 0.02f, height = 0.03f, icon = UIUtils.LoadIcon("prev"), buttonContent = UIButton.ButtonContent.ImageOnly, margin = 0.001f, }); endFrameButton.SetLightLayer(3); cx += 0.02f; // RANGE: Add UILabel UILabel frameRangeLabel = UILabel.Create(new UILabel.CreateLabelParams { parent = root.transform, widgetName = "FrameRange", relativeLocation = new Vector3(cx, 0, -UILabel.default_thickness), width = 0.04f, height = 0.03f, margin = 0.001f, material = UIUtils.LoadMaterial("UIBase"), }); frameRangeLabel.baseColor.useConstant = true; frameRangeLabel.baseColor.constant = UIOptions.BackgroundColor; frameRangeLabel.selectedColor.useConstant = true; frameRangeLabel.selectedColor.constant = UIOptions.SelectedColor; frameRangeLabel.SetLightLayer(3); UIUtils.SetTMProStyle(frameRangeLabel.gameObject, alignment: TextAlignmentOptions.Center); cx += 0.04f; // Set camera UIButton setCameraButton = UIButton.Create(new UIButton.CreateButtonParams { parent = root.transform, widgetName = "SetCameraButton", relativeLocation = new Vector3(cx, 0, -UIButton.default_thickness), width = 0.03f, height = 0.03f, icon = UIUtils.LoadIcon("icon-camera"), buttonContent = UIButton.ButtonContent.ImageOnly, margin = 0.001f, }); setCameraButton.isCheckable = true; setCameraButton.checkedSprite = UIUtils.LoadIcon("icon-camera"); setCameraButton.checkedColor.useConstant = false; setCameraButton.checkedColor.constant = UIOptions.FocusColor; setCameraButton.checkedColor.reference = UIOptions.FocusColorVar; setCameraButton.baseSprite = UIUtils.LoadIcon("icon-camera"); setCameraButton.SetLightLayer(3); // Link widgets to the item script. shotItem.currentShotLabel = currentShotLabel; shotItem.shotEnabledCheckbox = shotEnabledCheckbox; shotItem.shotNameLabel = shotNameLabel; shotItem.cameraNameLabel = cameraNameLabel; shotItem.startFrameSpinner = startFrameSpinner; shotItem.startFrameButton = startFrameButton; shotItem.frameRangeLabel = frameRangeLabel; shotItem.endFrameSpinner = endFrameSpinner; shotItem.endFrameButton = endFrameButton; shotItem.setCameraButton = setCameraButton; shotItem.SetShot(shot); return(shotItem); }
private void OnSetStartEndFromCurrentFrame(UnityAction <Shot, float> action, UISpinner spinner) { spinner.IntValue = GlobalState.Animation.CurrentFrame; action.Invoke(shot, GlobalState.Animation.CurrentFrame); UpdateShotRange(0); // 0: unused }