public static string[] GuidsToAssetPaths(string[] guids) { int count = guids.Length; var result = ArrayPool <string> .Create(count); for (int n = count - 1; n >= 0; n--) { result[n] = AssetDatabase.GUIDToAssetPath(guids[n]); } return(result); }
public override Object CreateInstance(Type type) { if (type.IsScriptableObject()) { return(ScriptableObject.CreateInstance(type)); } try { #if DEV_MODE Debug.Log("Activator.CreateInstance(" + StringUtils.ToString(type) + ")"); #endif return(Activator.CreateInstance(type) as Object); } #if DEV_MODE && DEBUG_DEFAULT_VALUE_EXCEPTIONS catch (Exception e) { Debug.LogWarning(e); } #else catch { } #endif try { var constructor = type.GetConstructors()[0]; var parameters = constructor.GetParameters(); int count = parameters.Length; var parameterValues = ArrayPool <object> .Create(count); for (int n = 0; n < count; n++) { parameterValues[n] = parameters[n].DefaultValue(); #if DEV_MODE Debug.Log(StringUtils.ToString(type) + " parameterValues[" + n + "] = " + StringUtils.ToString(parameterValues[n]) + ")"); #endif } return(Activator.CreateInstance(type, parameterValues) as Object); } #if DEV_MODE && DEBUG_DEFAULT_VALUE_EXCEPTIONS catch (Exception e) { Debug.LogWarning(e); } #else catch { } #endif try { return(FormatterServices.GetUninitializedObject(type) as Object); } #if DEV_MODE && DEBUG_DEFAULT_VALUE_EXCEPTIONS catch (Exception e) { Debug.LogWarning(e); } #else catch { } #endif throw new Exception("CreateInstance failed for UnityObject of type " + StringUtils.ToString(type)); }
public static Object[] LoadAssetsAtPath(string[] paths) { int count = paths.Length; var result = ArrayPool <Object> .Create(count); for (int n = count - 1; n >= 0; n--) { result[n] = AssetDatabase.LoadAssetAtPath(paths[n], Types.UnityObject); } return(result); }
/// <summary> Creates a new instance of the drawer or returns a reusable instance from the pool. </summary> /// <param name="parent"> The parent drawers of the created drawers. Can be null. </param> /// <param name="inspector"> The inspector in which the IDrawer are contained. Can not be null. </param> /// <returns> The drawer instance, ready to be used. </returns> public static MissingScriptDrawer Create([NotNull] IParentDrawer parent, [NotNull] IInspector inspector) { MissingScriptDrawer result; if (!DrawerPool.TryGet(out result)) { result = new MissingScriptDrawer(); } result.Setup(ArrayPool <Component> .Create(1), parent, null, inspector); result.LateSetup(); return(result); }
public static Object[] LoadAssetsByGuids(string[] guids) { int count = guids.Length; var result = ArrayPool <Object> .Create(count); for (int n = count - 1; n >= 0; n--) { var path = AssetDatabase.GUIDToAssetPath(guids[n]); result[n] = AssetDatabase.LoadAssetAtPath(path, Types.UnityObject); } return(result); }
public static void ResetWithoutAffectingChildren([NotNull] this Transform transform) { int childCount = transform.childCount; if (childCount == 0) { transform.localPosition = Vector3.zero; transform.localEulerAngles = Vector3.zero; transform.localScale = Vector3.one; return; } var rectTransform = transform as RectTransform; if (rectTransform != null) { rectTransform.ResetWithoutAffectingChildren(); return; } #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(!(transform is RectTransform)); #endif var temp = (new GameObject()).transform; temp.parent = transform.parent; temp.localPosition = transform.localPosition; temp.localEulerAngles = transform.localEulerAngles; temp.localScale = transform.localScale; var children = ArrayPool <Transform> .Create(childCount); for (int n = childCount - 1; n >= 0; n--) { var child = transform.GetChild(n); children[n] = transform.GetChild(n); child.parent = temp; } transform.localPosition = Vector3.zero; transform.localEulerAngles = Vector3.zero; transform.localScale = Vector3.one; for (int n = childCount - 1; n >= 0; n--) { var child = temp.GetChild(n); child.parent = transform; } ArrayPool <Transform> .Dispose(ref children); Platform.Active.Destroy(temp.gameObject); }
public static GameObject[] GameObjects([NotNull] this Component[] targets) { int count = targets.Length; var result = ArrayPool <GameObject> .Create(count); for (int n = count - 1; n >= 0; n--) { var target = targets[n]; result[n] = target == null ? null : target.gameObject; } return(result); }
/// <summary> Dock EditorWindow as tab on the parent host view of the existing EditorWindow. </summary> /// <param name="window"> The window whose host view to dock on. </param> /// <param name="tab"> The EditorWindow to dock. </param> public static void AddTab([NotNull] this EditorWindow window, EditorWindow tab) { var dockArea = window.ParentHostView(); if (dockArea == null) { #if DEV_MODE Debug.LogWarning("m_Parent of target window " + window.GetType().Name + " (\"" + window.name + "\") was null; can't add " + tab.GetType().Name + " (\"" + tab.name + "\") as tab. You might want to revert Layout to factory settings."); #endif return; } var dockAreaType = Types.GetInternalEditorType("UnityEditor.DockArea"); var parentHostViewType = dockArea.GetType(); if (parentHostViewType != dockAreaType && !parentHostViewType.IsSubclassOf(dockAreaType)) { #if DEV_MODE Debug.LogWarning("m_Parent of target window " + window.GetType().Name + " (\"" + window.name + "\") was not a DockArea (but " + parentHostViewType.Name + "); can't add " + tab.GetType().Name + " (\"" + tab.name + "\") as tab."); #endif return; } #if UNITY_2018_3_OR_NEWER var types = ArrayPool <Type> .Create(2); types[1] = Types.Bool; #else var types = ArrayPool <Type> .Create(1); #endif types[0] = typeof(EditorWindow); var addTabMethod = dockAreaType.GetMethod("AddTab", BindingFlags.Public | BindingFlags.Instance, null, CallingConventions.Any, types, null); ArrayPool <Type> .Dispose(ref types); #if UNITY_2018_3_OR_NEWER var parameters = ArrayPool <object> .Create(2); parameters[1] = true; #else var parameters = ArrayPool <object> .Create(1); #endif parameters[0] = tab; addTabMethod.Invoke(dockArea, parameters); }
/// <summary> /// The smaller the result, the closer the match /// </summary> public static int FuzzyCompare(FuzzyComparable searchString, FuzzyComparable testAgainst) { int sCount = searchString.splitPoints.Length; int finalResult = 0; int[] sCompare = ArrayPool <int> .Create(0); int sFrom = 0; int sTo; int wordResult; for (int s = 0; s < sCount; s++) { sTo = searchString.splitPoints[s]; searchString.GetCompareSubsection(sFrom, sTo, ref sCompare); #if DEV_MODE //I think this can happen when a string starts with a space, ends with a space, or has multiple spaces in a row Debug.Assert(sCompare.Length > 0, "sCompare was empty with sFrom=" + sFrom + ", sTo=" + sTo + ", searchString=\"" + searchString.text + "\""); #endif wordResult = FuzzyCompareWord(sCompare, testAgainst); finalResult += wordResult; sFrom = sTo + 1; } sTo = searchString.Length; searchString.GetCompareSubsection(sFrom, sTo, ref sCompare); wordResult = FuzzyCompareWord(sCompare, testAgainst); finalResult += wordResult; finalResult /= sCount + 1; #if DEV_MODE && DEBUG_RESULT debugWords.Sort(); debugResults.Add("----------FINAL RESULT: " + finalResult.ToString("D4") + " \"" + testAgainst.Text + "\".FuzzyCompare(\"" + searchString.Text + "\")----------\n" + string.Join("\n", debugWords.ToArray())); debugWords.Clear(); #endif ArrayPool <int> .Dispose(ref sCompare); return(finalResult); }
public static void ResetWithoutAffectingChildren([NotNull] this RectTransform transform) { int childCount = transform.childCount; if (childCount == 0) { transform.localPosition = Vector3.zero; transform.localEulerAngles = Vector3.zero; transform.localScale = Vector3.one; return; } var temp = (new GameObject("", typeof(RectTransform))).transform; temp.parent = transform.parent; temp.localPosition = transform.localPosition; temp.localEulerAngles = transform.localEulerAngles; temp.localScale = transform.localScale; var children = ArrayPool <Transform> .Create(childCount); for (int n = childCount - 1; n >= 0; n--) { var child = transform.GetChild(n); children[n] = transform.GetChild(n); child.parent = temp; } transform.localPosition = Vector3.zero; transform.localEulerAngles = Vector3.zero; transform.localScale = Vector3.one; for (int n = childCount - 1; n >= 0; n--) { var child = temp.GetChild(n); child.parent = transform; } ArrayPool <Transform> .Dispose(ref children); Platform.Active.Destroy(temp.gameObject); }
public static string CharInArrayToString(int[] ints) { int count = ints.Length; var chars = ArrayPool <char> .Create(count); for (int n = 0; n < count; n++) { //chars[n] = (char)ints[n]; //new test - maybe temp char c = (char)ints[n]; #if DEV_MODE Debug.Assert(c != '\0', "ints[" + n + "] / " + n + " was null!"); #endif chars[n] = c == '\0' ? '~' : c; } var result = new string(chars); ArrayPool <char> .Dispose(ref chars); return(result); }
public TypeId(Type type) { if (type.IsGenericType) { baseId = TypeExtensions.GetTypeId(type.GetGenericTypeDefinition()); var genericTypes = type.GetGenericArguments(); int genericTypeCount = genericTypes.Length; genericTypeIds = ArrayPool <TypeId> .Create(genericTypeCount); for (int n = 0; n < genericTypeCount; n++) { genericTypeIds[n] = new TypeId(genericTypes[n]); } } else { baseId = TypeExtensions.GetTypeId(type); genericTypeIds = ArrayPool <TypeId> .ZeroSizeArray; } }
public static LinkedMemberInfo Deserialize(byte[] bytes) { #if DEV_MODE && PI_ASSERTATIONS UnityEngine.Debug.Assert(bytes != null); #endif using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); stream.Write(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); SerializableMemberInfo deserialized; deserialized = formatter.Deserialize(stream) as SerializableMemberInfo; int count = deserialized.targetReferences.Length; var targets = ArrayPool <Object> .Create(count); for (int n = count - 1; n >= 0; n--) { targets[n] = ObjectIds.GetTarget(deserialized.targetReferences[n]); } var hierarchy = LinkedMemberHierarchy.Get(targets); LinkedMemberInfo result = null; var memberStack = deserialized.memberStack; // TO DO: serialize parent info better for more reliable fetching for (int n = memberStack.Count - 1; n >= 0; n--) { var member = memberStack.Pop(); result = member.Deserialize(hierarchy); } return(result); } }
private static void OpenContextMenu() { #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(openingMenu != null); #endif if (OnMenuOpening != null) { OnMenuOpening(openingMenu, openingMenuInspector, openingMenuSubject.Target); } // new test if (!ApplicationUtility.HasFocus) { #if DEV_MODE Debug.LogWarning("Aborting display of context menu because Unity application does not have focus any longer."); #endif if (disposeMenuAfterOpening) { openingMenu.Dispose(); } openingMenu = null; if (onMenuClosed != null) { var invoke = onMenuClosed; onMenuClosed = null; invoke(openingMenuPart); } return; } var genericMenu = new GenericMenu(); if (openingMenuSubject.HasValidInstanceReference()) { var linkedMember = openingMenuSubject.Target.MemberInfo; if (linkedMember != null) { var serializedProperty = linkedMember.SerializedProperty; if (serializedProperty != null) { var editorGUI = typeof(EditorGUI); var addUnityInternalPropertyMenuItems = editorGUI.GetMethod("FillPropertyContextMenu", BindingFlags.Static | BindingFlags.NonPublic); if (addUnityInternalPropertyMenuItems != null) { var parameters = ArrayPool <object> .Create(3); parameters[0] = serializedProperty; parameters[1] = null; parameters[2] = genericMenu; addUnityInternalPropertyMenuItems.Invoke(null, parameters); } #if DEV_MODE else { Debug.LogError("Could not find method EditorGUI.FillPropertyContextMenu!"); } #endif } } } openingMenu.AddToGenericMenu(ref genericMenu, false); if (disposeMenuAfterOpening) { openingMenu.Dispose(); } openingMenu = null; if (openingMenuPosition.HasValue) { var openMenuAtPosition = openingMenuPosition.Value; openingMenuPosition = null; var openAtScreenPoint = openMenuAtPosition.position; var openAtLocalPoint = GUIUtility.ScreenToGUIPoint(openAtScreenPoint); openMenuAtPosition.position = openAtLocalPoint; #if DEV_MODE && DEBUG_OPEN_MENU Debug.Log("Opening context menu @ " + openMenuAtPosition + " now."); #endif genericMenu.DropDown(openMenuAtPosition); } else { #if DEV_MODE && DEBUG_OPEN_MENU Debug.Log("Opening context menu at cursor position now."); #endif genericMenu.ShowAsContext(); } if (onMenuClosed != null) { var invoke = onMenuClosed; onMenuClosed = null; invoke(openingMenuPart); } }
/// <summary> /// The smaller the result, the closer the match /// </summary> public static int FuzzyCompareWord(int[] sCompare, FuzzyComparable testAgainst) { #if DEV_MODE && DEBUG_WORD var debugSteps = ""; #endif int tCount = testAgainst.splitPoints.Length; int stepResult; int[] tCompare = ArrayPool <int> .Create(0); int tFrom = 0; int tTo; int wordResult = int.MaxValue; for (int t = 0; t < tCount; t++) { tTo = testAgainst.splitPoints[t]; if (tTo > tFrom) { testAgainst.GetCompareSubsection(tFrom, tTo, ref tCompare); #if DEV_MODE //I think this can happen when a string starts with a space, ends with a space, or has multiple spaces in a row Debug.Assert(tCompare.Length > 0, "tCompare was empty with tFrom=" + tFrom + ", tTo=" + tTo + ", testAgainst=\"" + testAgainst.text + "\" (WORD NOW: " + wordResult + ")"); #endif if (tCompare.Length > 0) { stepResult = FuzzyCompare(sCompare, tCompare); if (stepResult < wordResult) { wordResult = stepResult; } } #if DEV_MODE && DEBUG_WORD else { stepResult = int.MaxValue; } debugSteps += "\n\"" + testAgainst.Text + "\".GetCompareSubsection(\"" + CharInArrayToString(sCompare) + "\") vs \"" + CharInArrayToString(tCompare) + "\" STEP: " + stepResult + " (WORD NOW: " + wordResult + ")"; #endif } tFrom = tTo + 1; } tTo = testAgainst.Length; testAgainst.GetCompareSubsection(tFrom, tTo, ref tCompare); stepResult = FuzzyCompare(sCompare, tCompare); if (stepResult < wordResult) { wordResult = stepResult; } #if DEV_MODE && DEBUG_WORD debugSteps += "\n\"" + testAgainst.Text + "\".GetCompareSubsection(\"" + CharInArrayToString(sCompare) + "\") vs \"" + CharInArrayToString(tCompare) + "\" STEP: " + stepResult + " (WORD NOW: " + wordResult + ")"; debugWords.Add("\"" + testAgainst.Text + "\" WORD RESULT: " + wordResult + " for word " + CharInArrayToString(sCompare) + debugSteps); #endif ArrayPool <int> .Dispose(ref tCompare); return(wordResult); }
/// <summary> /// Minimized the target window. If window is already minimized, nothing happens. /// /// Currently minimizing is only supported if EditorWindow is not docked, or it is docked /// in a split view with another window in such a way that the minimized window resides /// below the other window. /// </summary> public void Minimize() { if (minimized) { return; } positionBeforeMinimize = window.position; minSizeBeforeMinimize = window.minSize; maxSizeBeforeMinimize = window.maxSize; splitViewIndexBeforeMinimize = -1; var dockArea = window.ParentHostView(); if (dockArea == null) { var setPosition = window.position; setPosition.height = 11f; window.position = setPosition; window.minSize = new Vector2(window.minSize.x, 11f); window.maxSize = new Vector2(window.maxSize.x, 11f); minimized = true; } else { var parentProperty = dockArea.GetType().GetProperty("parent", BindingFlags.Instance | BindingFlags.Public); if (parentProperty == null) { #if DEV_MODE Debug.LogError("Minimize: parent property not found."); #endif return; } var splitView = parentProperty.GetValue(dockArea, null); if (splitView != null && string.Equals(splitView.GetType().Name, "SplitView")) { var splitStateField = splitView.GetType().GetField("splitState", BindingFlags.Instance | BindingFlags.NonPublic); if (splitStateField == null) { #if DEV_MODE Debug.LogError("Minimize: splitState field not found."); #endif return; } var splitState = splitStateField.GetValue(splitView); if (splitState == null) { var setupSplitterMethod = splitView.GetType().GetMethod("SetupSplitter", BindingFlags.Instance | BindingFlags.NonPublic); if (setupSplitterMethod == null) { #if DEV_MODE Debug.LogError("Minimize: SetupSplitter method not found."); #endif return; } setupSplitterMethod.Invoke(splitView); splitState = splitStateField.GetValue(splitView); if (splitState == null) { #if DEV_MODE Debug.LogError("Minimize: splitState was null event after calling SetupSplitter."); #endif return; } } #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(splitState != null); #endif var realSizesField = splitState.GetType().GetField("realSizes", BindingFlags.Instance | BindingFlags.Public); if (realSizesField == null) { #if DEV_MODE Debug.LogError("Minimize: realSizesField field not found."); #endif return; } var realSizes = (int[])realSizesField.GetValue(splitState); #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(realSizes != null); #endif if (realSizes.Length == 1) { #if DEV_MODE && DEBUG_MINIMIZE Debug.Log("minimizing with realSizes count 1"); #endif var setPosition = window.position; setPosition.height = 11f; window.position = setPosition; window.minSize = new Vector2(window.minSize.x, 11f); window.maxSize = new Vector2(window.maxSize.x, 11f); minimized = true; minimizedDimensions = window.position.size; return; } // minimizing in horizontal split not supported yet var verticalField = splitView.GetType().GetField("vertical", BindingFlags.Instance | BindingFlags.Public); if (verticalField == null) { #if DEV_MODE Debug.LogError("Minimize: vertical field not found."); #endif return; } bool vertical = (bool)verticalField.GetValue(splitView); if (!vertical) { #if DEV_MODE && DEBUG_MINIMIZE Debug.Log("aborting minimize because horizontal splits are not supported"); #endif return; } if (splitViewSizesBeforeMinimize != null) { ArrayPool <int> .Dispose(ref splitViewSizesBeforeMinimize); } splitViewSizesBeforeMinimize = ArrayPool <int> .Create(realSizes.Length); Array.Copy(realSizes, splitViewSizesBeforeMinimize, realSizes.Length); splitViewIndexBeforeMinimize = realSizes.Length - 1; splitViewPositionBeforeMinimize = 0f; for (int n = 0; n < splitViewIndexBeforeMinimize; n++) { splitViewPositionBeforeMinimize += realSizes[n]; } #if DEV_MODE && DEBUG_MINIMIZE Debug.Log("splitViewIndexWas=" + splitViewIndexBeforeMinimize + ", sizesBeforeMinimize=" + StringUtils.ToString(splitViewSizesBeforeMinimize) + ", splitViewPositionWas=" + splitViewPositionBeforeMinimize); #endif var placeView = splitView.GetType().GetMethod("PlaceView", BindingFlags.Instance | BindingFlags.NonPublic); if (placeView == null) { #if DEV_MODE Debug.LogError("Minimize: PlaceView method not found."); #endif return; } const float SetSize = 11f; //vertical ? 11f : 80f; float sizeChanged = splitViewSizesBeforeMinimize[splitViewIndexBeforeMinimize] - SetSize; #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(sizeChanged > 0f); #endif var parameters = new object[] { splitViewIndexBeforeMinimize, splitViewPositionBeforeMinimize + sizeChanged, SetSize }; placeView.Invoke(splitView, parameters); var reflowMethod = splitView.GetType().GetMethod("Reflow", BindingFlags.Instance | BindingFlags.NonPublic); if (reflowMethod == null) { #if DEV_MODE Debug.LogError("Minimize: Reflow method not found."); #endif return; } reflowMethod.Invoke(splitView); #if DEV_MODE && PI_ASSERTATIONS Debug.Assert(vertical); #endif window.minSize = new Vector2(window.minSize.x, SetSize); window.maxSize = new Vector2(window.maxSize.x, SetSize); minimized = true; } else { #if DEV_MODE && DEBUG_MINIMIZE Debug.Log("dockArea.parent: " + (splitView == null ? "null" : splitView.GetType().Name)); Debug.Log("aborting minimize because splitView was null, with dockArea.parent=" + (splitView == null ? "null" : splitView.GetType().Name)); #endif } } minimizedDimensions = window.position.size; }