void Update() { //if the component is not active, hide us if (_dieSides == null || !_dieSides.gameObject.activeSelf) { _canvasGroup.alpha = 0; return; } else { _canvasGroup.alpha = 1; } //update canvas position and rotation transform.position = _dieSides.transform.position + Vector3.up * _distanceFromTarget; if (Application.isPlaying) { transform.rotation = Quaternion.LookRotation(_camera.transform.forward); } //if no closestHit is available it means we have no die sides at all DieSideMatchInfo dieSideMatchInfo = _dieSides.GetDieSideMatchInfo(); if (dieSideMatchInfo.closestMatch == null) { _text.text = "Please use the\nDieSidesEditor first\nto find all sides."; } else { //set basic info about the current die _text.text = "Die type:" + _dieSides.dieSideCount + "\nClosest match:" + dieSideMatchInfo.closestMatch.ValuesAsString() + "\nExact match?:" + (dieSideMatchInfo.isExactMatch ? "<color=green>YES</color>" : "<color=blue>NO</color>"); //add die specific info if available if (_die != null) { _text.text += "\nIs rolling?:" + _die.isRolling + "\nLast event:" + _lastDieEvent; } } //fade out this world canvas as we get closer and closer to the camera float camDistance = (_camera.transform.position - transform.position).magnitude; if (camDistance > _fadeStartDistance) { return; } float fadeDistance = _fadeStartDistance - _fadeEndDistance; float currentDistance = camDistance - _fadeEndDistance; float alpha = Mathf.Clamp(currentDistance / fadeDistance, 0, 1); _canvasGroup.alpha = alpha; }
/** * Draws debug info about the selected die: normals & values. */ void OnSceneGUI() { if (Selection.gameObjects.Length > 1 || !_debugViewOn) return; DieSides dieSides = target as DieSides; if (dieSides != null && dieSides.dieSideCount > 0) { //get the closest side match for the current orientation so we //can apply pretty colors DieSideMatchInfo dieSideMatchInfo = dieSides.GetDieSideMatchInfo(); DieSide current = dieSideMatchInfo.closestMatch; for (int i = 0; i < dieSides.dieSideCount; i++) { if (_highlightedIndex != -1 && _highlightedIndex != i) continue; DieSide dieSide = dieSides.GetDieSide(i); Vector3 worldPos = dieSides.transform.TransformPoint(dieSide.centerPoint); Vector3 worldNormal = dieSides.transform.TransformDirection(dieSide.normal); Vector3 endPos = worldPos + worldNormal * _labelDrawDistance; Color color = Color.green; Color labelColor = _labelDrawColor; //should we apply our regular red/green color scheme or should we make this //element blink because we selected it? if (_highlightedIndex == i && Math.Floor(Time.realtimeSinceStartup * 5) % 2 == 0) { color = Color.yellow; labelColor = Color.yellow; } else { color = dieSide == current ? Color.green : Color.red; } GUIStyle guiStyle = new GUIStyle(); guiStyle.fontSize = 20; guiStyle.normal.textColor = labelColor; Handles.zTest = UnityEngine.Rendering.CompareFunction.LessEqual; Handles.color = color; Handles.DrawLine(worldPos, endPos); Handles.Label(endPos, "[" + i + "]=" + dieSide.ValuesAsString(), guiStyle); } } SceneView.RepaintAll(); }
/** * Show the UI that allows you to manipulate the data from the found sides or clear all data. */ private void showDataSidesManipulationUI() { _sideDataDisplayed = EditorGUILayout.Foldout( _sideDataDisplayed, //(_sideDataDisplayed ? "Hide value map" : "Show value map") + "Die sides value map" + " (" + _dieSides.arraySize + " sides)", _boldFoldout ); if (_sideDataDisplayed) { DieSides dieSides = target as DieSides; DieSideMatchInfo dieSideMatchInfo = dieSides.GetDieSideMatchInfo(); for (int i = 0; i < _dieSides.arraySize; i++) { showDataSideManipulationUI(i, dieSideMatchInfo); } //this little part allows us to change the highlight while we are tabbing //highlight is only updated if we were already highlighting if (_highlightedIndex > -1) { int currentFocus = -1; string nameOfFocusedControl = GUI.GetNameOfFocusedControl() ?? "x_x"; bool wasParseable = int.TryParse(nameOfFocusedControl.Split('_')[0], out currentFocus); if (wasParseable && _highlightedIndex != currentFocus) { _highlightedIndex = currentFocus; dieSides.transform.rotation = dieSides.GetWorldRotationFor(_highlightedIndex); } } showDataValueCountUpdateUI(); if (GUILayout.Button("Clear sides")) { if (EditorUtility.DisplayDialog("Warning", "This will delete all sides and their data. Are you sure?", "Yes", "No")) { _dieSides.arraySize = 0; _valueCountPerSide.intValue = 1; _showGenerationDebugData = false; _generationDebugData = ""; } } } }