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 = ""; } } } }
/** * For each side, show its data manipulation UI. */ private void showDataSideManipulationUI(int pSideIndex, DieSideMatchInfo pDieSideMatchInfo) { DieSides dieSides = target as DieSides; Color defaultColor = GUI.backgroundColor; EditorGUILayout.BeginHorizontal(); if (_highlightedIndex == pSideIndex) GUI.backgroundColor = Color.yellow; string sideLabel = "Side " + pSideIndex + " = "; _sideLabelStyle.normal.textColor = Color.black; if (pDieSideMatchInfo != null && pDieSideMatchInfo.closestMatch == dieSides.GetDieSide(pSideIndex)) { _sideLabelStyle.normal.textColor = pDieSideMatchInfo.isExactMatch ? darkGreen : darkBlue; } EditorGUILayout.LabelField(sideLabel, _sideLabelStyle, _sideLabelOptions); //show entry fields for all entries in the data array SerializedProperty dieSide = _dieSides.GetArrayElementAtIndex(pSideIndex); SerializedProperty dataArray = dieSide.FindPropertyRelative(_dieSideValuesPropertyName); EditorGUI.BeginChangeCheck(); SerializedProperty element; for (int i = 0; i < dataArray.arraySize; i++) { element = dataArray.GetArrayElementAtIndex(i); //set the next controlname to sideLabel so we can focus on it GUI.SetNextControlName(pSideIndex+"_"+i); //we have to do it like this unfortunately becauses the focus mechanism doesn't //work well with EditorGUILayout.IntField //string value = GUILayout.TextField("" + element.intValue, _dataValueOptions); //int intValue; //int.TryParse(value, out intValue); //element.intValue = intValue; element.intValue = EditorGUILayout.IntField(element.intValue, _dataValueOptions); } if (EditorGUI.EndChangeCheck()) { dieSides.GetDieSide(pSideIndex).DirtyStringCache(); } //show a toggle button for rotating and highlight //and update our highlight index accordingly if pressed bool currentHighLight = _highlightedIndex == pSideIndex; bool newHighLight = GUILayout.Toggle(_highlightedIndex == pSideIndex, _rotateButtonContent, "Button", _smallEditorButtonOptions); if (currentHighLight != newHighLight) { _highlightedIndex = (_highlightedIndex == pSideIndex) ? -1 : pSideIndex; if (_highlightedIndex != -1) { EditorGUI.FocusTextInControl(_highlightedIndex + "_0"); //GUI.FocusControl(_highlightedIndex + "_0"); } //rotate our die so that this value/side becomes activated dieSides.transform.rotation = dieSides.GetWorldRotationFor(pSideIndex); if (!Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene()); } EditorGUILayout.EndHorizontal(); GUI.backgroundColor = defaultColor; }