using UnityEngine; using System; using System.Linq; using System.Collections.Generic; using UnityEngine.Assertions; #if UNITY_EDITOR using UnityEditor.SceneManagement; #endif namespace UnityEngine.ProBuilder { static class InternalUtility { // @todo public static T[] GetComponents(GameObject go) where T : Component { return go.transform.GetComponentsInChildren(); } // @todo public static T[] GetComponents(this IEnumerable transforms) where T : Component { List c = new List(); foreach (Transform t in transforms) c.AddRange(t.GetComponentsInChildren()); return c.ToArray() as T[]; } public static GameObject EmptyGameObjectWithTransform(Transform t) { GameObject go = new GameObject(); go.transform.localPosition = t.localPosition; go.transform.localRotation = t.localRotation; go.transform.localScale = t.localScale; #if UNITY_EDITOR StageUtility.PlaceGameObjectInCurrentStage(go); #endif return go; } public static GameObject MeshGameObjectWithTransform(string name, Transform t, Mesh mesh, Material mat, bool inheritParent) { GameObject go = InternalUtility.EmptyGameObjectWithTransform(t); go.name = name; go.AddComponent().sharedMesh = mesh; go.AddComponent().sharedMaterial = mat; go.hideFlags = HideFlags.HideAndDontSave; if (inheritParent) go.transform.SetParent(t.parent, false); return go; } public static T NextEnumValue(this T current) where T : IConvertible { Assert.IsTrue(current is Enum); var values = Enum.GetValues(current.GetType()); for (int i = 0, c = values.Length; i < c; i++) if (current.Equals(values.GetValue((i)))) return (T)values.GetValue((i + 1) % c); return current; } public static string ControlKeyString(char character) { if (character == PreferenceKeys.CMD_SUPER) return "Control"; else if (character == PreferenceKeys.CMD_SHIFT) return "Shift"; else if (character == PreferenceKeys.CMD_OPTION) return "Alt"; else if (character == PreferenceKeys.CMD_ALT) return "Alt"; else if (character == PreferenceKeys.CMD_DELETE) #if UNITY_EDITOR_WIN return "Backspace"; #else return "Delete"; #endif else return character.ToString(); } /** * Attempt to parse a color from string input. */ public static bool TryParseColor(string value, ref Color col) { string valid = "01234567890.,"; value = new string(value.Where(c => valid.Contains(c)).ToArray()); string[] rgba = value.Split(','); if (rgba.Length < 4) return false; try { float r = float.Parse(rgba[0]); float g = float.Parse(rgba[1]); float b = float.Parse(rgba[2]); float a = float.Parse(rgba[3]); col.r = r; col.g = g; col.b = b; col.a = a; } catch { return false; } return true; } #if !UNITY_2019_2_OR_NEWER public static bool TryGetComponent(this Component source, out T component) where T : Component { return (component = source.GetComponent()) != null; } public static bool TryGetComponent(this GameObject source, out T component) where T : Component { return (component = source.GetComponent()) != null; } #endif /// /// Get a reference to an existing component, or add a new component if one does not already exist. /// public static T DemandComponent(this Component component) where T : Component { return component.gameObject.DemandComponent(); } public static T DemandComponent(this GameObject gameObject) where T : Component { T component; if (!gameObject.TryGetComponent(out component)) component = gameObject.AddComponent(); return component; } } }